检查是否一个Android应用程序在后台运行 [英] Checking if an Android application is running in the background
问题描述
到后台,我的意思是没有的应用程序的活动是当前可见的用户?
有几种方法来检测后台应用程序是否正在运行,但其中只有一个是完全可靠的:
-
正确的解决方案(学分转到丹,的CommonsWare 和 NeTeInStEiN )
你的应用程序自己使用Activity.onPause
,Activity.onResume
方法跟踪的知名度。商店知名度的地位在其它的类。比较好的选择是自己执行应用
或服务
(还有的a一些变化此解决方案,如果你想从服务检查活动的可见性)。
 
示例的
实现自定义的应用程序
类(注意isActivityVisible()
静态方法):公共类MyApplication的扩展应用{ 公共静态布尔isActivityVisible(){ 返回activityVisible; } 公共静态无效activityResumed(){ activityVisible = TRUE; } 公共静态无效activityPaused(){ activityVisible = FALSE; } 私有静态布尔activityVisible; }
填写您的应用程序类的
的AndroidManifest.xml
:<应用 机器人:名称=your.app.package.MyApplication 机器人:图标=@可绘制/图标 机器人:标签=@字符串/ APP_NAME>
添加
的onPause
和onResume
每活动
的该项目(您可以创建一个共同的祖先为您的活动,如果你想,但如果你的活动是从MapActivity
/ListActivity <已经延伸/ code>等,你仍然需要手工编写以下):
@覆盖 保护无效onResume(){ super.onResume(); MyApplication.activityResumed(); } @覆盖 保护无效的onPause(){ super.onPause(); MyApplication.activityPaused(); }
&NBSP;
更新的
<一href="http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html">ActivityLifecycleCallbacks添加在API级别14(Android 4.0的)。你可以用它们来跟踪你的应用程序的活动是否是当前可见的用户。检查下面玉米秆的回答了解详情。 -
错了
我曾经提出了以下解决方案:您可以检测当前前景与<$c$c>ActivityManager.getRunningAppProcesses()$c$c>返回
RunningAppProcessInfo
记录的列表。要确定您的应用程序在前台检查RunningAppProcessInfo.importance
字段平等RunningAppProcessInfo.IMPORTANCE_FOREGROUND
,而RunningAppProcessInfo.processName
等于你的应用程序包的名称。另外,如果你调用
ActivityManager.getRunningAppProcesses()
从您的应用程序UI线程将返回的重要性IMPORTANCE_FOREGROUND
您任务无论它实际上是在前台与否。 (通过的AsyncTask
)后调用它在后台线程,它会返回正确的结果。虽然这种解决方案可能工作(和它确实工作的大部分时间)我强烈建议使用它来避免。这里的原因。 作为戴安娜Hackborn写道:
这些API是不存在的应用程序,以立足于自己的用户界面流,而是做事情像显示用户运行的应用程序或任务管理器,或此类。
是有保存在内存中为这些事情的清单。然而,关在另一个进程中,从你单独运行的线程,而不是你可以在时间,使被返回的时间正确的决定,或(b)有一个一致的画面(A)上看到数管理。加什么下一个活动去进行时,总是在点开关的情况发生,这是不是直到确切点(其中的活动状态进行了简要锁定做开关)的决定,我们实际上肯定知道是什么,接下来的事情会。
和此处实施和全局行为是不能保证在未来保持相同。
我希望我读这之前,我贴在SO答案,但希望这不是为时已晚承认我的错误。
-
另一种错误的解决方法
<一href="http://brainflush.word$p$pss.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/">Droid-Fu在ActivityManager.getRunningTasks
其isApplicationBroughtToBackground
方法的一个答案用途库提。见戴安娜的评论上面,不要使用该方法无论是。
By background, I mean none of the application's activities are currently visible to the user?
There are few ways to detect whether your application is running in the background, but only one of them is completely reliable:
The right solution (credits go to Dan, CommonsWare and NeTeInStEiN)
Track visibility of your application by yourself usingActivity.onPause
,Activity.onResume
methods. Store "visibility" status in some other class. Good choices are your own implementation of theApplication
or aService
(there are also a few variations of this solution if you'd like to check activity visibility from the service).
Example
Implement customApplication
class (note theisActivityVisible()
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 your application class in
AndroidManifest.xml
:<application android:name="your.app.package.MyApplication" android:icon="@drawable/icon" android:label="@string/app_name" >
Add
onPause
andonResume
to everyActivity
in the project (you may create a common ancestor for your Activities if you'd like to, but if your activity is already extended fromMapActivity
/ListActivity
etc. you still need to write the following by hand):@Override protected void onResume() { super.onResume(); MyApplication.activityResumed(); } @Override protected void onPause() { super.onPause(); MyApplication.activityPaused(); }
Update
ActivityLifecycleCallbacks were added in API level 14 (Android 4.0). You can use them to track whether an activity of your application is currently visible to the user. Check Cornstalks' answer below for the details.The wrong one
I used to suggest the following solution:You can detect currently foreground/background application with
ActivityManager.getRunningAppProcesses()
which returns a list ofRunningAppProcessInfo
records. To determine if your application is on the foreground checkRunningAppProcessInfo.importance
field for equality toRunningAppProcessInfo.IMPORTANCE_FOREGROUND
whileRunningAppProcessInfo.processName
is equal to your application package name.Also if you call
ActivityManager.getRunningAppProcesses()
from your application UI thread it will return importanceIMPORTANCE_FOREGROUND
for your task no matter whether it is actually in the foreground or not. Call it in the background thread (for example viaAsyncTask
) and it will return correct results.While this solution may work (and it indeed works most of the time) I strongly recommend to refrain from using it. And here's why. As Dianne Hackborn wrote:
These APIs are not there for applications to base their UI flow on, but to do things like show the user the running apps, or a task manager, or such.
Yes there is a list kept in memory for these things. However, it is off in another process, managed by threads running separately from yours, and not something you can count on (a) seeing in time to make the correct decision or (b) have a consistent picture by the time you return. Plus the decision about what the "next" activity to go to is always done at the point where the switch is to happen, and it is not until that exact point (where the activity state is briefly locked down to do the switch) that we actually know for sure what the next thing will be.
And the implementation and global behavior here is not guaranteed to remain the same in the future.
I wish I had read this before I posted an answer on the SO, but hopefully it's not too late to admit my error.
Another wrong solution
Droid-Fu library mentioned in one of the answers usesActivityManager.getRunningTasks
for itsisApplicationBroughtToBackground
method. See Dianne's comment above and don't use that method either.
这篇关于检查是否一个Android应用程序在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!