您好,登錄后才能下訂單哦!
Android 獲取手機信息
應用信息:包名、版本號、版本名,手機是否有Root權限
手機信息:手機屏幕寬和高、當前可用內存大小、總內存大小、IMEI號、IESI號、手機型號、手機品牌、手機MacAdd、CPU型號、CPU頻率
開門見山,以下是Java代碼,XML只有一個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();// 獲取手機可用內存大小 getTotalMemory();//獲取總內存大小 getHeightAndWidth();//獲取屏幕寬高 getInfo();//獲取IMEI號,IESI號,手機型號 getMacAddress();//獲取IMEI號,IESI號,手機型號 getCpuInfo();//手機CPU信息 getPackage();//獲取軟件包名,版本名,版本號 isRoot();//手機是否root String text = getHeightAndWidth() + "\n" + getTotalMemory() + "\n" + getAvailMemory() + "\n" + getInfo() + "\n" + getMacAddress() + "\n" + getCpuInfo() + "\n" + getPackage() + "\n" + isRoot(); mPhoneInfo.setText(text); } /** * 獲取軟件包名,版本名,版本號 */ 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; } /** * 獲取手機是否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當前可用內存大小 */ private String getAvailMemory() {// 獲取android當前可用內存大小 File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return "當前可用內存:" + Formatter.formatFileSize(MainActivity.this, blockSize * availableBlocks); } /** * 獲得系統總內存 */ private String getTotalMemory() { String str1 = "/proc/meminfo";// 系統內存信息文件 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader( localFileReader, 8192); str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統總內存大小 arrayOfString = str2.split("\\s+"); initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統總內存,單位是KB,乘以1024轉換為Byte localBufferedReader.close(); } catch (IOException e) { } return "總內存大小:" + Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte轉換為KB或者MB,內存大小規格化 } /** * 獲得手機屏幕寬高 * @return */ public String getHeightAndWidth(){ int width=getWindowManager().getDefaultDisplay().getWidth(); int heigth=getWindowManager().getDefaultDisplay().getHeight(); String str = "Width:" + width+"\nHeight:"+heigth+""; return str; } /** * 獲取IMEI號,IESI號,手機型號 */ private String getInfo() { TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE); String imei = mTm.getDeviceId(); String imsi = mTm.getSubscriberId(); String mtype = android.os.Build.MODEL; // 手機型號 String mtyb= android.os.Build.BRAND;//手機品牌 String numer = mTm.getLine1Number(); // 手機號碼,有的可得,有的不可得 return "手機IMEI號:"+imei+"\n手機IESI號:"+imsi+"\n手機型號:"+mtype+"\n手機品牌:"+mtyb+"\n手機號碼"+numer; } /** * 獲取手機MAC地址 * 只有手機開啟wifi才能獲取到mac地址 */ private String getMacAddress(){ String result = ""; WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); result = wifiInfo.getMacAddress(); return "手機macAdd:" + result; } /** * 手機CPU信息 */ private String getCpuInfo() { String str1 = "/proc/cpuinfo"; String str2 = ""; String[] cpuInfo = {"", ""}; //1-cpu型號 //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型號:" + 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; } }
注意:添加權限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
以下是效果視圖:
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。