获取手机的一些信息

获取手机的一些信息

获取手机上的信息,如手机型号,手机系统,本机密码等信息。
代码如下
package com.example.admin.phonemessagedemo;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Debug;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

/**
* Created by admin on 2017/3/23.
*/
public class DevicesUtil {
/**
 * 获取手机型号
 */
public static String getDeviceModel() {
    return android.os.Build.MODEL;
}

/**
 * 获取手机os系统
 */
public static String getDeviceOsVersion() {
    return android.os.Build.VERSION.RELEASE;
}

/**
 * 获取设备的IMEI
 */
public static String getIMEI() {
    Context context = BaseApplication.getInstance();
    if (null == context) {
        return null;
    }
    String imei = null;
    try {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        imei = tm.getDeviceId();
    } catch (Exception e) {
    }
    return imei;
}

/**
 * 获取当前设备的SN
 */
public static String getSimSN() {
    Context context = BaseApplication.getInstance();
    if (null == context) {
        return null;
    }
    String simSN = null;
    try {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        simSN = tm.getSimSerialNumber();
    } catch (Exception e) {
    }
    return simSN;
}

/**
 * 获取当前设备的MAC地址
 */
public static String getMacAddress() {
    Context context = BaseApplication.getInstance();
    if (null == context) {
        return null;
    }
    String mac = null;
    try {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wm.getConnectionInfo();
        mac = info.getMacAddress();
    } catch (Exception e) {
    }
    return mac;
}

/**
 * 检测手机是否已插入SIM卡
 */
public static boolean isCheckSimCardAvailable() {
    Context context = BaseApplication.getInstance();
    if (null == context) {
        return false;
    }
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return tm.getSimState() == TelephonyManager.SIM_STATE_READY;
}

/**
 * sim卡是否可读
 */
public static boolean isCanUseSim() {
    Context context = BaseApplication.getInstance();
    if (null == context) {
        return false;
    }
    try {
        TelephonyManager mgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return TelephonyManager.SIM_STATE_READY == mgr.getSimState();
    } catch (Exception e) {

    }
    return false;
}

/**
 * 取得当前sim手机卡的imsi
 */
public static String getIMSI() {
    Context context = BaseApplication.getInstance();
    if (null == context) {
        return null;
    }
    String imsi = null;
    try {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        imsi = tm.getSubscriberId();
    } catch (Exception e) {

    }
    return imsi;
}

/**
 * 返回本地手机号码,这个号码不一定能获取到
 */
public static String getNativePhoneNumber(Context context) {
    TelephonyManager telephonyManager;
    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String NativePhoneNumber = null;
    NativePhoneNumber = telephonyManager.getLine1Number();
    return NativePhoneNumber;
}

/**
 * 返回手机服务商名字
 */
public static String getProvidersName() {
    String ProvidersName = null;
    // 返回唯一的用户ID;就是这张卡的编号神马的
    String IMSI = getIMSI();
    if (TextUtils.isEmpty(IMSI)) {
        return "其他服务商:" + IMSI;
    }
    // IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
    if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
        ProvidersName = "中国移动";
    } else if (IMSI.startsWith("46001")) {
        ProvidersName = "中国联通";
    } else if (IMSI.startsWith("46003")) {
        ProvidersName = "中国电信";
    } else {
        ProvidersName = "其他服务商:" + IMSI;
    }
    return ProvidersName;
}


/**
 * 获得设备ip地址
 */
public static String getLocalIPAddress() {
    try {
        Enumeration en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface intf = en.nextElement();
            Enumeration enumIpAddr = intf.getInetAddresses();
            while (enumIpAddr.hasMoreElements()) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {

    }
    return null;
}

/**
 * 获取设备信息
 */
public static String[] getDivceInfo() {
    String str1 = "/proc/cpuinfo";
    String str2 = "";
    String[] cpuInfo = {"", ""};
    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) {

    }
    return cpuInfo;
}

/**
 * 判断手机CPU是否支持NEON指令集
 */
public static boolean isNEON() {
    boolean isNEON = false;
    String cupinfo = getCPUInfos();
    if (cupinfo != null) {
        cupinfo = cupinfo.toLowerCase();
        isNEON = cupinfo != null && cupinfo.contains("neon");
    }
    return isNEON;
}

/**
 * 读取CPU信息文件,获取CPU信息
 */
@SuppressWarnings("resource")
private static String getCPUInfos() {
    String str1 = "/proc/cpuinfo";
    String str2 = "";
    StringBuilder resusl = new StringBuilder();
    String resualStr = null;
    try {
        FileReader fr = new FileReader(str1);
        BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
        while ((str2 = localBufferedReader.readLine()) != null) {
            resusl.append(str2);
            // String cup = str2;
        }
        if (resusl != null) {
            resualStr = resusl.toString();
            return resualStr;
        }
    } catch (IOException e) {

    }
    return resualStr;
}

/**
 * 获取当前设备cpu的型号
 */
public static int getCPUModel() {
    return matchABI(getSystemProperty("ro.product.cpu.abi")) | matchABI(getSystemProperty("ro.product.cpu.abi2"));
}

/**
 * 匹配当前设备的cpu型号
 */
private static int matchABI(String abiString) {
    if (TextUtils.isEmpty(abiString)) {
        return 0;
    }
    if ("armeabi".equals(abiString)) {
        return 1;
    } else if ("armeabi-v7a".equals(abiString)) {
        return 2;
    } else if ("x86".equals(abiString)) {
        return 4;
    } else if ("mips".equals(abiString)) {
        return 8;
    }
    return 0;
}

/**
 * 获取CPU核心数
 */
public static int getCpuCount() {
    return Runtime.getRuntime().availableProcessors();
}

/**
 * 获取Rom版本
 */
public static String getRomversion() {
    String rom = "";
    try {
        String modversion = getSystemProperty("ro.modversion");
        String displayId = getSystemProperty("ro.build.display.id");
        if (modversion != null && !modversion.equals("")) {
            rom = modversion;
        }
        if (displayId != null && !displayId.equals("")) {
            rom = displayId;
        }
    } catch (Exception e) {

    }
    return rom;
}

/**
 * 获取系统配置参数
 */
public static String getSystemProperty(String key) {
    String pValue = null;
    try {
        Class c = Class.forName("android.os.SystemProperties");
        Method m = c.getMethod("get", String.class);
        pValue = m.invoke(null, key).toString();
    } catch (Exception e) {

    }
    return pValue;
}

/**
 * 获取系统中的Library包
 */
public static List getSystemLibs() {
    Context context = BaseApplication.getInstance();
    if (null == context) {
        return null;
    }
    PackageManager pm = context.getPackageManager();
    String[] libNames = pm.getSystemSharedLibraryNames();
    List listLibNames = Arrays.asList(libNames);
    return listLibNames;
}

/**
 * 获取手机外部总空间大小
 *
 * @return 大小(单位:字节)
 */
@SuppressWarnings("deprecation")
public static long getTotalExternalMemorySize() {
    long totalSpace = -1L;
    if (isSDCardMounted()) {
        try {
            String path = Environment.getExternalStorageDirectory().getPath();// 获取外部存储目录即 SDCard
            StatFs stat = new StatFs(path);
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            totalSpace = totalBlocks * blockSize;
        } catch (Exception e) {
        }
    }
    return totalSpace;
}

/**
 * 获取手机外部可用空间大小
 *
 * @return 大小(单位:字节)
 */
@SuppressWarnings("deprecation")
public static long getAvailableExternalMemorySize() {
    long availableSpace = -1L;
    if (isSDCardMounted()) {
        try {
            String path = Environment.getExternalStorageDirectory().getPath();
            StatFs stat = new StatFs(path);
            availableSpace = stat.getAvailableBlocks() * (long) stat.getBlockSize();
        } catch (Exception e) {
        }
    }
    return availableSpace;
}

/**
 * 获取手机内部总空间大小
 *
 * @return 大小(单位:字节)
 */
@SuppressWarnings("deprecation")
public static long getTotalInternalMemorySize() {
    long totalSpace = -1L;
    try {
        String path = Environment.getDataDirectory().getPath();
        StatFs stat = new StatFs(path);
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();// 获取该区域可用的文件系统数
        totalSpace = totalBlocks * blockSize;
    } catch (Exception e) {

    }
    return totalSpace;
}

/**
 * 获取手机内部可用空间大小
 *
 * @return 大小(单位:字节)
 */
@SuppressWarnings("deprecation")
public static long getAvailableInternalMemorySize() {
    long availableSpace = -1l;
    try {
        String path = Environment.getDataDirectory().getPath();// 获取 Android 数据目录
        StatFs stat = new StatFs(path);// 一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况
        long blockSize = stat.getBlockSize();// 返回 Int ,大小,以字节为单位,一个文件系统
        long availableBlocks = stat.getAvailableBlocks();// 返回 Int ,获取当前可用的存储空间
        availableSpace = availableBlocks * blockSize;
    } catch (Exception e) {

    }
    return availableSpace;
}

/**
 * 获取单个应用最大分配内存,单位为byte
 */
public static long getOneAppMaxMemory() {
    Context context = BaseApplication.getInstance();
    if (context == null) {
        return -1;
    }
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    return activityManager.getMemoryClass() * 1024 * 1024;
}

/**
 * 获取指定本应用占用的内存,单位为byte
 */
public static long getUsedMemory() {
    return getUsedMemory(null);
}

/**
 * 获取指定包名应用占用的内存,单位为byte
 */
public static long getUsedMemory(String packageName) {
    Context context = BaseApplication.getInstance();
    if (context == null) {
        return -1;
    }
    if (TextUtils.isEmpty(packageName)) {
        packageName = context.getPackageName();
    }
    long size = 0;
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List runapps = activityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo runapp : runapps) { // 遍历运行中的程序
        if (packageName.equals(runapp.processName)) {// 得到程序进程名,进程名一般就是包名,但有些程序的进程名并不对应一个包名
            // 返回指定PID程序的内存信息,可以传递多个PID,返回的也是数组型的信息
            Debug.MemoryInfo[] processMemoryInfo = activityManager.getProcessMemoryInfo(new int[]{runapp.pid});
            // 得到内存信息中已使用的内存,单位是K
            size = processMemoryInfo[0].getTotalPrivateDirty() * 1024;
        }
    }
    return size;
}

/**
 * 获取手机剩余内存,单位为byte
 */
public static long getAvailableMemory() {
    Context context = BaseApplication.getInstance();
    if (context == null) {
        return -1;
    }
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(info);
    return info.availMem;
}

/**
 * 手机低内存运行阀值,单位为byte
 */
public static long getThresholdMemory() {
    Context context = BaseApplication.getInstance();
    if (context == null) {
        return -1;
    }
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(info);
    return info.threshold;
}

/**
 * 手机是否处于低内存运行
 */
public static boolean isLowMemory() {
    Context context = BaseApplication.getInstance();
    if (context == null) {
        return false;
    }
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(info);
    return info.lowMemory;
}

/**
 * 是否装载了SD卡
 *
 * @return 是否装载
 */
private static boolean isSDCardMounted() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        return true;
    else
        return false;
}
}

补充:

    因为6.0增加了权限管理,用上述获取mac的方法在6.0的系统上将默认返回02:00:00:00:00:00,下面补充一种万能的方法,在6.0和低版本的手机上都能获取到mac地址的方法,亲测有效。

public String getMacAddr() {
    try {
        List all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }

            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                res1.append(String.format("%02X:",b));
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
    }
    return "02:00:00:00:00:00";
}

注意

并不是所有的信息都可以获得的,根据手机型号、系统、用户权限设置的不同,可能有些设备能获得,有些获取不到。

你可能感兴趣的:(android)