基础概念积累_1

1.IMEI号和IMSI号

(1)IMEI(International Mobile Equipment Identity,国际移动身份识别码):是由15位数字组成的”电子串号”,其组成结构为TAC(6位数字)+FAC(两位数字)+SNR(6位数字)+SP (1位数字)。它与每台手机一一对应,而且该码是全世界唯一的。每一只手机在组装完成后都将被赋予一个全球唯一的一组号码,这个号码从生产到交付使用都将被制造生产的厂商所记录。 IMEI码贴在手机背面的标志上,并且读写于手机内存中。它也是该手机在厂家的”档案”和”身份证号”。
(2)IMSI(International Mobile Subscriber Identification Number,国际移动用户识别码):是区别移动用户的标志,储存在SIM卡中,可用于区别移动用户的有效信息。其总长度不超过15位,同样使用0~9的数字。其中MCC是移动用户所属国家代号,占3位数字,中国的MCC规定为460;MNC是移动网号码,最多由两位数字组成,用于识别移动用户所归属的移动通信网;MSIN是移动用户识别码,用以识别某一移动通信网中的移动用户。

获取IMEI号和IMSI号需要权限,先要在AndroidManifest.xml文件中加上权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

在Android中获取IMEI号方式如下:

 /**
     * 获取手机IMEI号
     */
    public static String getIMEI(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();

        return imei;
    }

在Android中获取IMSI号方式如下:

   /**
       * 获取手机IMSI号
       */
     public static String getIMSI(Context context){
        TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = mTelephonyMgr.getSubscriberId();

        return imsi ;
    }

参考链接:http://blog.csdn.net/u013059863/article/details/49847109

2.获取安卓设备的mac地址

2.1 官方获取mac地址的方法是:

public static String getWifiMac(Context ctx) {
        WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        String str = info.getMacAddress();
        if (str == null) str = "";
        return str;
    }

2.2 使用adbshell 获取

/**
     * 这是使用adb shell命令来获取mac地址的方式
     * @return
     */
    public static String getMac() {
        String macSerial = null;
        String str = "";

        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }

在真机红米Note2上是有结果的:
但在genymotion模拟器上,结果如下 getWifiMac = 02:00:00:00:00:00,getMac() = null
2.3 除了上述问题外,还有一个方法可以获取到虚拟机的地址,如下:

package com.example.util;

import android.text.TextUtils;

import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;

/**
 * 获取Mac地址
 */
public class MacUtils {

    /**
     * 获取手机的MAC地址
     *
     * @return
     */
    public static String getMac() {
        String str = "";
        String macSerial = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (TextUtils.isEmpty(macSerial)) {
            try {
                return loadFileAsString("/sys/class/net/eth0/address")
                        .toUpperCase().substring(0, 17);
            } catch (Exception e) {
                e.printStackTrace();

            }

        }
        return macSerial;
    }

    public static String loadFileAsString(String fileName) throws Exception {
        FileReader reader = new FileReader(fileName);
        String text = loadReaderAsString(reader);
        reader.close();
        return text;
    }

    public static String loadReaderAsString(Reader reader) throws Exception {
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[4096];
        int readLength = reader.read(buffer);
        while (readLength >= 0) {
            builder.append(buffer, 0, readLength);
            readLength = reader.read(buffer);
        }
        return builder.toString();
    }

}

3.获取手机相关信息

//手机型号
mPhoneBrand = android.os.Build.MODEL;
//系统版本
mSystemVersion = android.os.Build.VERSION.RELEASE;
//本机手机号码
TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); 
mPhoneModel = phoneMgr.getLine1Number();

参考链接:https://www.cnblogs.com/hankzhouAndroid/p/6554131.html

猜你喜欢

转载自blog.csdn.net/sindyue/article/details/79584533