How to use code programming to obtain the IMEI / ESN number of the device, but in Android > 6
•
Android
How to find / get IMEI number > 6 in Android programmatically
Note: I added read to the androidmanifest. XML file_ PHONE_ State permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
And in mainactivity
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = manager.getdeviceid();
//Device Id is IMEI number
Log.d("msg", "Device id " + deviceid);
resolvent:
Note: you need runtime permissions
I added read to the androidmanifest. XML file_ PHONE_ State permission
<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);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码