1

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?

Community
  • 1
  • 1
wman
  • 21
  • 4
  • So what you mean is you receive a broadcast in your BroadcastReceiver but you don't get it inside your main activity ? – cafebabe1991 Feb 16 '17 at 08:53
  • @cafebabe1991 Basically yes. My GeofenceReceiver is still getting called for geofence transitions even if the app is killed, but then I can not broadcast something to my MainActivity. – wman Feb 16 '17 at 09:11

1 Answers1

0

Since the app has been killed it has to be started again. The app can be started using the context passed inside your BroadcastReceiver class. See the snippet below to send your activity the geoFence data.

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent();
    i.setClassName("com.test", "com.test.MainActivity");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("data", geoData); 
    context.startActivity(i)
 }
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
  • I tried it with Intent broadcastIntent = new Intent(context, MainActivity.class); broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); broadcastIntent.putExtra("resultCode", Activity.RESULT_OK); broadcastIntent.putExtra(Constants.BUNDLE_KEY, bundle); context.startActivity(broadcastIntent);But this means that my app is going to foreground. I don't want that. I just want to update a model to refresh the lastPosition Geofence. – wman Feb 16 '17 at 10:16
  • You will then have to start your service and your service will in turn should update the data. – cafebabe1991 Feb 16 '17 at 15:07