Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to determine if one of my activities is in the foreground
    primarykey
    data
    text
    <p>I have implemented a BroadcastReceiver which is triggered by the AlarmManager. The AlarmManager is initialized on BOOT_COMPLETED. So i have to declare the receiver in the manifest.</p> <p>My problem is that i want the BroadcastReceiver only to do something when none of my own activities are in the foreground (aka the user is not interacting with my application). I pull information from a remote server and don't want to notify the user if he is currently in my application anyways.</p> <p>So far i have not managed to find a way to determine if my application is in the foreground. Is there a way to do such thing? The ActivityManager tells me if my application is running but not whether it is in the foreground. </p> <p>The problem is pretty much the same as described here: <a href="https://stackoverflow.com/questions/2282435/inform-activity-from-a-broadcastreceiver-only-if-it-is-in-the-foreground">Inform Activity from a BroadcastReceiver ONLY if it is in the foreground</a></p> <p><strong>SOLUTION:</strong></p> <p>After evaluating several solutions i want to quickly outline what i think is the best method to deal with activities in the background/foreground.</p> <p>The preferred way is to register a broadcast receiver in the onResume method of your activity and to deregister it on the activities on onPause. Any service or other background element will than need to send a broadcast intent with a specific action that your activity will intercept.</p> <p>If your activity is in the foreground it will have its intent receiver registered and is able to directly deal with the intent send from your service. If it is not in the foreground it will not receive the intent but the service that invokved the broadcast will know that nobody intercepted its broadcast intent and will be able to deal with that itself. Eg it could than launch the desired activity, show a notification etc.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload