views:

1034

answers:

2

Hi,

As a simple test I'm trying to have a broadcastReceiver that will notify the user with a Toast message that an hardware button was clicked in android.

I've created a receiver tag in my manifest file with a name .TestReceiver that listens to Action_CALL_BUTTON to be informed when the user clicks on the green SEND button:

<receiver android:name=".TestReceiver" >
    <intent-filter>             
        <action android:name="android.intent.action.ACTION_CALL_BUTTON" />
    </intent-filter>            
</receiver>

Next, I created the TestReceiver class that extends BroadcastReceiver and creates a Toast. When I click on the call button I don't see the Toast message but I tested it on an activity and it shows. What am I missing here?

package com.example.helloandroid;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class TestReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
     // TODO Auto-generated method stub

     CharSequence text = "Hello toast!";
     int duration = Toast.LENGTH_SHORT;

     Toast toast = Toast.makeText(context, text, duration);
     toast.show();

    }

}

Thanks

+1  A: 

Here are some reasons why any BroadcastReceiver might fail to receive:

  1. In your specific case, ACTION_CALL_BUTTON isn't a broadcast Intent action. It is an activity Intent action, which will not be intercepted by BroadcastReceivers. You can tell this via the "Activity Action" statement in the documentation for this particular action.
  2. The broadcast Intent might not be eligible for manifest-registered receivers. ACTION_BATTERY_STATUS is one such action. These can only be received by receivers registered in Java via registerReceiver(). Unfortunately, which follow this rule are only sporadically documented.
  3. You may need a special permission to receive an Intent. For example, ACTION_BOOT_COMPLETED only works if you hold the RECEIVE_BOOT_COMPLETED permission. These are usually mentioned in the documentation for that action.
CommonsWare
A: 

So I can register to the ACTION_CALL_BUTTON using registerReceiver()? I didn't see any permission required for that.

pablo
As I wrote: "In your specific case, ACTION_CALL_BUTTON isn't a broadcast Intent action. It is an activity Intent action, which will not be intercepted by BroadcastReceivers. You can tell this via the "Activity Action" statement in the documentation for this particular action."
CommonsWare