BroadcastReceiver not receiving intent

左心房为你撑大大i 提交于 2019-12-30 11:05:40

问题


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.



来源:https://stackoverflow.com/questions/1910339/broadcastreceiver-not-receiving-intent

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