简体   繁体   中英

BroadcastReceiver not receiving intent

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?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM