测试READ_PHONE_STATE的Android M权限(危险权限)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试READ_PHONE_STATE的Android M权限(危险权限)相关的知识,希望对你有一定的参考价值。
如果设备在使用getLine1Number()尝试获取电话号码时运行android 6.0或更高版本:
java.lang.SecurityException:需要READ_PHONE_STATE:用户10184和当前进程都没有android.permission.READ_PHONE_STATE。这是出来的。
我宣布许可为:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
答案
在Android 6.0中,您需要明确要求用户授予权限。仅仅在清单中声明它是不够的。
这篇文章中的article是开始学习新模型的好地方,但我将简要总结一下。
每次执行需要“危险权限”的操作时,都需要检查当前是否已授予权限,因为用户可以随时撤消该权限。
这可以使用checkSelfPermission
方法完成。
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// We do not have this permission. Let's ask the user
}
您可以使用requestPermissions
方法请求权限
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_READ_STATE);
其中PERMISSION_READ_STATE
是您定义的常量整数,以便稍后检入回调方法。
然后,您将在活动中覆盖onRequestPermissionsResult
,并查看是否已授予权限。如果是的话,你可以继续进行危险的行动。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_READ_STATE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted!
// you may now do the action that requires this permission
} else {
// permission denied
}
return;
}
}
}
另一答案
public class MainActivity extends AppCompatActivity {
TextView textView;
String device_unique_id,IMEI;
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
}
public void GetImei(View view)
{
loadIMEI();
}
public void loadIMEI() {
// Check if the READ_PHONE_STATE permission is already available.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_PHONE_STATE)) {
// get_imei_data();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
} else {
TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
IMEI = mngr.getDeviceId();
device_unique_id = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID);
textView.setText(device_unique_id+"----"+mngr.getDeviceId());
// READ_PHONE_STATE permission is already been granted.
Toast.makeText(this,"Alredy granted",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Toast.makeText(this,"Alredy DONE",Toast.LENGTH_SHORT).show();
TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
IMEI = mngr.getDeviceId();
device_unique_id = Settings.Secure.getString(this.getContentResolver(),Settings.Secure.ANDROID_ID);
textView.setText(device_unique_id+"----"+mngr.getDeviceId());
} else {
Toast.makeText(this,"ehgehfg",Toast.LENGTH_SHORT).show();
}
}
}
以上是关于测试READ_PHONE_STATE的Android M权限(危险权限)的主要内容,如果未能解决你的问题,请参考以下文章
如何解决此错误:android.permission.READ_PHONE_STATE?
警告您的 Apk 正在使用需要隐私政策的权限:(android.permission.READ_PHONE_STATE)