Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 429
Amir Hosseinzadeh Avatar asked Dec 05 '22 16:12

Amir Hosseinzadeh


2 Answers

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);
}
like image 71
Amir Hosseinzadeh Avatar answered Dec 23 '22 06:12

Amir Hosseinzadeh


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

like image 38
Ajmal Tk Avatar answered Dec 23 '22 05:12

Ajmal Tk