Android receiver ignore NEW_OUTGOING_CALL

独自空忆成欢 提交于 2021-02-10 14:14:24

问题


I have a with Broadcastreceiver with an intent-filter to catch android.intent.action.NEW_OUTGOING_CALL.

I read many tutorials and answer here about handling NEW_OUTGOING_CALL intent, but I was unable to make this thing works. My goal is to log intent android.intent.action.NEW_OUTGOING_CALL received by my Broadcastreceiver. I'm unable to make this simple thing works.

Here's my code:

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.vannus.broadcasttest">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

This is the code for the Broadcastreceiver (TestReceiver.java)

package net.vannus.broadcasttest;

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

public class TestReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.w("###TEST###",intent.getAction());
    }
}

The project also contains an empty MainActivity with no functionality. Executing the project the main activity is launch, but no log are written when I try to make or receive calls. I tested the code in the emulator (Android 7) and on a Motorola G4 phone (Android 6.0), but nothing was logged on logcat. I'm using Android Studio 2.3

What am I doing wrong?

Thanks

Vannus


回答1:


The solution is very simple.... on the phone go to settings-> apps select application and give phone permission. The clue came when searching the logcat I found this error

03-05 20:18:57.090 1547-1775/system_process W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010 (has extras) } to net.vannus.broadcasttest/.TestReceiver requires android.permission.PROCESS_OUTGOING_CALLS due to sender android (uid 1000).




回答2:


in my case, app had the permissions already, but for some reason when i declared

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

after android.intent.action.NEW_OUTGOING_CALL, the receiver started sending out the new outgoing call event. so the declaration looks like this :

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



回答3:


From android Oreo the problem is that for "dangerous" type permissions (of which PROCESS_OUTGOING_CALLS" is one), it is not enough to declare then in the manifest. You need to specifically prompt the user to grant this permission. This can be done using the framework as discussed here:

https://developer.android.com/training/permissions/requesting

Specifically you need to basically call "requestPermissions" with an array of the permissions you actually need and it will ask the user to approve them.

Personally I find it quite annoying but I get the rationale behind it.




回答4:


Check for permission run time

 @TargetApi(Build.VERSION_CODES.M)
public static boolean hasSelfPermission(Activity activity, String[] permissions) {
    // Below Android M all permissions are granted at install time and are already available.
    if (!isMNC()) {
        return true;
    }

    // Verify that all required permissions have been granted
    for (String permission : permissions) {
        if (activity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

and use this method to check permission

@TargetApi(Build.VERSION_CODES.M)
private boolean isReadStatePermission() {
    if (hasSelfPermission((Activity) context, READ_PHONE_STATE)) {
        return true;
    } else {
        if (((Activity) context).shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)) {
            ((Activity) context).requestPermissions(READ_PHONE_STATE, REQUEST_WRITE_STORAGE);
        } else {
            ((Activity) context).requestPermissions(READ_PHONE_STATE, REQUEST_WRITE_STORAGE);
        }
        return false;
    }

perform any operation after checking the permission

  if (isReadStatePermission()) {
        PERFROM YOUR OPERATION
    }


来源:https://stackoverflow.com/questions/42611371/android-receiver-ignore-new-outgoing-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!