Checking if an Android application is running in the background -


by background, mean none of application's activities visible user?

there few ways detect whether application running in background, 1 of them reliable:

  1. the right solution (credits go dan, commonsware , neteinstein)
    track visibility of application using activity.onpause, activity.onresume methods. store "visibility" status in other class. choices own implementation of application or service (there a few variations of solution if you'd check activity visibility service).
     
    example
    implement custom application class (note isactivityvisible() static method):

    public class myapplication extends application {    public static boolean isactivityvisible() {     return activityvisible;   }      public static void activityresumed() {     activityvisible = true;   }    public static void activitypaused() {     activityvisible = false;   }    private static boolean activityvisible; } 

    register application class in androidmanifest.xml:

    <application     android:name="your.app.package.myapplication"     android:icon="@drawable/icon"     android:label="@string/app_name" > 

    add onpause , onresume every activity in project (you may create common ancestor activities if you'd to, if activity extended mapactivity/listactivity etc. still need write following hand):

    @override protected void onresume() {   super.onresume();   myapplication.activityresumed(); }  @override protected void onpause() {   super.onpause();   myapplication.activitypaused(); } 

     
    update
    activitylifecyclecallbacks added in api level 14 (android 4.0). can use them track whether activity of application visible user. check cornstalks' answer below details.

  2. the wrong one
    used suggest following solution:

    you can detect foreground/background application activitymanager.getrunningappprocesses() returns list of runningappprocessinfo records. determine if application on foreground check runningappprocessinfo.importance field equality runningappprocessinfo.importance_foreground while runningappprocessinfo.processname equal application package name.

    also if call activitymanager.getrunningappprocesses() application ui thread return importance importance_foreground task no matter whether in foreground or not. call in background thread (for example via asynctask) , return correct results.

    while solution may work (and indeed works of time) recommend refrain using it. , here's why. as dianne hackborn wrote:

    these apis not there applications base ui flow on, things show user running apps, or task manager, or such.

    yes there list kept in memory these things. however, off in process, managed threads running separately yours, , not can count on (a) seeing in time make correct decision or (b) have consistent picture time return. plus decision "next" activity go done @ point switch happen, , not until exact point (where activity state briefly locked down switch) know sure next thing be.

    and implementation , global behavior here not guaranteed remain same in future.

    i wish had read before posted answer on so, it's not late admit error.

  3. another wrong solution
    droid-fu library mentioned in 1 of answers uses activitymanager.getrunningtasks isapplicationbroughttobackground method. see dianne's comment above , don't use method either.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -