撤销权限 android.permission.CALL_PHONE
Revoked permission android.permission.CALL_PHONE
我正在尝试使用以下代码以编程方式呼叫号码:
String number = ("tel:" + numTxt.getText());
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(number));
startActivity(intent);
我在清单中设置了权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>
我正在使用真实设备进行测试和调试,它是带有 Android M 的 Nexus 5,我的 compileSdkVersion 是 23。我收到以下安全异常:
error: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{cbbd7c1 5228:com.dialerTest.DialerApp/u0a96} (pid=5228, uid=10096) with revoked permission android.permission.CALL_PHONE
我在网络和这个社区中搜索了类似的 Q/A,但找不到答案。任何帮助将不胜感激。
在 android 6.0(Api 23 级)中,我们有一个叫做 "Runtime Permissions" 的东西。你必须阅读它。
您可以找到文档 here。
权限CALL_PHONE属于危险权限组。
因此,如果您的应用目标 SDK 为 23 或更高版本,并且您的设备在 Android 6.0 或更高版本上为 运行,则您必须在应用为 运行 时请求 CALL_PHONE 权限。
示例:
String number = ("tel:" + numTxt.getText());
mIntent = new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(number));
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
// MY_PERMISSIONS_REQUEST_CALL_PHONE is an
// app-defined int constant. The callback method gets the
// result of the request.
} else {
//You already have permission
try {
startActivity(mIntent);
} catch(SecurityException e) {
e.printStackTrace();
}
}
当您的应用请求权限时,系统会向用户显示一个对话框。当用户响应时,系统会调用您应用的 onRequestPermissionsResult() 方法,将用户响应传递给它。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the phone call
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
您的代码只有在您发出 ACTION_DIAL
时才能工作,而不是 ACTION_CALL
需要请求许可的地方,因此如果您想拨打电话,请按照以下示例操作:
清单:
<uses-permission android:name="android.permission.CALL_PHONE" />
代码:
import static android.Manifest.permission.CALL_PHONE;
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:0612312312"));
/*
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:0612312312"));
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
}*/
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(i);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
片段class
第一步:
导入静态 android.Manifest.permission.CALL_PHONE;
步骤 2:Where 您的点击按钮:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" +driver_no ));
if (ContextCompat.checkSelfPermission(getActivity(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
[或者] 你在 activity 中使用 class 表示将 getActivity 更改为 getApplicationContext()
仅供参考:如果您的目标是 Android 11,则需要在清单中为 Intent.ACTION_DIAL 添加 Intent Query 元素。
https://developer.android.com/training/basics/intents/package-visibility#intent-signature
我正在尝试使用以下代码以编程方式呼叫号码:
String number = ("tel:" + numTxt.getText());
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(number));
startActivity(intent);
我在清单中设置了权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>
我正在使用真实设备进行测试和调试,它是带有 Android M 的 Nexus 5,我的 compileSdkVersion 是 23。我收到以下安全异常:
error: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{cbbd7c1 5228:com.dialerTest.DialerApp/u0a96} (pid=5228, uid=10096) with revoked permission android.permission.CALL_PHONE
我在网络和这个社区中搜索了类似的 Q/A,但找不到答案。任何帮助将不胜感激。
在 android 6.0(Api 23 级)中,我们有一个叫做 "Runtime Permissions" 的东西。你必须阅读它。
您可以找到文档 here。
权限CALL_PHONE属于危险权限组。
因此,如果您的应用目标 SDK 为 23 或更高版本,并且您的设备在 Android 6.0 或更高版本上为 运行,则您必须在应用为 运行 时请求 CALL_PHONE 权限。
示例:
String number = ("tel:" + numTxt.getText());
mIntent = new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(number));
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
// MY_PERMISSIONS_REQUEST_CALL_PHONE is an
// app-defined int constant. The callback method gets the
// result of the request.
} else {
//You already have permission
try {
startActivity(mIntent);
} catch(SecurityException e) {
e.printStackTrace();
}
}
当您的应用请求权限时,系统会向用户显示一个对话框。当用户响应时,系统会调用您应用的 onRequestPermissionsResult() 方法,将用户响应传递给它。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the phone call
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
您的代码只有在您发出 ACTION_DIAL
时才能工作,而不是 ACTION_CALL
需要请求许可的地方,因此如果您想拨打电话,请按照以下示例操作:
清单:
<uses-permission android:name="android.permission.CALL_PHONE" />
代码:
import static android.Manifest.permission.CALL_PHONE;
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:0612312312"));
/*
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:0612312312"));
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
}*/
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(i);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
片段class
第一步: 导入静态 android.Manifest.permission.CALL_PHONE;
步骤 2:Where 您的点击按钮:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" +driver_no ));
if (ContextCompat.checkSelfPermission(getActivity(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
[或者] 你在 activity 中使用 class 表示将 getActivity 更改为 getApplicationContext()
仅供参考:如果您的目标是 Android 11,则需要在清单中为 Intent.ACTION_DIAL 添加 Intent Query 元素。
https://developer.android.com/training/basics/intents/package-visibility#intent-signature