I am working on a geofence app for Android. With the help of the example from google and refactoring from IntentService
to BroadcastReceiver
as described here I now have an app that works as expected if app is in background. What I did not manage is how I can update my App e.g. a Model if the app is killed? My GeofenceReceiver
is triggered/called while the app is killed, but my BroadcastReceiver
message is not received in my MainActivity
.
In my GeofenceReceiver
I do something like this if I get a geofence-transition:
1:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putStringArrayListExtra(Constants.TRIGGERING_GEOFENCE_IDS_KEY, triggeringIDs);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setColor(Color.RED)
.setContentTitle(notificationDetails)
.setContentText(context.getString(R.string.geofence_transition_notification_text))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(resultPendingIntent);
builder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
2:
Intent broadcastIntent = new Intent(ACTION);
broadcastIntent.putExtra("resultCode", Activity.RESULT_OK);
broadcastIntent.putExtra(Constants.BUNDLE_KEY, bundle);
LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent);
As I already said the broadcast is received in my MainActivity
, if the app is in background. If I kill the App the broadcast is not received in my MainActivity
. I checked that with terminal and logcat.
Any ideas?