如何通过代码编程获取设备的IMEI / ESN号但是在android>中6

时间:2016-09-08 08:05:40

标签: android android-6.0-marshmallow android-permissions imei

  

我的Android版本是 Marshmallow 6.0

如何在Android中查找/获取imei号码> 6以编程方式。

注意:我在AndroidManifest.xml文件中添加了READ_PHONE_STATE权限。

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

在MainActivity中

TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = manager.getDeviceId();

//Device Id is IMEI number

Log.d("msg", "Device id " + deviceid);

3 个答案:

答案 0 :(得分:15)

  

我的Android版本是 Marshmallow 6.0

注意:您需要运行时权限

我在AndroidManifest.xml文件中添加了READ_PHONE_STATE权限。

private static final int PERMISSIONS_REQUEST_READ_PHONE_STATE = 999;

private TelephonyManager mTelephonyManager;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                PERMISSIONS_REQUEST_READ_PHONE_STATE);
    } else {
        getDeviceImei();
    }
....

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    if (requestCode == PERMISSIONS_REQUEST_READ_PHONE_STATE
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        getDeviceImei();
    }
}

private void getDeviceImei() {

    mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String deviceid = mTelephonyManager.getDeviceId();
    Log.d("msg", "DeviceImei " + deviceid);
}
MainActivity中的

Background="{Binding IsSelected, Converter={StaticResource SelectedToBackgroundConverter}}"

答案 1 :(得分:2)

在API级别26中也不推荐使用getDeviceId()方法。 因此,您需要使用getImei()或getAllCellInfo()来获取imei详细信息。

答案 2 :(得分:-1)

需要权限:READ_PHONE_STATE

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

/**
 * Gets the device unique id called IMEI. Sometimes, this returns 00000000000000000 for the
 * rooted devices.
 **/
public static String getDeviceImei(Context ctx) {
    TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    Log.i(Tag, "Return Sim count in device : "+manager.getPhoneCount());
    Log.i(Tag, "Defualt device ID: "+manager.getDeviceId());
    Log.i(Tag, "First Sim IMEI:  "+manager.getDeviceId(0));
    Log.i(Tag, "Secand Sim IMEI "+manager.getDeviceId(1));
    return telephonyManager.getDeviceId();
}

如果设备ID不可用,则返回null。