我试图找到我的IMEI没有,但无法从我的设备到达这里是代码。我已经在我的Android清单提供了我的perssion但我仍然找不到。任何帮助将不胜感激。
Context context;
TextView imei_number;
Button get_imei;
String IMEI_Number_Holder;
TelephonyManager telephonyManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
imei_number = (TextView) findViewById(R.id.textView1);
get_imei = (Button) findViewById(R.id.button1);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 0);
} else {
//TODO
}
telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
get_imei.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_PHONE_STATE},1);
IMEI_Number_Holder = telephonyManager.getDeviceId();
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
imei_number.setText(IMEI_Number_Holder);
}
});
}
}
我尝试了很多方法,但无法得到请帮助我。这是另一个代码,我尝试过这段代码但得到的错误就像
引起:java.lang.SecurityException:getDeviceId:用户10183和当前进程都没有android.permission.READ_PHONE_STATE。
我应该怎么做请指导我在谷歌尝试了很多方法,但帮助不大。我的设备是OS是Nougat
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
String imeiNumber1 = tm.getDeviceId(1); //(API level 23)
String imeiNumber2 = tm.getDeviceId(2);
}
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
答案 0 :(得分:1)
我已更新您的onClick
方法:
get_imei.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE_REQUEST);
} else {
IMEI_Number_Holder = telephonyManager.getDeviceId();
imei_number.setText(IMEI_Number_Holder);
}
}
});
问题在于您的行动顺序。系统会检查您permission != PackageManager.PERMISSION_GRANTED
是否意味着未授予,而是在调用getDeviceId()
之后。