[英]How to display the current device's IMEI in a TextView?
I tried to display the Device's IMEI in textView using this code, but it doesn't work. 我试图使用此代码在textView中显示设备的IMEI,但它不起作用。 Nothing appears when I run the program.
当我运行程序时,什么都没有出现。
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getDeviceIMEI();
}
});
private void getDeviceIMEI() {
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String IMEI_Number_Holder = telephonyManager.getDeviceId();
textView.setText(IMEI_Number_Holder);
}
I also added this permission in AndroidManifest.xml: 我还在AndroidManifest.xml中添加了此权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
try out this code. 试试这个代码。
/**
* Return the IMEI.
* Must hold {@code <uses-permission android:name="android.permission.READ_PHONE_STATE" />}
* @return the IMEI
*/
@SuppressLint("HardwareIds")
@RequiresPermission(READ_PHONE_STATE)
public static String getIMEI() {
TelephonyManager tm = (TelephonyManager) Toolkit.getApp().getSystemService(Context.TELEPHONY_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) return tm.getImei();
return tm.getDeviceId();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.