I have searched SO and even found a question with more or less the same title as my question. But still my code isn't working.
I try to send a position from my GPS service to my MainActivity using broadcast.
In my GPS service I do
Intent intent = new Intent(MainActivity.EXTRA_NEW_POS_FOR_PLOTTING_INTENT);
intent.putExtra(MainActivity.EXTRA_NEW_POS_FOR_PLOTTING, new LatLng(location.getLatitude(), location.getLongitude()));
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
On the receiving side in MainActivity.onCreate() i do:
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
LatLng pos = intent.getExtras().getParcelable(EXTRA_NEW_POS_FOR_PLOTTING);
newLocForPlotting(pos);
}
};
And in MainActivity.onStart() I have:
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter(EXTRA_NEW_POS_FOR_PLOTTING_INTENT ));
I know that my service calls sendBroadcast(), but my onReceive() is never called.
What is going wrong?
put this code into your activity onResume method.
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter(EXTRA_NEW_POS_FOR_PLOTTING_INTENT ));
You can also use Local broadcast like this
Intent intent = new Intent(BundleTags.BROADCAST_TAG_FOR_INCOMING_CALL_REQUEST);
// You can also include some extra data.
intent.putExtra(RESPONSE_MESSAGE, body);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
In activity
LocalBroadcastManager.getInstance(ServiceProviderActivity.this).registerReceiver(myReceiver,
new IntentFilter(BROADCAST_TAG_FOR_INCOMING_CALL_REQUEST));