Tm.Getdeviceid() Is Deprecated

TelephonyManager.getDeviceId(0) returns different results

As you said IMEI/Device Id is tagged to sim slot.

For dual SIM phones there are three IMEI values(one for each slot) and IMEI-SV.

Let’s say IMEI for slot 1 is: 123456789012345

IMEI for slot 2 is: 012500123456789

Depending on the scenarios, following is the returned value by telephonyManagerObj.getDeviceId():

  1. When you don’t have any SIM card, the method will return IMEI for slot1 i.e. 123456789012345
  2. When you have SIM card in slot 1, the method will return IMEI for slot1 i.e. 123456789012345
  3. When you have SIM card in both slots, the method will return IMEI for slot1 i.e. 123456789012345
  4. But when you have SIM card only in slot 2, the method will return IMEI for slot2 i.e. 012500123456789
  5. I found that on one device when I insert the SIM card incorrectly in slot 1 then the method returned IMEI-SV

One work around to maintain consistency is to store the IMEI in SharedPreference/Sqlite once you successfully managed to retrieve it.

So when you need IMEI value in your code, you can first check if its available in your local storage. If not available then retrieve the IMEI and store it for next time usage.

Be careful, getDeviceId() has been deprecated in Android O. Check this so for alternative

Will TelephonyManger.getDeviceId() return device id for Tablets like Galaxy Tab...?

This is not a duplicate question. As it turns out, Google's CTS require that getPhoneType of TelephonyManager needs to be none and getDeviceId of TelephonyManager needs to be null for non-phone devices.

So to get IMEI, please try to use:

String imei = SystemProperties.get("ro.gsm.imei")

Unfortunately, SystemProperties is a non-public class in the Android OS, which means it isn't publicly available to regular applications. Try looking at this post for help accessing it: Where is android.os.SystemProperties

get the IMEI number of phone having the Android Version 8.1 as the telephonyManager.getDeviceId(); have being deprecated after Marshmallow

Starting from android 8 you need to use getImei(int slotIndex) to retrieve the IMEI of the device.

You also need to add <uses-permission android:name="android.permission.READ_PHONE_STATE"/> permission in your AndroidManifest.xml

Remember to implement permission model to request permission at runtime as follows:

ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
1);
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {

// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

//continue using `getImei()` or `getDeviceId()`
} else {

//Use device Id or use fallback case
}
return;
}
}
}

How to get the device's IMEI/ESN programmatically in android?

You want to call android.telephony.TelephonyManager.getDeviceId().

This will return whatever string uniquely identifies the device (IMEI on GSM, MEID for CDMA).

You'll need the following permission in your AndroidManifest.xml:

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

in order to do this.

That being said, be careful about doing this. Not only will users wonder why your application is accessing their telephony stack, it might be difficult to migrate data over if the user gets a new device.

Update: As mentioned in the comments below, this is not a secure way to authenticate users, and raises privacy concerns. It is not recommended. Instead, look at the Google+ Login API if you want to implement a frictionless login system.

The Android Backup API is also available if you just want a lightweight way to persist a bundle of strings for when a user resets their phone (or buys a new device).

Kotlin : TelephonyManager.getImei() Call requires permission which may be rejected by user

For Java:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
} else {
// else for if they have already given permission
}
}

You can check this tutorial also: https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/

For Kotlin:

 val permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)

if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_PHONE_STATE), 1)
}

You can check this tutorial also: https://www.techotopia.com/index.php/Kotlin_-_Making_Runtime_Permission_Requests_in_Android



Related Topics



Leave a reply



Submit