/**
* 文件名:BaseActivity.java
* 版本號:
* 日期:2012-6-20
* 建立人:
* Copyright wadata 版權全部
* 變動:
*/
packagecom.wadata.mobilefollowup.view.base;
importjava.util.List;
importandroid.app.Activity;
importandroid.app.ActivityManager;
importandroid.app.ActivityManager.RunningAppProcessInfo;
importandroid.content.Context;
/**
* 名稱:BaseActivity
* 描述:
* 建立人:
* 日期:2012-6-20 下午5:53:35
* 變動:
*/
publicclassBaseActivityextendsActivity {
@Override
protectedvoidonStop() {
// TODO Auto-generated method stub
super.onStop();
if(!isAppOnForeground()) {
//app 進入後臺
//全局變量isActive = false 記錄當前已經進入後臺
}
}
@Override
protectedvoidonResume() {
// TODO Auto-generated method stub
super.onResume();
//if (!isActive) {
//app 從後臺喚醒,進入前臺
//isActive = true;
//}
}
/**
* 程序是否在前臺運行
*
*/
publicbooleanisAppOnForeground() {
// Returns a list of application processes that are running on the
// device
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
String packageName = getApplicationContext().getPackageName();
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if(appProcesses ==null)
returnfalse;
for(RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if(appProcess.processName.equals(packageName)
&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
returntrue;
}
}
returnfalse;
}
}