在開發中,你是不是沒有抽象一個出常用的類,那你可能要為你的懶惰付出很大的代價。要時刻記得自己的工具箱,不斷往裡面新增一些小小玩意。今天就給大家帶來一個很有意思的例子。前後臺執行!!

在Android開發中為了使用的方便要把所有的Activity包裹一層形成自己的activity,比如這個activity在建立時加入到一個容器裡,在ondestroy時及時清除,可以通過Application管理activity,這個是今天我要介紹的專案背景。

好了,我就直接開始切入題目。

前臺,就是當前顯示執行的是自己的App;後臺,就是自己的App不是啟用或者說專案的Activity不在activity堆疊的最頂端。那如何判斷呢?

我們離開自己的app可能是通過按鍵或頂端的提示,亦或者是中途的電話打斷,總而言之,就是Activity不是出於執行的狀態,所以我們的目標就是監聽所有的Activity的狀態!!咋一聽嚇死了,我的專案有好多Activity呢?別忘了我開始說的 我把每個Activity繼承於自己的Activity啦,所以實際上我只需要監聽一個啦。

好啦,貼程式碼:

package com.chaoxing.core;

import java.util.List;

import roboguice.activity.RoboActivity;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.app.ActivityManager.RunningAppProcessInfo; public class DefaultActivity extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); appRunningInBackground = AppRunningInBackground.getInstance(getApplicationContext());
appRunningInBackground.onCreate();
} @Override
protected void onStart() {
super.onStart();
appRunningInBackground.onStart();
}
@Override
protected void onStop() {
super.onStop();
appRunningInBackground.onStop();
} private AppRunningInBackground appRunningInBackground ; }

fragmentActivity也是同理。

import java.util.List;

public class AppRunningInBackground {

	public static boolean notifyShowing = false;
public static boolean isSpecialSystem=false;
private boolean isInstance = false;
ActivityManager activityManager;
KeyguardManager keyguardManager;
private Context context = null;
private static AppRunningInBackground appRunningInBackground = null;
public static AppRunningInBackground getInstance(Context context){
synchronized (AppRunningInBackground.class) {
if(appRunningInBackground == null){
appRunningInBackground = new AppRunningInBackground(context);
} return appRunningInBackground;
} } public AppRunningInBackground(Context context) {
this.context = context;
}
public void onCreate() {
if(!isInstance){
activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
isInstance = true;
} } public void onStart() {
if(notifyShowing && appRunningInBackground != null){
notifyShowing = false;
Intent startIntent = new Intent(AppGlobalConfig.STARTACTIVITY);
context.sendBroadcast(startIntent);
}
} public void onStop() {
if(appRunningInBackground != null && !isAppOnForeground() ){
notifyShowing = true;
Intent startIntent = new Intent(AppGlobalConfig.STOPACTIVITY);
context.sendBroadcast(startIntent);
}
} /**
* 判斷程式是否在前臺
* @return true 在前臺; false 在後臺
*/
private boolean isAppOnForeground() {
if(!isSpecialSystem){
boolean isspecial=true;
String packageName = context.getPackageName();
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(packageName)) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND||appProcess.importance == RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
return true;
}
if (keyguardManager.inKeyguardRestrictedInputMode()) return true;
}
if(isspecial){
if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
isspecial=false;
}
}
}
if(isspecial){
isSpecialSystem=true;
return !isApplicationBroughtToBackgroundByTask();
}
return false;
}else{
return !isApplicationBroughtToBackgroundByTask();
}
} /**
* 判斷當前應用程式是否處於後臺,通過getRunningTasks的方式
* @return true 在後臺; false 在前臺
*/
public boolean isApplicationBroughtToBackgroundByTask() {
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
}