我想要一个代码来获取IMEI并在textview中显示
这是我的代码:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String IMEI;
IMEI = retrieve_IMEI.get_dev_id().toString();
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(IMEI);
}
public static class retrieve_IMEI {
public static String get_dev_id() {
Context ctx = null;
// Getting the Object of TelephonyManager
TelephonyManager tManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
// Getting IMEI Number of Device
String Imei = tManager.getDeviceId();
return Imei;
}
}
我使用READ_PHONE_STATE权限
当我运行我的代码时,我会关闭力量。我该怎么办?
答案 0 :(得分:1)
您的上下文ctx为null,您正在使用它访问TelephonyManager类。请正确初始化
答案 1 :(得分:0)
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String IMEI;
IMEI = retrieve_IMEI.get_dev_id(this).toString();
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(IMEI);
}
public static class retrieve_IMEI {
public static String get_dev_id(Context ctx) {
// Getting the Object of TelephonyManager
TelephonyManager tManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
// Getting IMEI Number of Device
String Imei = tManager.getDeviceId();
return Imei;
}
}