权限拒绝 - 需要被授予的android.permission.CALL_PHONE

时间:2014-05-06 18:08:34

标签: android android-permissions

我正在使用Notification的操作发起呼叫:

Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:+48123456"));
PendingIntent pCallIntent = PendingIntent.getActivity(context, 0, callIntent, 0);

Notification notification = new Notification.Builder(context)
        .addAction(android.R.drawable.ic_menu_call, "Call", pCallIntent)
        .build();

notificationManager.notify(0, notification);

触发Call按钮后,我得到SecurityException

05-06 20:00:09.275    3426-6293/system_process W/ActivityManager﹕ Unable to send startActivity intent
    java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxxxx flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster bnds=[128,231][423,327] } from null (pid=-1, uid=10142) requires android.permission.CALL_PHONE
            at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1191)
            at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:746)
            at com.android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.java:3391)
            at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:252)
            at com.android.server.am.ActivityManagerService.startActivityIntentSender(ActivityManagerService.java:3272)
            at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:237)
            at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2146)
            at android.os.Binder.execTransact(Binder.java:404)
            at dalvik.system.NativeStart.run(Native Method)

我在AndroidManifest中设置了适当的权限:

<uses-permission android:name="android.permission.CALL_PHONE" />

我该怎么办才能修复它?

1 个答案:

答案 0 :(得分:0)

PendingIntent应该包含创建它的“安全上下文”,所以我有点惊讶这不起作用,虽然我当然从未尝试过拨打电话号码Notification

您可能的选择是:

  • 切换到DIAL_PHONE,因为这不需要许可。

  • PendingIntent路由到您的BroadcastReceiver,然后转身并拨打电话号码。确保这是一个未导出的BroadcastReceiver(即,没有<intent-filter>),因此其他应用程序不会向您发送广播,导致您在可能不合时宜的时刻拨打该号码。