I'm getting the IMEI
and device Id's, so here I am getting a problem getDeviceId()
is deprecated.
TelephonyManager tm = (TelephonyManager)
getSystemService(this.TELEPHONY_SERVICE);
imei = tm.getDeviceId();
device = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID);
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 returns IMEI
for GSM
or (@link getMeid}
which returns MEID
for CDMA
.
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();
}
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;
}
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);
}
}
});
}
}
}
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-->