如何通过代码编程获取设备的 IMEI/ESN 编号 但是在 android > 6
How to get device's IMEI/ESN number with code programming But in android > 6
My Android version is Marshmallow 6.0
如何以编程方式在 android > 6 中查找/获取 imei 号码。
注意:我在 AndroidManifest.xml 文件中添加了 READ_PHONE_STATE 权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
在 MainActivity 里面
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = manager.getDeviceId();
//Device Id is IMEI number
Log.d("msg", "Device id " + deviceid);
My Android version is Marshmallow 6.0
注意:您需要运行时权限
我在 AndroidManifest.xml 文件中添加了 READ_PHONE_STATE 权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
在 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);
}
需要权限:READ_PHONE_STATE
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
/**
* Gets the device unique id called IMEI. Sometimes, this returns 00000000000000000 for the
* rooted devices.
**/
public static String getDeviceImei(Context ctx) {
TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
Log.i(Tag, "Return Sim count in device : "+manager.getPhoneCount());
Log.i(Tag, "Defualt device ID: "+manager.getDeviceId());
Log.i(Tag, "First Sim IMEI: "+manager.getDeviceId(0));
Log.i(Tag, "Secand Sim IMEI "+manager.getDeviceId(1));
return telephonyManager.getDeviceId();
}
Return 如果设备 ID 不可用,则为空。
API 级别 26 中也弃用了 getDeviceId() 方法。
因此,您还需要使用 getImei() 或 getAllCellInfo() 来获取 imei 详细信息。
My Android version is Marshmallow 6.0
如何以编程方式在 android > 6 中查找/获取 imei 号码。
注意:我在 AndroidManifest.xml 文件中添加了 READ_PHONE_STATE 权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
在 MainActivity 里面
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = manager.getDeviceId();
//Device Id is IMEI number
Log.d("msg", "Device id " + deviceid);
My Android version is Marshmallow 6.0
注意:您需要运行时权限
我在 AndroidManifest.xml 文件中添加了 READ_PHONE_STATE 权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
在 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);
}
需要权限:READ_PHONE_STATE
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
/**
* Gets the device unique id called IMEI. Sometimes, this returns 00000000000000000 for the
* rooted devices.
**/
public static String getDeviceImei(Context ctx) {
TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
Log.i(Tag, "Return Sim count in device : "+manager.getPhoneCount());
Log.i(Tag, "Defualt device ID: "+manager.getDeviceId());
Log.i(Tag, "First Sim IMEI: "+manager.getDeviceId(0));
Log.i(Tag, "Secand Sim IMEI "+manager.getDeviceId(1));
return telephonyManager.getDeviceId();
}
Return 如果设备 ID 不可用,则为空。
API 级别 26 中也弃用了 getDeviceId() 方法。 因此,您还需要使用 getImei() 或 getAllCellInfo() 来获取 imei 详细信息。