Permission Denial With Broadcast Receiver

2020-06-12 03:09发布

问题:

I am trying to create an app which enters a log message when I make an outgoing call.

However, when I run the code, I get a permission denial, despite the fact that I have entered in the permissions.

Denial Log:

"09-04 02:35:50.535 1294-1666/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.PROCESS_OUTGOING_CALLS due to sender android (uid 1000)"

Manifest Code:

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

And here is the code for my receiver:

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);
        }
    }
        }

I am very new to this so there is a good chance that there are several errors or I am completely off base. Regardless I would greatly appreciate any guidance. Would anyone know why I am getting this denial?

Thanks

Edit:

It is also giving me these permission denials even though I have added the phone state permission.

The privileged phone-state permission is a system permission so I cannot add.

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:

I have launched the same application on Android emulator and nothing helped, even

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

The solution was to go to Settings->Apps -> MyApplication -> Permissions -> Toggle Phone Permission on.

Android Phone Permission for Application



回答2:

I got it to work by following this link closely Intercepting outgoing call - what am I missing? (thanks ajit)

I ended up taking off the PHONE_STATE permission, adding android:enabled="true" and android:exported="true" to my receiver in the manifest, relocating the NEW_OUTGOING_CALL permission to below application(not sure if this is necessary), taking away the intended sdk versions and basically copying the receiver from the link.

Updated manifest code from receiver tag to manifest tag is:

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


回答3:

You should add the following thing in your Manifest receiver

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


回答4:

Use permission CALL_PHONE instead of OUTGOING

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

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


回答5:

U need to apply the runtime permission int your code when your code running on Android 6.0 or above.



回答6:

Best solution is when you run the code activate usb debugging, make sure you select disable permissions monitor settings in developer settings ! App wont be asked for permissions by the OS anymore. Happy helping :)

This will work without changing anything in manifest!