BroadcastReceiver not receiving intent

2020-03-30 02:51发布

问题:

So now I have my BroastcastReceiver declared in the manifest file...

   <receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.CALL_BUTTON" />
    </intent-filter>
   </receiver>

I want to catch the intent when the Call button is pressed.

Here is my code...

 public class MyReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

         Toast.makeText(context, "intent received", Toast.LENGTH_LONG);

         if(intent.getAction().equals("android.intent.action.CALL_BUTTON")) {
             Toast.makeText(context, "call button pressed", Toast.LENGTH_LONG);
         }

     }

 }

However, I don't see the toast when I hit the call button. Did I miss something?

This is a continuation using an answer from this question...

How to use Intents from a Service or Broadcast Receiver?

回答1:

The short answer is that you can't do what you're trying to do.

The 'ACTION_CALL_BUTTON' action is an "Activity starting action" rather than a "Broadcast action". It can be used in an Intent used in startActivity to launch an Activity that should respond to the call button being pressed. What you want is to be notified when the call button is pressed, and the system doesn't broadcast an Intent to announce that.

Alternatively, you could include the same intent-filter on an Activity to have it come up as an option for the user to select when they press the call button.

What are you hoping to do when the user presses the call button?



回答2:

Per the docs, ACTION_CALL_BUTTON is not a broadcast action; rather, it's an activity action.

See Dianne's message about this in another thread.