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:
the right solution (credits go dan, commonsware , neteinstein)
track visibility of application usingactivity.onpause
,activity.onresume
methods. store "visibility" status in other class. choices own implementation ofapplication
orservice
(there a few variations of solution if you'd check activity visibility service).
example
implement customapplication
class (noteisactivityvisible()
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
everyactivity
in project (you may create common ancestor activities if you'd to, if activity extendedmapactivity
/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.the wrong one
used suggest following solution:you can detect foreground/background application
activitymanager.getrunningappprocesses()
returns list ofrunningappprocessinfo
records. determine if application on foreground checkrunningappprocessinfo.importance
field equalityrunningappprocessinfo.importance_foreground
whilerunningappprocessinfo.processname
equal application package name.also if call
activitymanager.getrunningappprocesses()
application ui thread return importanceimportance_foreground
task no matter whether in foreground or not. call in background thread (for example viaasynctask
) , 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.
another wrong solution
droid-fu library mentioned in 1 of answers usesactivitymanager.getrunningtasks
isapplicationbroughttobackground
method. see dianne's comment above , don't use method either.
Comments
Post a Comment