Some of the answers were incomplete, so here is a complete solution of what I was looking for.
First of all, set up the MyApplication class, which implements ActivityLifecycleCallbacks :
public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks { private static boolean isActive; @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(this); } public static boolean isActivityVisible(){ return isActive; } @Override public void onActivityResumed(Activity activity) { isActive = true; } @Override public void onActivityPaused(Activity activity) { isActive = false; } ... no other methods need to be used, but there are more that ... must be included for the ActivityLifecycleCallbacks }
Be sure to indicate this in your manifest (only a line with a name is added, by default - by default):
<application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:hardwareAccelerated="true">
What was done above is used to track the life cycle of your application. You can use this to check if your application is currently in the foreground or not.
Next, configure BroadcastReceiver , wherever you want to run the code (in the case of opening the application when a trigger occurs). In this case, it is in my MainActivity :
protected BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ... Do whatever you want here Toast.makeText(...).show(); } };
Register the receiver in your onCreate the same action:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... LocalBroadcastManager.getInstance(this).registerReceiver(mNotificationReceiver, new IntentFilter("some_custom_id")); }
And do not forget to unregister:
@Override protected void onDestroy() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mNotificationReceiver); super.onDestroy(); }
Upon receipt of the broadcast, code is executed inside the receiver.
Now, to check if the application is in the foreground and send a broadcast, if so. Inside the IntentService :
@Override protected void onHandleIntent(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { String errorMessage = getErrorString(this, geofencingEvent.getErrorCode()); return; } int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { ... if(MyApplication.isActivityVisible()){ Intent intnt = new Intent("some_custom_id"); intnt.putExtra("message", geofenceTransitionDetails); LocalBroadcastManager.getInstance(this).sendBroadcast(intnt); }else{ sendNotification(geofenceTransitionDetails); } } else { // Log the error. } }
An important bit is the last nested if statement:
if(MyApplication.isActivityVisible()){ Intent intnt = new Intent("some_custom_id"); intnt.putExtra("message", geofenceTransitionDetails); LocalBroadcastManager.getInstance(this).sendBroadcast(intnt); }else{ sendNotification(geofenceTransitionDetails); }
Check to see if the application is in the foreground using MyApplication.isActivityVisible() as above, and then either sends a notification or sends a broadcast. Just make sure your intent code (ie "some_custom_id" ) matches your sender and recipient.
And about that. If the application is in the foreground (in particular, MainActivity), I am executing some code. If the application is not in the foreground, I send a notification.