方法一:app 开发中,经常可能会涉及到判断app 是否运行在后台。比如需要在后台启动运行Service, 或者需要从后台切换回前台时,做一些update 操作的等等。这些都需要我们能够监测到app 运行到后台。
最开始的时候我们采用的是监听系统的广播来实现,主要是监听Intent.ACTION_CLOSE_SYSTEM_DIALOGS 和 Intent.ACTION_SCREEN_OFF 这两个系统广播,监听的代码放在我们app 的 RootActivity (启动方式为 singleTask )里面。
Receiver 的大概代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
final String SYSTEM_DIALOG_REASON_LOCK_KEY = "lock";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null && (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)
|| reason.equals(SYSTEM_DIALOG_REASON_LOCK_KEY))) {
setAppGoBackground(true);
}
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
setAppGoBackground(true);
}
}
}
|
这种方式虽然能够监听到用户按HOME键以及锁屏使app切换到后台的动作。但是总觉得这种方式不够优雅。
其实 android 在从sdk14 开始为我们提供了一个新的api 用于检测app 中activity 的生命周期,正好我们app 的minSdkVersion 已经设置为 14 了。 所以现在我们可以使用ActivityLifecycleCallbacks来 监听app 是否运行在后台的行为了。
1
2
3
4
5
6
7
8
9
|
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
class ActivityLifecycleListener implements ActivityLifecycleCallbacks{
private int refCount = 0;
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
refCount++;
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
refCount--;
if(refCount == 0){
setAppGoBackGround(true);
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
|
1
|
registerActivityLifecycleCallbacks(new ActivityLifecycleListener());
|
是不是超级EASY.
原文:http://www.jianshu.com/p/0879b6b6d952
方法2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
/*
BACKGROUND=400 EMPTY=500 FOREGROUND=100
GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
*/
Log.i(context.getPackageName(), "此appimportace ="
+ appProcess.importance
+ ",context.getClass().getName()="
+ context.getClass().getName());
if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
Log.i(context.getPackageName(), "处于后台"
+ appProcess.processName);
return true;
} else {
Log.i(context.getPackageName(), "处于前台"
+ appProcess.processName);
return false;
}
}
}
return false;
}
|