玩Android的朋友們進來看看吧!
1.擷取手機安裝的應用資訊(排除系統內建):
Java代碼:
1 private String getAllApp() {
2 String result = "";
3 List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
4 for (PackageInfo i : packages) {
5 if ((i.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
6 result += i.applicationInfo.loadLabel(getPackageManager()).toString() + ",";
7 }
8 }
9 return result.substring(0, result.length() - 1);
10 }
以字串的形式返回應用的名字app1,app2,app3,......,appn中間的以","分開。當然還可以得到包名,icon表徵圖,等等 。
2.擷取手機可用記憶體和總記憶體:
Java代碼:
1 private String[] getTotalMemory() {
2 String[] result = {"",""}; //1-total 2-avail
3 ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
4 mActivityManager.getMemoryInfo(mi);
5 long mTotalMem = 0;
6 long mAvailMem = mi.availMem;
7 String str1 = "/proc/meminfo";
8 String str2;
9 String[] arrayOfString;
10 try {
11 FileReader localFileReader = new FileReader(str1);
12 BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
13 str2 = localBufferedReader.readLine();
14 arrayOfString = str2.split("\\s+");
15 mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
16 localBufferedReader.close();
17 } catch (IOException e) {
18 e.printStackTrace();
19 }
20 result[0] = Formatter.formatFileSize(this, mTotalMem);
21 result[1] = Formatter.formatFileSize(this, mAvailMem);
22 Log.i(TAG, "meminfo total:" + result[0] + " used:" + result[1]);
23 return result;
24 }
手機的記憶體資訊主要在/proc/meminfo檔案中,其中第一行就是總記憶體,而剩餘記憶體可通過 ActivityManager.MemoryInfo得到 。
3.手機CPU資訊
Java代碼:
1 private String[] getCpuInfo() {
2 String str1 = "/proc/cpuinfo";
3 String str2 = "";
4 String[] cpuInfo = {"", ""}; //1-cpu型號 //2-cpu頻率
5 String[] arrayOfString;
6 try {
7 FileReader fr = new FileReader(str1);
8 BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
9 str2 = localBufferedReader.readLine();
10 arrayOfString = str2.split("\\s+");
11 for (int i = 2; i < arrayOfString.length; i++) {
12 cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
13 }
14 str2 = localBufferedReader.readLine();
15 arrayOfString = str2.split("\\s+");
16 cpuInfo[1] += arrayOfString[2];
17 localBufferedReader.close();
18 } catch (IOException e) {
19 }
20 Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);
21 return cpuInfo;
22 }
和記憶體資訊同理,cpu資訊可通過讀取/proc/cpuinfo檔案來得到,其中第一行為cpu型號,第二行為cpu頻率。
4.擷取手機MAC地址:
Java代碼:
1 private String getMacAddress(){
2 String result = "";
3 WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
4 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
5 result = wifiInfo.getMacAddress();
6 Log.i(TAG, "macAdd:" + result);
7 return result;
8 }
MAC地址比較容易,直接通過WifiManager得到。
4.IMEI號,IESI號,手機型號等:
Java代碼:
1 private void getInfo() {
2 TelephonyManager mTm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
3 String imei = mTm.getDeviceId();
4 String imsi = mTm.getSubscriberId();
5 String mtype = android.os.Build.MODEL; // 手機型號
6 String numer = mTm.getLine1Number(); // 手機號碼,有的可得,有的不可得
7 }
5.擷取手機螢幕高度:
Java代碼:
1 private void getWeithAndHeight(){
2 //這種方式在service中無法使用,
3 DisplayMetrics dm = new DisplayMetrics();
4 getWindowManager().getDefaultDisplay().getMetrics(dm);
5 String width = dm.widthPixels; //寬
6 String height = dm.heightPixels; //高
7
8 //在service中也能得到高和寬
9 WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
10 width = mWindowManager.getDefaultDisplay().getWidth();
11 height = mWindowManager.getDefaultDisplay().getHeight();
12 }
轉載地址:http://www.cnblogs.com/ayan/archive/2011/12/29/2306824.html