How to Get the Device'S Imei/Esn Programmatically in Android

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).

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

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

Best Way to get Device IMEI number android java programmatically with onRequestPermissionsResult

Please use this

manifest file

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

In Java File

 public static String getUniqueIMEIId(Context context) {
try {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return "";
}
String imei = telephonyManager.getDeviceId();
Log.e("imei", "=" + imei);
if (imei != null && !imei.isEmpty()) {
return imei;
} else {
return android.os.Build.SERIAL;
}
} catch (Exception e) {
e.printStackTrace();
}
return "not_found";
}

How to get device imei number in non-activity class?

Service has its own context you can use that, for your concern about getting the IMEI in a service use below code:

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("ranjith","started service");
TelephonyManager c=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String device_id = c.getDeviceId();
return super.onStartCommand(intent, flags, startId);
}
}

For your question about how to pass context to non-Activity class, the answer is just pass it as a parameter in the constructor to the other class, for eg:-

public class myActivityclass extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
nonActivityclass c=new nonActivityclass(Context context);
-----------------------------
}

In the non-Activity class:

public class nonActivityclass{
Context context;
public nonActivityclass(Context context)
{
this.context=context
}
//you can use context now
}

How get IMEI of device?

The answer is sad. We can't !

Like @Nitsh said, "The READ_PRIVILEGED_PHONE_STATE permission is only granted to apps signed with the platform key and privileged system apps".

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).



Related Topics



Leave a reply



Submit