package com.liu.phone.help;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.List;import android.app.Activity;import android.app.ActivityManager;import android.app.ActivityManager.MemoryInfo;import android.content.Context;import android.content.pm.ApplicationInfo;import android.content.pm.PackageInfo;import android.net.wifi.WifiInfo;import android.net.wifi.WifiManager;import android.telephony.TelephonyManager;import android.text.format.Formatter;import android.util.DisplayMetrics;import android.util.Log;/** * 手機的基本資料類 * @author Administrator * */public class PhoneInfo { public static final String TAG = "PhoneInfo"; /** * 擷取手機上所有的APP * @return */ public static String getAllApp(Context context) { String result = ""; List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0); for (PackageInfo i : packages) { if ((i.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { result += i.applicationInfo.loadLabel(context.getPackageManager()).toString() + ","; } } return result.substring(0, result.length() - 1); } /** * 擷取手機可用記憶體 * @param context * @return */ public static String getAvailMemory(Context context) {// 擷取android當前可用記憶體大小 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo mi = new MemoryInfo(); am.getMemoryInfo(mi); return Formatter.formatFileSize(context, mi.availMem);// 將擷取的記憶體大小規格化 } /** * 擷取手機的記憶體資訊 * @return */ public static String[] getTotalMemory(Context context) { String[] result = {"",""}; //1-total 2-avail ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo mi = new MemoryInfo(); am.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(context, mTotalMem); result[1] = Formatter.formatFileSize(context, mAvailMem); Log.i(TAG, "meminfo total:" + result[0] + " used:" + result[1]); return result; } /** * 擷取手機CPU資訊 * @return */ public static 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; } /** * 擷取手機mac地址 * @return */ public static String getMacAddress(Context context){ String result; WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); result = wifiInfo.getMacAddress(); return result; } /** * 擷取手機手機IMEI,IMSI,手機型號,手機號碼 */ public static String getInfo(Context context) { TelephonyManager mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); StringBuilder sb = new StringBuilder(); String imei = mTm.getDeviceId(); sb.append(imei).append(","); String imsi = mTm.getSubscriberId(); sb.append(imsi).append(","); String mtype = android.os.Build.MODEL; // 手機型號 sb.append(mtype).append(","); String numer = mTm.getLine1Number(); // 手機號碼,有的可得,有的不可得 sb.append(numer).append(","); return sb.toString(); } /** * 擷取手機的寬度與高度 */ public static String getWeithAndHeight(Activity activity){ //這種方式在service中無法使用, DisplayMetrics dm = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dm); StringBuilder sb = new StringBuilder(); String width = String.valueOf(dm.widthPixels); //寬 sb.append(width).append(","); String height = String.valueOf(dm.heightPixels); //高 sb.append(height).append(","); return sb.toString(); // //在service中也能得到高和寬 // WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); // width = mWindowManager.getDefaultDisplay().getWidth(); // height = mWindowManager.getDefaultDisplay().getHeight(); } }