360android手机浏览器有个轻应用,做的挺不错,研究了一下下。
在Activity类中有几个方法,
1.getTaskId():Return the identifier of the task this activity is in. This identifier will remain the same for the lifetime of the activity.
2.isTaskRoot(): Return whether this activity is the root of a task. The root is the first activity in a task.
3.moveTaskToBack(boolean nonRoot):Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.
对Android的task这个概念要理解透彻才能理解。辅助的要了解一下process。
运行了360的请应用后,长按home键,会有好几个轻应用的图标,很怪。
package com.example.test1;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.os.Debug;
public class AppInformation {
private ActivityManager activityManager = null;
public AppInformation(Context context) {
this.activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}
public boolean isAppRunning(String packagename) {
System.out.println("appIsRunning()...");
return false;
}
public int killProcessByName(String killProcessName) {
System.out.println("killProcessByName()...");
int killNum = 0;
List appProcessList = activityManager.getRunningAppProcesses();
for (int i = 0; i < appProcessList.size(); i++) {
RunningAppProcessInfo appProcessInfo = (RunningAppProcessInfo) appProcessList.get(i);
// 进程ID
int pid = appProcessInfo.pid;
// 用户ID,类似于Linux的权限不同,ID也就不同, 比如root
int uid = appProcessInfo.uid;
// 进程名,默认是包名或者由属性android:process=""指定
String processName = appProcessInfo.processName;
// 获得该进程占用的内存
int[] memPid = new int[] {
pid
};
// 此MemoryInfo位于android.os.Debug.MemoryInfo包中,用来统计进程的内存信息
Debug.MemoryInfo[] memoryInfo = activityManager.getProcessMemoryInfo(memPid);
// 获取进程占内存用信息kb单位
int memSize = memoryInfo[0].dalvikPrivateDirty;
System.out.println("process name: " + processName + " pid: " + pid + " uid: " + uid
+ " memory size is -->" + memSize + "kb");
// 获得每个进程里运行的应用程序(包),即每个应用程序的包名
String[] packageList = appProcessInfo.pkgList;
for (String pkg : packageList) {
System.out.println("package name " + pkg + " in process id is -->" + pid);
}
if (killProcessName.equals(processName)) {
System.out.println("===============killProcess pid-->" + pid);
android.os.Process.killProcess(pid);
killNum++;
}
}
return killNum;
}
public long getSystemAvaialbeMemorySize() {
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long memSize = memoryInfo.availMem;
System.out.println("getSystemAvaialbeMemorySize()...memory size: " + memSize);
return memSize;
// 调用系统函数,字符串转换long -String KB/MB
// return Formatter.formatFileSize(context, memSize);
}
public List getRunningAppProcessInfo() {
System.out.println("getRunningAppProcessInfo()...");
List appProcessList = activityManager.getRunningAppProcesses();
for (int i = 0; i < appProcessList.size(); i++) {
RunningAppProcessInfo appProcessInfo = (RunningAppProcessInfo) appProcessList.get(i);
// 进程ID
int pid = appProcessInfo.pid;
// 用户ID,类似于Linux的权限不同,ID也就不同, 比如root
int uid = appProcessInfo.uid;
// 进程名,默认是包名或者由属性android:process=""指定
String processName = appProcessInfo.processName;
// 获得该进程占用的内存
int[] memPid = new int[] {
pid
};
// 此MemoryInfo位于android.os.Debug.MemoryInfo包中,用来统计进程的内存信息
Debug.MemoryInfo[] memoryInfo = activityManager.getProcessMemoryInfo(memPid);
// 获取进程占内存用信息kb单位
int memSize = memoryInfo[0].dalvikPrivateDirty;
System.out.println("process name: " + processName + " pid: " + pid + " uid: " + uid
+ " memory size is -->" + memSize + "kb");
// 获得每个进程里运行的应用程序(包),即每个应用程序的包名
String[] packageList = appProcessInfo.pkgList;
for (String pkg : packageList) {
System.out.println("package name " + pkg + " in process id is -->" + pid);
}
}
return appProcessList;
}
public List getRunningServiceInfo() {
System.out.println("getRunningServiceInfo()...");
List serviceList = activityManager.getRunningServices(30);
for (int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = (RunningServiceInfo) serviceList.get(i);
// 进程ID
int pid = serviceInfo.pid;
// 用户ID,类似于Linux的权限不同,ID也就不同, 比如root
int uid = serviceInfo.uid;
// 进程名,默认是包名或者由属性android:process=""指定
String processName = serviceInfo.process;
String serviceStr = serviceInfo.toString();
System.out.println("serviceStr: " + serviceStr);
// 获得该进程占用的内存
int[] memPid = new int[] {
pid
};
// 此MemoryInfo位于android.os.Debug.MemoryInfo包中,用来统计进程的内存信息
Debug.MemoryInfo[] memoryInfo = activityManager.getProcessMemoryInfo(memPid);
// 获取进程占内存用信息kb单位
int memSize = memoryInfo[0].dalvikPrivateDirty;
System.out.println("The name of the process this service runs in: " + processName
+ " pid: " + pid + " uid: " + uid + " memory size is -->" + memSize + "kb");
}
return serviceList;
}
public List getRunningTaskInfo() {
System.out.println("getRunningServiceInfo()...");
List taskList = activityManager.getRunningTasks(30);
for (int i = 0; i < taskList.size(); i++) {
RunningTaskInfo taskInfo = (RunningTaskInfo) taskList.get(i);
String packageName = taskInfo.baseActivity.getPackageName();
System.out.println("package name: " + packageName);
}
return taskList;
}
}
http://blog.csdn.net/wdaming1986/article/details/7304191
http://www.cnblogs.com/cnhome/archive/2013/03/20/2971649.html
http://blog.csdn.net/lygglobetech/article/details/6600692