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" />
以下是效果视图:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 获取手机信息实例详解全部内容,希望文章能够帮你解决Android 获取手机信息实例详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)