I\'m getting the IMEI
and device Id\'s, so here I am getting a problem getDeviceId()
is deprecated.
TelephonyManager tm = (Telephon
Since Android 10, you shouldn't request IMEI
https://developer.android.com/training/articles/user-data-ids#java
Workarround:
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (null != telephonyManager) {
if (telephonyManager.getDeviceId() != null){
deviceUniqueIdentifier = telephonyManager.getDeviceId();
}else{
//Workarroud here
deviceUniqueIdentifier = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
}
This is my solution:
@SuppressWarnings("deprecation")
private String getIMEINumber() {
String IMEINumber = "";
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
IMEINumber = telephonyMgr.getImei();
} else {
IMEINumber = telephonyMgr.getDeviceId();
}
}
return IMEINumber;
}
getDeviceId()
Returns the unique device ID of a subscription, for example, the IMEI for GSM and the MEID for CDMA phones. Return null if device ID is not available.
This method was deprecated in API level 26.
Use
(@link getImei}
which returnsIMEI
forGSM
or
(@link getMeid}
which returnsMEID
forCDMA
.
for more information read TelephonyManager
Try this to get IMEI
@RequiresApi(api = Build.VERSION_CODES.O)
TelephonyManager tm = (TelephonyManager)
getSystemService(this.TELEPHONY_SERVICE);
String imei = tm.getImei();
OR
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String imei = telephonyMgr.getImei();
} else {
String imei = telephonyMgr.getDeviceId();
}
Try this to get
MEID
@RequiresApi(api = Build.VERSION_CODES.O)
TelephonyManager tm = (TelephonyManager)
getSystemService(this.TELEPHONY_SERVICE);
String meid=tm.getMeid();
OR
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String meid=tm.getMeid();
}
Kotlin Code for getting DeviceId ( IMEI ) with handling with permission & compatibility check for all android versions :
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
== PackageManager.PERMISSION_GRANTED) {
// Permission is granted
val imei : String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
telephonyManager.imei
} else { // older OS versions
telephonyManager.getDeviceId()
}
imei?.let {
Log.i("Log", "DeviceId=$imei" )
}
} else { // Permission is not granted
}
Also add this permission to AndroidManifest.xml :
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- IMEI-->
copy and paste my this program and understood. we face issue here only in Permission (Run time and Check Permission Type );- Now i complete it and paste here a accurate program:
import android.*;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.UUID;
public class Login extends AppCompatActivity {
private Button loginBtn;
private TextView textView;
private String IMEINumber;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginBtn = (Button) findViewById(R.id.loginBtn);
textView = (TextView) findViewById(R.id.textView);
final int reqcode = 1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
String[] per = {Manifest.permission.READ_PHONE_STATE};
requestPermissions(per, reqcode);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(Login.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
IMEINumber = tm.getImei();
textView.setText(IMEINumber);
}
} else {
IMEINumber = tm.getDeviceId();
textView.setText(IMEINumber);
}
}
});
}
}
}
Edit: Since Android 10, you cannot request IMEI and MEID unless you have READ_PRIVILEGED_PHONE_STATE permission
Should not it be something like this?
if (Build.VERSION.SDK_INT >= 26) {
if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
deviceId = telMgr.getMeid();
} else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
deviceId = telMgr.getImei();
} else {
deviceId = ""; // default!!!
}
} else {
deviceId = telMgr.getDeviceId();
}