就是让app中所有的activity继承与一个公共的activity(例如:BaseActivity),然后在BaseActivity的onStop()中判断当前程序是否处于后台
代码片段
[java] view plaincopyprint
01/
02 文件名:BaseActivityjava
03 版本号:
04 日期:2012-6-20
05 创建人:
06 Copyright wadata 版权所有
07 变更:
08 /
09
10package comwadatamobilefollowupviewbase;
11
12import javautilList;
13import androidappActivity;
14import androidappActivityManager;
15import androidappActivityManagerRunningAppProcessInfo;
16import androidcontentContext;
17
18/
19 名称:BaseActivity
20 描述:
21 创建人:
22 日期:2012-6-20 下午5:53:35
23 变更:
24 /
25
26public class BaseActivity extends Activity {
27 @Override
28 protected void onStop() {
29 // TODO Auto-generated method stub
30 superonStop();
31
32 if (!isAppOnForeground()) {
33 //app 进入后台
34
35 //全局变量isActive = false 记录当前已经进入后台
36 }
37 }
38
39 @Override
40 protected void onResume() {
41 // TODO Auto-generated method stub
42 superonResume();
43
44
45 //if (!isActive) {
46 //app 从后台唤醒,进入前台
47
48 //isActive = true;
49 //}
50 }
51
52 /
53 程序是否在前台运行
54
55 @return
56 /
57 public boolean isAppOnForeground() {
58 // Returns a list of application processes that are running on the
59 // device
60
61 ActivityManager activityManager = (ActivityManager) getApplicationContext()getSystemService(ContextACTIVITY_SERVICE);
62 String packageName = getApplicationContext()getPackageName();
63
64 List<RunningAppProcessInfo> appProcesses = activityManager
65 getRunningAppProcesses();
66 if (appProcesses == null)
67 return false;
68
69 for (RunningAppProcessInfo appProcess : appProcesses) {
70 // The name of the process that this object is associated with
71 if (appProcessprocessNameequals(packageName)
72 && appProcessimportance == RunningAppProcessInfoIMPORTANCE_FOREGROUND) {
73 return true;
74 }
75 }
76
77 return false;
78 }
79}
以上就是关于如何监听android程序进入后台,和恢全部的内容,包括:如何监听android程序进入后台,和恢、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)