我正在尝试使用以下代码以编程方式呼叫一个号码:
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
我在网络和这个社区中搜索了类似的问答,但找不到答案。任何帮助将不胜感激。
权限CALL_PHONE属于危险权限组。
因此,如果您的应用程序的目标 SDK 为 23 或更高版本,并且您的设备运行在 Android 6.0 或更高版本上,则您必须在应用程序运行时请求 CALL_PHONE 权限。
您可以使用我复制的以下代码http://web.archive.org/web/20160111030837/http://developer.android.com/training/permissions/requesting.html http://web.archive.org/web/20160111030837/http://developer.android.com/training/permissions/requesting.html —
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();
}
} When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's
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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)