Intent.ACTION_CALL:android.permission.CALL_PHONE是意图所必需的缺少权限
作者:互联网
我正在尝试使用下面提供的代码以编程方式致电号码:
if(ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},CALL);
}
else
{
String number="56789855";
Intent intent4=new Intent(Intent.ACTION_CALL);
intent4.setData(Uri.parse("tel:"+number));
startActivity(intent4);
}
我也在Android清单文件中设置了权限:
<uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission>
Android Studio引发了一个错误:
06002
我似乎无法找到发生此错误的原因.任何帮助将不胜感激.
解决方法:
尝试这个,
必需的android.Manifest.permission.CALL_PHONE而不是Manifest.permission.CALL_PHONE
private Context mContext=YourActivity.this;
private static final int REQUEST = 112;
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.CALL_PHONE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
makeCall();
}
} else {
makeCall();
}
获取权限结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall();
} else {
Toast.makeText(mContext, "The app was not allowed to call.", Toast.LENGTH_LONG).show();
}
}
}
}
检查棉花糖的权限
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
表现
<uses-permission android:name="android.permission.CALL_PHONE" />
通话功能:
public void makeCall()
{
String number="56789855";
Intent intent4=new Intent(Intent.ACTION_CALL);
intent4.setData(Uri.parse("tel:"+number));
startActivity(intent4);
}
标签:android-permissions,permissions,android-intent,android 来源: https://codeday.me/bug/20191026/1934140.html