Android 擷取手機的一些基本資料類

來源:互聯網
上載者:User
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();      }    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.