使用广播接收器的权限拒绝

我正在尝试创建一个应用程序,该应用程序在我拨打拨出电话时会输入日志消息。

但是,当我运行代码时,尽管我已输入权限,但我仍会收到权限拒绝。

拒绝日志:

"09-04 02:35:50.535 1294-1666/?W/BroadcastQueue: Permission Denial: receive Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver需要android.permission.PROCESS_OUTGOING_CALLS,因为发送者Android(uid 1000)”

清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="samples.varma.packagecom.testreceive2" >
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="23" />


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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name=".MainActivity"
            android:enabled="true"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver
            android:name=".CallReceiver">


            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>
    </application>


</manifest>

这是我的接收器的代码:

package samples.varma.packagecom.testreceive2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallReceiver extends BroadcastReceiver {
    public CallReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (state == null) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.i("TAG", "Outgoing Number: " + number);
        } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.i("TAG", "Incoming Number: " + number);
        }
    }
        }

我对此非常陌生,因此很有可能有几个错误,或者我完全偏离了基础。无论如何,我将不胜感激任何指导。有谁知道我为什么会得到这种否认?

谢谢

编辑:

它也给了我这些权限拒绝,即使我已经添加了电话状态权限。

特权电话状态权限是系统权限,因此我无法添加。

09-04 04:36:03.249    1294-1440/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000)
09-04 04:36:03.271    1294-1308/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)

 1294-1308/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)

答案 1

我已经在Android模拟器上启动了相同的应用程序,没有任何帮助,即使

android:enabled="true"
android:exported="true"

解决方案是转到设置 - >应用程序 - > MyApplication - >权限 - >切换电话权限打开。

安卓手机申请许可


答案 2

我通过密切关注此链接来让它工作 拦截传出呼叫 - 我错过了什么?(谢谢ajit)

我最终取消了权限,在清单中添加和添加到我的接收器,将权限重新定位到下面的应用程序(不确定这是否必要),删除预期的sdk版本,并基本上从链接中复制接收器。PHONE_STATEandroid:enabled="true"android:exported="true"NEW_OUTGOING_CALL

从接收器标记到清单标记的更新清单代码是:

    <receiver
            android:name=".testreceive3"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>

                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>-->
            </intent-filter>
        </receiver>
    </application>

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

 </manifest>

推荐