How to get device's IMEI/ESN number with code programming But in android > 6

回眸只為那壹抹淺笑 提交于 2019-12-03 10:17:06

问题


My Android version is Marshmallow 6.0

How to Find / Get imei number in android > 6 programmatically.

Note : I added READ_PHONE_STATE permission inside AndroidManifest.xml file.

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

And inside MainActivity

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

//Device Id is IMEI number

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

回答1:


My Android version is Marshmallow 6.0

Note : You need Runtime Permissions

I added READ_PHONE_STATE permission inside AndroidManifest.xml file.

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

in MainActivity:

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);
}



回答2:


Also getDeviceId() method was deprecated in API level 26. So further you need to use getImei() or getAllCellInfo() for fetching imei details.




回答3:


Requires Permission: 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();
}

Return null if device ID is not available.



来源:https://stackoverflow.com/questions/39385672/how-to-get-devices-imei-esn-number-with-code-programming-but-in-android-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!