4.判断app 进入前台或退入后台
2016-08-03 本文已影响72人
gadfly_only
1.api14之前靠定时器手动监测
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// 4.0 之前的系统,每 2s 在后台检测一次当前是否在前台
mSubscriptionCheckOldSys = Observable.interval(2, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
if (AppUtils.isRunningForeground2(IMEnvironment.getInstance().getContext())) {
checkServiceAlive();
} else {
stopLink();
}
}
});
}
/*
* 判断程序是否在前台运行
* 存在问题,如果程序有一个前台运行的service则会始终返回true
*/
public static boolean isRunningForeground2(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
Log.d("background", appProcess.processName);
return false;
} else {
Log.i("foreground", appProcess.processName);
return true;
}
}
}
return false;
}
2.在api14之后用ActivityLifecycleCallbacks
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class BJApplicationStateCallbacks implements ActivityLifecycleCallbacks {
public static interface BJApplicationStateListener {
public void applicationDidEnterForeground();
public void applicationDidEnterBackground();
}
private int mForegroundActivities;
private boolean hasSeenFirstActivity;
// private boolean isChangingConfiguration;
private BJApplicationStateListener mListener = null;
public BJApplicationStateCallbacks(BJApplicationStateListener listener) {
mListener = listener;
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
// TODO Auto-generated method stub
}
@Override
public void onActivityStarted(Activity activity) {
mForegroundActivities++;
if (hasSeenFirstActivity && mForegroundActivities == 1/* && !isChangingConfiguration*/) {
if (mListener != null) {
mListener.applicationDidEnterForeground();
}
}
hasSeenFirstActivity = true;
// isChangingConfiguration = false;
}
@Override
public void onActivityResumed(Activity activity) {
// TODO Auto-generated method stub
}
@Override
public void onActivityPaused(Activity activity) {
// TODO Auto-generated method stub
}
@Override
public void onActivityStopped(Activity activity) {
mForegroundActivities--;
if (mForegroundActivities == 0) {
if (mListener != null) {
mListener.applicationDidEnterBackground();
}
}
// isChangingConfiguration = activity.isChangingConfigurations();
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
// TODO Auto-generated method stub
}
@Override
public void onActivityDestroyed(Activity activity) {
// TODO Auto-generated method stub
}
}
//demo
/**
* 监听 APP 进入前后台的状态。只在 4.0+ 有效
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void registerActivityLifeCycleCallback() {
((Application) IMEnvironment.getInstance().getContext())
.registerActivityLifecycleCallbacks(new BJApplicationStateCallbacks(
new BJApplicationStateCallbacks.BJApplicationStateListener() {
@Override
public void applicationDidEnterForeground() {
mIMServiceV2.applicationDidEnterForeground();
}
@Override
public void applicationDidEnterBackground() {
mIMServiceV2.applicationDidEnterBackground();
}
}));
}