/**
* 文件名:BaseActivity.java
* 版本号:
* 变更:
*/
package com.wadata.mobilefollowup.view.base
import java.util.List
import android.app.Activity
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.content.Context
/**
* 名称:BaseActivity
* 描述:
* 创建人:
* 变更:
*/
public class BaseActivity extends Activity {
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop()
if (!isAppOnForeground()) {
//app 进入后台
//全局变量isActive = false 记录当前已经进入后台
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume()
//if (!isActive) {
//app 从后台唤醒,进入前台
//isActive = true
//}
}
/**
* 程序是否在前台运行
*
* @return
*/
public boolean isAppOnForeground() {
// 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)
return false
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) {
return true
}
}
return false
}
}
Android中可以使用 ActivityManager来获得正在运行或者已经启动的程序,示例如下:
// 查询所有正在运行的应用程序信息: 包括他们所在的进程id和进程名// 这儿我直接获取了系统里安装的所有应用程序,然后根据报名pkgname过滤获取所有真正运行的应用程序
private List<RunningAppInfo> queryAllRunningAppInfo() {
pm = this.getPackageManager()
// 查询所有已经安装的应用程序
List<ApplicationInfo> listAppcations = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES)
Collections.sort(listAppcations,new ApplicationInfo.DisplayNameComparator(pm))// 排序
// 保存所有正在运行的包名 以及它所在的进程信息
Map<String, ActivityManager.RunningAppProcessInfo> pgkProcessAppMap = new HashMap<String, ActivityManager.RunningAppProcessInfo>()
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)
// 通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程
List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager
.getRunningAppProcesses()
for (ActivityManager.RunningAppProcessInfo appProcess : appProcessList) {
int pid = appProcess.pid // pid
String processName = appProcess.processName // 进程名
Log.i(TAG, "processName: " + processName + " pid: " + pid)
敬饥 String[] pkgNameList = appProcess.pkgList // 获得运行在该进程里的所有应用程序包
// 输出所有应用程序的包名
for (int i = 0 i < pkgNameList.length i++) {
String pkgName = pkgNameList[i]
Log.i(TAG, "packageName " + pkgName + " at index " + i+ " in process " + pid)
// 加入至map对象里
pgkProcessAppMap.put(pkgName, appProcess)
}
}
// 保存所有正在运行的应用程序信亮败返息
List<RunningAppInfo> runningAppInfos = new ArrayList<RunningAppInfo>() // 保存过滤查到的AppInfo
for (ApplicationInfo app : listAppcations) {
// 如果该包名存在 则构造枯宽一个RunningAppInfo对象
if (pgkProcessAppMap.containsKey(app.packageName)) {
// 获得该packageName的 pid 和 processName
int pid = pgkProcessAppMap.get(app.packageName).pid
String processName = pgkProcessAppMap.get(app.packageName).processName
runningAppInfos.add(getAppInfo(app, pid, processName))
}
}
return runningAppInfos
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)