您好,登錄后才能下訂單哦!
Android 獲取手機(jī)信息
應(yīng)用信息:包名、版本號(hào)、版本名,手機(jī)是否有Root權(quán)限
手機(jī)信息:手機(jī)屏幕寬和高、當(dāng)前可用內(nèi)存大小、總內(nèi)存大小、IMEI號(hào)、IESI號(hào)、手機(jī)型號(hào)、手機(jī)品牌、手機(jī)MacAdd、CPU型號(hào)、CPU頻率
開門見山,以下是Java代碼,XML只有一個(gè)TextView顯示信息。
package com.example.getphoneinfo; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.telephony.TelephonyManager; import android.text.format.Formatter; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { TextView mPhoneInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { // TODO Auto-generated method stub getAvailMemory();// 獲取手機(jī)可用內(nèi)存大小 getTotalMemory();//獲取總內(nèi)存大小 getHeightAndWidth();//獲取屏幕寬高 getInfo();//獲取IMEI號(hào),IESI號(hào),手機(jī)型號(hào) getMacAddress();//獲取IMEI號(hào),IESI號(hào),手機(jī)型號(hào) getCpuInfo();//手機(jī)CPU信息 getPackage();//獲取軟件包名,版本名,版本號(hào) isRoot();//手機(jī)是否root String text = getHeightAndWidth() + "\n" + getTotalMemory() + "\n" + getAvailMemory() + "\n" + getInfo() + "\n" + getMacAddress() + "\n" + getCpuInfo() + "\n" + getPackage() + "\n" + isRoot(); mPhoneInfo.setText(text); } /** * 獲取軟件包名,版本名,版本號(hào) */ private String getPackage(){ try { String pkName = this.getPackageName(); String versionName = this.getPackageManager().getPackageInfo( pkName, 0).versionName; int versionCode = this.getPackageManager() .getPackageInfo(pkName, 0).versionCode; return "Package:" + pkName + "\nversionName:" + versionName + "\nversionCode:" + versionCode; } catch (Exception e) { } return null; } /** * 獲取手機(jī)是否root信息 * @return */ private String isRoot(){ String bool = "Root:false"; try{ if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists())){ bool = "Root:false"; } else { bool = "Root:true"; } } catch (Exception e) { } return bool; } /** * 獲取android當(dāng)前可用內(nèi)存大小 */ private String getAvailMemory() {// 獲取android當(dāng)前可用內(nèi)存大小 File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return "當(dāng)前可用內(nèi)存:" + Formatter.formatFileSize(MainActivity.this, blockSize * availableBlocks); } /** * 獲得系統(tǒng)總內(nèi)存 */ private String getTotalMemory() { String str1 = "/proc/meminfo";// 系統(tǒng)內(nèi)存信息文件 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader( localFileReader, 8192); str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統(tǒng)總內(nèi)存大小 arrayOfString = str2.split("\\s+"); initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統(tǒng)總內(nèi)存,單位是KB,乘以1024轉(zhuǎn)換為Byte localBufferedReader.close(); } catch (IOException e) { } return "總內(nèi)存大?。? + Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte轉(zhuǎn)換為KB或者M(jìn)B,內(nèi)存大小規(guī)格化 } /** * 獲得手機(jī)屏幕寬高 * @return */ public String getHeightAndWidth(){ int width=getWindowManager().getDefaultDisplay().getWidth(); int heigth=getWindowManager().getDefaultDisplay().getHeight(); String str = "Width:" + width+"\nHeight:"+heigth+""; return str; } /** * 獲取IMEI號(hào),IESI號(hào),手機(jī)型號(hào) */ private String getInfo() { TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE); String imei = mTm.getDeviceId(); String imsi = mTm.getSubscriberId(); String mtype = android.os.Build.MODEL; // 手機(jī)型號(hào) String mtyb= android.os.Build.BRAND;//手機(jī)品牌 String numer = mTm.getLine1Number(); // 手機(jī)號(hào)碼,有的可得,有的不可得 return "手機(jī)IMEI號(hào):"+imei+"\n手機(jī)IESI號(hào):"+imsi+"\n手機(jī)型號(hào):"+mtype+"\n手機(jī)品牌:"+mtyb+"\n手機(jī)號(hào)碼"+numer; } /** * 獲取手機(jī)MAC地址 * 只有手機(jī)開啟wifi才能獲取到mac地址 */ private String getMacAddress(){ String result = ""; WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); result = wifiInfo.getMacAddress(); return "手機(jī)macAdd:" + result; } /** * 手機(jī)CPU信息 */ private String getCpuInfo() { String str1 = "/proc/cpuinfo"; String str2 = ""; String[] cpuInfo = {"", ""}; //1-cpu型號(hào) //2-cpu頻率 String[] arrayOfString; try { FileReader fr = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); for (int i = 2; i < arrayOfString.length; i++) { cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " "; } str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); cpuInfo[1] += arrayOfString[2]; localBufferedReader.close(); } catch (IOException e) { } return "CPU型號(hào):" + cpuInfo[0] + "\nCPU頻率:" + cpuInfo[1]; } public void initView() { // TODO Auto-generated method stub mPhoneInfo = (TextView)findViewById(R.id.id_tv_phone_info); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
注意:添加權(quán)限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
以下是效果視圖:
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。