Evg*_*tin 19 android android-intent android-permissions
我正在尝试以编程方式使用以下代码调用数字:
String number = ("tel:" + numTxt.getText());
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(number));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
我在Manifest中设置了权限:
<uses-permission android:name="android.permission.CALL_PHONE"/>
Run Code Online (Sandbox Code Playgroud)
我正在使用真实设备进行测试和调试,它是带有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
Run Code Online (Sandbox Code Playgroud)
我在网络和这个社区搜索了类似的问答,但找不到答案.任何帮助将不胜感激.
Fai*_*l T 13
权限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();
}
}
Run Code Online (Sandbox Code Playgroud)
当您的应用请求权限时,系统会向用户显示一个对话框.当用户响应时,系统会调用您应用的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
}
}
Run Code Online (Sandbox Code Playgroud)
你的代码只有在你创建一个时才能工作ACTION_DIAL,而不是ACTION_CALL在你需要请求许可的地方,所以如果你想打电话,请按照这个例子:
清单:
<uses-permission android:name="android.permission.CALL_PHONE" />
Run Code Online (Sandbox Code Playgroud)
代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27551 次 |
| 最近记录: |