Activity可以获取运行中的应用信息,可以获取到servcie,process,app,memory,Task信息等。
获取信息
ActivityManager常用方法
判断应用是否在前台运行,应用是否在运行
//判断应用是否在前台运行
public boolean isRunningForeground(Context context){
String packageName=getPackageName(context);
String topActivityClassName=getTopActivityName(context);
System.out.println("packageName="+packageName+",topActivityClassName="+topActivityClassName);
if (packageName!=null&&topActivityClassName!=null&&topActivityClassName.startsWith(packageName)) {
System.out.println("应用在前台执行");
return true;
} else {
System.out.println("应用在后台执行");
return false;
}
}
// 判断应用是否在运行
public boolean isRun(Context context,String mPackageName){
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
boolean isAppRunning = false;
//100表示取的最大的任务数,info.topActivity表示当前正在运行的Activity,info.baseActivity表示系统后台有此进程在运行
for (RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(mPackageName) || info.baseActivity.getPackageName().equals(mPackageName)) {
isAppRunning = true;
Log.i("ActivityService",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
break;
}
}
if(isAppRunning){
Log.i("ActivityService", "该程序正在运行");
}else{
Log.i("ActivityService", "该程序没有运行");
}
return isAppRunning;
}
//获取栈顶ActivityName
public String getTopActivityName(Context context){
String topActivityClassName=null;
ActivityManager activityManager =
(ActivityManager)(context.getSystemService(android.content.Context.ACTIVITY_SERVICE )) ;
List<runningtaskinfo> runningTaskInfos = activityManager.getRunningTasks(1) ;
if(runningTaskInfos != null){
ComponentName f=runningTaskInfos.get(0).topActivity;
topActivityClassName=f.getClassName();
}
return topActivityClassName;
}
public String getPackageName(Context context){
String packageName = context.getPackageName();
return packageName;
}
自定义ActivityManager管理Activity
我们需要定义一个自己的ActivityManager,并且在BaseActivity中的OnCreate方法里将启动的Activity通过我们自定义的ActivityManager把任务放入栈中,在onDestroy方法中将Activity退栈。
/**
* 用于管理Activity,获取Activity
* 在结束一个activity后应该判断当前栈是否为空,为空则将本类引用置为null,以便于虚拟机回收内存
* 单例,调用 {@link #getActivityManager()} 获取实例
* 成员变量 {@link #mActivityStack} 应该与系统的回退栈保持一致,所以在启动activity的时候必须在其onCreate中
* 将该activity加入栈顶,在activity结束时,必须在onDestroy中将该activity出栈
*/
public class ActivityManager {
private static ReStack<Activity> mActivityStack; //Activity栈
private static ActivityManager mInstance;
private ActivityManager() {
mActivityStack = new ReStack<>();
}
/**
* 获取ActivityManager的单例.
*
* @return ActivityManager实例
*/
public static ActivityManager getActivityManager() {
if (mInstance == null) {
mInstance = new ActivityManager();
}
return mInstance;
}
/**
* 添加一个activity到栈顶.
*
* @param activity 添加的activity
*/
public void pushActivity(Activity activity) {
if (mActivityStack == null) {
mActivityStack = new ReStack<>();
}
mActivityStack.push(activity);
}
/**
* 获取栈顶的Activity.
*
* @return 如果栈存在, 返回栈顶的activity
*/
public Activity peekActivity() {
if (mActivityStack != null && !mActivityStack.isEmpty()) {
return mActivityStack.peek();
} else {
return null;
}
}
/**
* 结束当前的activity,在activity的onDestroy中调用.
*/
public void popActivity() {
if (mActivityStack != null && !mActivityStack.isEmpty()) {
mActivityStack.pop();
}
//如果移除一个activity之后栈为空,将本类的引用取消,以便于让虚拟机回收
if (mActivityStack != null && mActivityStack.isEmpty()) {
mInstance = null;
}
}
/**
* 结束最接近栈顶的匹配类名的activity.
* 遍历到的不一定是被结束的,遍历是从栈底开始查找,为了确定栈中有这个activity,并获得一个引用
* 删除是从栈顶查找,结束查找到的第一个
* 在activity外结束activity时调用
*
* @param klass 类名
*/
public void popActivity(Class<? extends BaseActivity> klass) {
for (Activity activity : mActivityStack) {
if (activity != null && activity.getClass().equals(klass)) {
activity.finish();
break; //只结束一个
}
}
}
//移除所有的Activity
public void removeAll(){
for (Activity activity : mActivityStack) {
if (activity != null) {
activity.finish();
break;
}
}
}
}
转载:https://blog.csdn.net/qq_38520096/article/details/82109948