Android 获取手机的一些基本信息类
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(); } }
有些东西会,但不精通,而我现在要做的就是精通,复习回顾,整理自己的框架,举一反三。。。