Android 获取手机基本信息方法整理

  • PHP二次开发
  • 2017-6-13 08:40
  • PHP二次开发
  • 阅读(543)
  • 评论(5
  • 摘要: 本方法在 小米5.1系统测试通过需要的权限:uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/uses-permission android:name="android.permission.READ_PHONE_STATE"/具体代码:package com. ...
    本方法在 小米5.1系统测试通过

    需要的权限:
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    具体代码:
    package com.example.test;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.lang.reflect.Method;

    import android.app.ActivityManager;
    import android.app.ActivityManager.MemoryInfo;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Environment;
    import android.os.StatFs;
    import android.telephony.TelephonyManager;
    import android.text.format.Formatter;
    import android.util.DisplayMetrics;

    public class DeviceInfo {
    public static String extSdcardPath;
    public static TelephonyManager mTelephonyMgr;
    public static String sdcardPath;
    public static ActivityManager am;

    // 获取设备供应商
    public static final String BRAND = getString("ro.product.brand");

    // 设备模型家族
    public static final String PRODUCT = getString("ro.product.name");

    // 操作系统版本
    public static final String RELEASE = getString("ro.build.version.release");

    // 固件版本
    public static final String FINGERPRINT = getString("ro.build.fingerprint");

    // 版本号
    public static final String ID = getString("ro.build.id");

    // 设备ID
    public static final String MODEL = getString("ro.product.model");

    // 操作系统版本代码
    public static final String SDK_INT = getString("ro.build.version.sdk");

    /** * 获得SD卡总大小 * * @return */
    public static String getSDTotalSize(Context context) {
    extSdcardPath = System.getenv("SECONDARY_STORAGE");
    StatFs stat = new StatFs(extSdcardPath);
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return Formatter.formatFileSize(context, blockSize * totalBlocks);
    }

    /** * 获得sd卡剩余容量,即可用大小 * * @return */
    public static String getSDAvailableSize(Context context) {
    extSdcardPath = System.getenv("SECONDARY_STORAGE");
    StatFs stat = new StatFs(extSdcardPath);
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return Formatter.formatFileSize(context, blockSize * availableBlocks);
    }

    /** * 获得机身内部存储总大小 * * @return */
    public static String getInsideTotalSize(Context context) {
    sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    StatFs stat = new StatFs(sdcardPath);
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return Formatter.formatFileSize(context, blockSize * totalBlocks);
    }

    /** * 获得机身存储可用内存 * * @return */
    public static String getOutsideAvailableSize(Context context) {
    sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    StatFs stat = new StatFs(sdcardPath);
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return Formatter.formatFileSize(context, blockSize * availableBlocks);
    }

    // 获取总内存的大小
    public static String getTotalMemory(Context context) {
    // MemTotal: 341780 kB
    try {
    FileInputStream fis = new FileInputStream(new File("/proc/meminfo"));
    // 包装一个一行行读取的流
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));
    // 取到所有的内存信息
    String memTotal = bufferedReader.readLine();

    StringBuffer sb = new StringBuffer();

    for (char c : memTotal.toCharArray()) {

    if (c >= '0' && c <= '9') {
    sb.append(c);
    }
    }
    // 为了方便格式化 所以乘以1024
    long totalMemory = Long.parseLong(sb.toString()) * 1024;

    return Formatter.formatFileSize(context, totalMemory);

    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }

    }

    /**
    * 获取android当前可用内存大小
    * @param context
    * @return
    */
    public static String getAvailMemory(Context context) {

    am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
    MemoryInfo mi = new MemoryInfo();
    am.getMemoryInfo(mi);
    // mi.availMem; 当前系统的可用内存

    return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
    }

    /**
    * 获取IMEI
    * @param context
    * @return
    */
    public static String getIMEI(Context context) {
    mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String imei = mTelephonyMgr.getDeviceId();
    return imei;
    }

    /**
    * 获取IMSI
    * @param context
    * @return
    */
    public static String getIMSI(Context context) {
    mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String imsi = mTelephonyMgr.getSubscriberId();
    return imsi;
    }

    /**
    * 获取屏幕分辨率
    * @param context
    * @return
    */
    public static String getResolution(Context context) {
    DisplayMetrics dm2 = context.getResources().getDisplayMetrics();
    return dm2.widthPixels + "," + dm2.heightPixels;
    }

    // 判断手机是否root
    public static boolean isRoot() {
    String binPath = "/system/bin/su";
    String xBinPath = "/system/xbin/su";

    if (new File(binPath).exists() && isCanExecute(binPath)) {
    return true;
    }
    if (new File(xBinPath).exists() && isCanExecute(xBinPath)) {
    return true;
    }
    return false;
    }

    private static boolean isCanExecute(String filePath) {
    java.lang.Process process = null;
    try {
    process = Runtime.getRuntime().exec("ls -l " + filePath);
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String str = in.readLine();
    if (str != null && str.length() >= 4) {
    char flag = str.charAt(3);
    if (flag == 's' || flag == 'x')
    return true;
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (process != null) {
    process.destroy();
    }
    }
    return false;
    }

    /**
    * 获取SIM卡运营商
    * @param context
    * @return
    */
    public static String getOperator(Context context) {
    String operator = null;
    mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String imsi = mTelephonyMgr.getSubscriberId();

    if (imsi != null) {

    if (imsi.startsWith("46000") || imsi.startsWith("46002"))

    {
    // 因为移动网络编号46000下的IMSI已经用完,所以虚拟了一个46002编号,134/159号段使用了此编号 //中国移动
    operator = "中国移动";

    } else if (imsi.startsWith("46001")) {

    // 中国联通
    operator = "中国联通";

    } else if (imsi.startsWith("46003")) {

    // 中国电信
    operator = "中国电信";
    }

    }
    return operator;
    }

    /**
    * 获取网络类型
    * @param context
    * @return
    */
    public static String getNetworkTypeName(Context context) {
    if (context != null) {
    ConnectivityManager connectMgr = (ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectMgr != null) {
    NetworkInfo info = connectMgr.getActiveNetworkInfo();
    if (info != null) {
    switch (info.getType()) {
    case ConnectivityManager.TYPE_WIFI:
    return "WIFI";
    case ConnectivityManager.TYPE_MOBILE:
    return getNetworkTypeName(info.getSubtype());
    }
    }
    }
    }
    return getNetworkTypeName(TelephonyManager.NETWORK_TYPE_UNKNOWN);
    }

    public static String getNetworkTypeName(int type) {
    switch (type) {
    case TelephonyManager.NETWORK_TYPE_GPRS:
    return "GPRS";
    case TelephonyManager.NETWORK_TYPE_EDGE:
    return "EDGE";
    case TelephonyManager.NETWORK_TYPE_UMTS:
    return "UMTS";
    case TelephonyManager.NETWORK_TYPE_HSDPA:
    return "HSDPA";
    case TelephonyManager.NETWORK_TYPE_HSUPA:
    return "HSUPA";
    case TelephonyManager.NETWORK_TYPE_HSPA:
    return "HSPA";
    case TelephonyManager.NETWORK_TYPE_CDMA:
    return "CDMA";
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
    return "CDMA - EvDo rev. 0";
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
    return "CDMA - EvDo rev. A";
    case TelephonyManager.NETWORK_TYPE_EVDO_B:
    return "CDMA - EvDo rev. B";
    case TelephonyManager.NETWORK_TYPE_1xRTT:
    return "CDMA - 1xRTT";
    case TelephonyManager.NETWORK_TYPE_LTE:
    return "LTE";
    case TelephonyManager.NETWORK_TYPE_EHRPD:
    return "CDMA - eHRPD";
    case TelephonyManager.NETWORK_TYPE_IDEN:
    return "iDEN";
    case TelephonyManager.NETWORK_TYPE_HSPAP:
    return "HSPA+";
    default:
    return "UNKNOWN";
    }
    }

    /**\
    * 获取网络接入点
    * @param context
    * @return
    */
    public static String getApn(Context context) {
    ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = conManager.getActiveNetworkInfo();

    String apn = ni.getExtraInfo();// 获取网络接入点,这里一般为cmwap和cmnet
    return apn;
    }

    /**
    * SystemProperties 系统属性映射
    * @param key
    * @return
    */
    public static String getString(String key) {
    String result = "";
    try {
    Class<?> c = Class.forName("android.os.SystemProperties");

    Method get = c.getMethod("get", String.class);
    result = (String) get.invoke(c, key);

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;
    }

    }



    PHP技术交流QQ群:422137578

    除非注明,文章均为 PHP二次开发 原创,转载请注明本文地址:http://www.php2.cc/article-2623-1.html

    相关阅读

    发表评论

    最新评论

    引用 自由职业   2017-6-29 14:33
    好好好好好 长的方法。。。。
    引用 9号彩票   2017-6-23 21:33
    可以随意获取这个信息吗
    引用 下拉通刷词软件   2017-6-20 14:32
    网上什么代码都有呢,手机app 竟然也有
    引用 淘宝优惠券   2017-6-18 10:40
    Android获取手机基本信息方法整理
    引用 自闭症   2017-6-13 21:13
    博主安卓小王子!支持下!

    查看全部评论(5)

    用户名:  *

    邮 箱:  *

    网 址: 注意加上“http://”哦!