Search code examples
androidbroadcastreceiverintentfilterandroid-pendingintentproximity

Why is my Receiver not Receiving Broadcast?


I'm having difficulty using/understanding BroadcastReceivers and IntentFilters. I have the following code in my Test activity. Test activity contains a .addProximityAlert, if the .addProximityAlert is triggered I want to broadcast to the Test2 receiver. I get an error when I test this. What am I doing wrong?

Test Activity:

public class Test extends BroadcastReceiver
{   
    LocationManager lm;
    ... 
    @Override
    public void onReceive(Context context, Intent intent) 
    {   
        ... 
        final String PROX_ALERT_INTENT = "com.example.proxalert.Test2";
        Intent alert = new Intent(PROX_ALERT_INTENT);
        PendingIntent proximityIntent = PendingIntent.getBroadcast(context, 0, alert, 0);

        lm.addProximityAlert(latitude, longitude, radius, expiration, proximityIntent);

        IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
        context.registerReceiver(new Test2(), filter);

Test2 Receiver:

public class Test2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
    String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Boolean entering = arg1.getBooleanExtra(key, false);
        if (entering) {
        //toast notification "welcome"
    }
    ...

Solution

  • I was able to get it to work by registering the receiver in the android manifest, rather than trying to register the receiver dynamically in test1. this website really helped:

    http://www.techotopia.com/index.php/Android_Broadcast_Intents_and_Broadcast_Receivers