android获取手机信息
1.获取手机安装的应用信息(排除系统自带):
private String getAllApp() { String result = ""; List<PackageInfo> packages = getPackageManager().getInstalledPackages(0); for (PackageInfo i : packages) { if ((i.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { result += i.applicationInfo.loadLabel(getPackageManager()).toString() + ","; } } return result.substring(0, result.length() - 1); }
以字符串的形式返回应用的名字app1,app2,app3,......,appn中间的以","分开。当然还可以得到包名,icon图标,等等 。
2.获取手机可用内存和总内存:
Java代码:
private String[] getTotalMemory() { String[] result = {"",""}; //1-total 2-avail ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); mActivityManager.getMemoryInfo(mi); long mTotalMem = 0; long mAvailMem = mi.availMem; String str1 = "/proc/meminfo"; String str2; String[] arrayOfString; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024; localBufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } result[0] = Formatter.formatFileSize(this, mTotalMem); result[1] = Formatter.formatFileSize(this, mAvailMem); Log.i(TAG, "meminfo total:" + result[0] + " used:" + result[1]); return result; }
手机的内存信息主要在/proc/meminfo文件中,其中第一行就是总内存,而剩余内存可通过 ActivityManager.MemoryInfo得到 。
3.手机CPU信息
Java代码:
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) { } Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]); return cpuInfo; }
和内存信息同理,cpu信息可通过读取/proc/cpuinfo文件来得到,其中第一行为cpu型号,第二行为cpu频率。
4.获取手机MAC地址:
Java代码:
private String getMacAddress(){ String result = ""; WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); result = wifiInfo.getMacAddress(); Log.i(TAG, "macAdd:" + result); return result; }
MAC地址比较容易,直接通过WifiManager得到。
4.IMEI号,IESI号,手机型号等:
Java代码:
private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); String imei = mTm.getDeviceId(); String imsi = mTm.getSubscriberId(); String mtype = android.os.Build.MODEL; // 手机型号 String numer = mTm.getLine1Number(); // 手机号码,有的可得,有的不可得 }
5.获取手机屏幕高度:
Java代码:
private void getWeithAndHeight(){ //这种方式在service中无法使用, DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); String width = dm.widthPixels; //宽 String height = dm.heightPixels; //高 //在service中也能得到高和宽 WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); width = mWindowManager.getDefaultDisplay().getWidth(); height = mWindowManager.getDefaultDisplay().getHeight(); }