拒绝广播接收器的权限

Permission Denial With Broadcast Receiver

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

然而,当我 运行 代码时,我得到了权限拒绝,尽管我已经输入了权限。

拒绝记录:

"09-04 02:35:50.535 1294-1666/? W/BroadcastQueue﹕ 权限被拒绝:接收 Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010 (有额外内容) } 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);
        }
    }
        }

我对此很陌生,所以很可能存在一些错误,或者我完全偏离了基础。无论如何,我将不胜感激任何指导。有谁知道我为什么会被拒绝?

谢谢

编辑:

即使我添加了 phone 状态权限,它也会给我这些权限拒绝。

特权phone状态权限是系统权限,所以我不能添加。

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)

您应该在您的 Manifest 接收器中添加以下内容

 <service android:name=".CallReceiver"
            android:enabled="true"
            android:exported="false" >
        </service>

使用权限 CALL_PHONE 而不是 OUTGOING

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

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

我通过密切关注 link 来实现它 Intercepting outgoing call - what am I missing?(感谢 ajit)

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

从接收者标签到清单标签的更新清单代码是:

    <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>

我在 Android 模拟器上启动了相同的应用程序,但没有任何帮助,甚至

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

解决方案是转到“设置”->“应用程序”->“我的应用程序”->“权限”->“打开Phone权限。

Android Phone Permission for Application

当您的代码 运行 在 Android 6.0 或更高版本上时,您需要在代码中应用运行时权限。

最佳解决方案是当您运行代码激活 USB 调试时,确保您select在开发者设置中禁用权限监控设置! OS 不会再向应用程序请求权限。乐于助人:)

这将在不更改清单中的任何内容的情况下工作!