Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast receiver onReceive() never called - Android

I've been testing sending and receiving broadcasts in my application but I have a problem. The service starts and I get the Toast to say the broadcast has been sent but onReceive is never called in the myMapView class. I've only included the code I think is relevant below... any help would be much appreciated. I don't think I'm registering the receiver correctly.

Thanks in advance.

public class myLocationService extends Service implements LocationListener {
    private static final String GEO_LNG = "GEO_LNG";
    private static final String GEO_LAT = "GEO_LAT";

    private void updateWithNewLocation(Location location){


    if (location != null){

            Double geoLat = location.getLatitude() * 1E6;
            Double geoLng = location.getLongitude() * 1E6;

            Intent intent = new Intent(this, myMapView.class);
            intent.putExtra(GEO_LNG,geoLng);
            intent.putExtra(GEO_LAT, geoLat);
            sendBroadcast(intent);
            Toast.makeText(myLocationService.this, "Broadcast sent", Toast.LENGTH_SHORT).show();
        }   
    else{
        Toast.makeText(myLocationService.this, "Location = null", Toast.LENGTH_SHORT).show();
    }

}
}


public class myMapView extends MapActivity{
    private static final String GEO_LNG = "GEO_LNG";
    private static final String GEO_LAT = "GEO_LAT";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);     

    IntentFilter filter = new IntentFilter();
    filter.addAction(GEO_LONG);
    filter.addAction(GEO_LAT);
    registerReceiver(locationRec, filter);

    Intent i = new Intent(this, myLocationService.class);

            startService(i);
}

    private BroadcastReceiver locationRec = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
                       double geoLng = intent.getExtras().getDouble(GEO_LONG);
                       double geoLat = intent.getExtras().getDouble(GEO_LAT);
            Toast.makeText(GeoTrailsMap.this, "Got broadcast", Toast.LENGTH_LONG).show();


        }
    };

Edit I've modified my code to look like this, but I'm still not having any luck with receiving the broadcasts.

public class myLocationService extends Service implements LocationListener {
private static final String GEO_LNG = "GEO_LNG";
private static final String GEO_LAT = "GEO_LAT";
public static final String LOCATION_UPDATE = "LOCATION_UPDATE";

private void updateWithNewLocation(Location location){


if (location != null){

        Double geoLat = location.getLatitude() * 1E6;
        Double geoLng = location.getLongitude() * 1E6;

        Intent intent = new Intent(this, myMapView.class);
        intent.putExtra(GEO_LNG,geoLng);
        intent.putExtra(GEO_LAT, geoLat);
        intent.setAction(LOCATION_UPDATE);
        sendBroadcast(intent);
        Toast.makeText(myLocationService.this, "Broadcast sent",    Toast.LENGTH_SHORT).show();
    }   
    else{
    Toast.makeText(myLocationService.this, "Location = null", Toast.LENGTH_SHORT).show();
    }

}
}


public class myMapView extends MapActivity{
private static final String GEO_LNG = "GEO_LNG";
private static final String GEO_LAT = "GEO_LAT";
private static final String LOCATION_UPDATE = myLocationService.LOCATION_UPDATE;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);     

    IntentFilter filter = new IntentFilter();
    filter.addAction(LOCATION_UPDATE);
    registerReceiver(locationRec, filter);

    Intent i = new Intent(this, myLocationService.class);

    startService(i);
}

private BroadcastReceiver locationRec = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
                   double geoLng = intent.getExtras().getDouble(GEO_LNG);
                   double geoLat = intent.getExtras().getDouble(GEO_LAT);
        Toast.makeText(GeoTrailsMap.this, "Got broadcast", Toast.LENGTH_LONG).show();


    }
};
like image 966
siu07 Avatar asked Jan 29 '26 10:01

siu07


1 Answers

The intent that's firing doesn't have an action set. When you call "addAction" in the IntentFilter here:

    IntentFilter filter = new IntentFilter();
    filter.addAction(GEO_LNG);
    filter.addAction(GEO_LAT);
    registerReceiver(locationRec, filter);

You're adding actions that the receiver will listen for (in this case, the receiver will listen for GEO_LNG and GEO_LAT.

In the service where you fire the intent, the intent needs to contain that action in order for the receiver to spot it. Decide which one you want to send, and then modify the Intent-firing code to look like this:

        Intent intent = new Intent(this, myMapView.class);
        // Here you're using GEO_LNG as both the Intent action, and a label 
        // for data being passed over.  Might want to change that for clarity?
        intent.putExtra(GEO_LNG, geoLng);
        intent.putExtra(GEO_LAT, geoLat);
        intent.setAction(GEO_LNG);
        ...
like image 87
Alexander Lucas Avatar answered Jan 31 '26 23:01

Alexander Lucas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!