本文实例讲述了Android编程实现任务管理器的方法。分享给大家供大家参考,具体如下:
任务管理器可以实现的功能有:
1.查看当前系统下运行的所有的进程
2.可以查看每个进程的进程号、版本号以及内存占用情况
3.杀死进程(可以杀死全部进程或者杀死指定的进程)
4.查看系统剩余内存
效果图:
杀死全部进程
实现思路:
ActivityManager类可以获取到当前系统的所有进程,以及每个进程的信息,也可以杀死某个进程,
ActivityManager.getRunningAppProcesses(); 获取系统的所有进程
ActivityManager.getProcessMemoryInfo(pids); 根据进程号获取每个进程的内存占用情况
ActivityManager.restartPackage(processName) ; 根据进程的包名杀死某个进程
PackageManager可以根据包名获取程序的图标、版本号、名字、权限、签名
核心代码:
protected void initData() {
runningAppProcessInfos =ActivityManager.getRunningAppProcesses(); //获取所有的进程
userRunningAppProcessInfos = new ArrayList<ActivityManager.RunningAppProcessInfo>();//获取用户的进程
for(int i = 0; i< runningAppProcessInfos.size();i++){
if("system".equals(runningAppProcessInfos.get(i).processName)
||"android.process.media".equals(runningAppProcessInfos.get(i).processName)
||"android.process.acore".equals(runningAppProcessInfos.get(i).processName)
){
continue;
}
userRunningAppProcessInfos.add(runningAppProcessInfos.get(i));
}
}
注:杀死全部进程实际上杀死的是用户的所有进程,系统进程是无法杀死的,所以这里要获取到用户的所有的进程。
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
int[] pids = {userRunningAppProcessInfos.get(position).pid};
android.os.Debug.MemoryInfo[] memoryInfos = am.getProcessMemoryInfo(pids);
int memorysize = memoryInfos[0].getTotalPrivateDirty();
tv_app_memory.setText("内存占用为 "+ memorysize +" KB");
public class TaskInfo {
Context context ;
PackageManager pm ;
public TaskInfo(Context context) {
this.context = context;
pm = context.getPackageManager();
}
public Drawable getAppIcon(String packname){
try {
ApplicationInfo info = pm.getApplicationInfo(packname, 0);
return info.loadIcon(pm);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String getAppVersion(String packname){
try {
PackageInfo packinfo = pm.getPackageInfo(packname, 0);
return packinfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
public String getAppName(String packname){
try {
ApplicationInfo info = pm.getApplicationInfo(packname, 0);
return info.loadLabel(pm).toString();
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String[] getAppPremission(String packname){
try {
PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS);
//获取到所有的权限
return packinfo.requestedPermissions;
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
public String getAppSignature(String packname){
try {
PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES);
//获取到所有的权限
return packinfo.signatures[0].toCharsString();
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
}
private void killAll() {
for (int i = 0 ; i< userRunningAppProcessInfos.size();i++){
System.out.println(userRunningAppProcessInfos.get(i).processName);
activitymanager.restartPackage(userRunningAppProcessInfos.get(i).processName);
}
// 更新显示的界面
pd.show();
new Thread(){
@Override
public void run() {
initData();
Message msg = new Message();
msg.what = GET_RUNNING_APP_FINISH;
hander.sendMessage(msg);
}
}.start();
}
private void killSelected() {
for (int i=0 ;i < isselected.size(); i++){
if(isselected.get(i)){ //如果用户也选中则返回true
//终止一个activity 需要用到 activitymanager
activitymanager.restartPackage(userRunningAppProcessInfos.get(i).processName);
}
}
//更新listview的显示
pd.show();
new Thread(){
@Override
public void run() {
initData();
Message msg = new Message();
msg.what = GET_RUNNING_APP_FINISH;
hander.sendMessage(msg);
}
}.start();
}
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo outInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(outInfo);
long availMemorySize = outInfo.availMem;
String strMemorySize = availMemorySize/1024/1024 + "MB";
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:android简易文件管理器实例(列表式文件目录)android文件管理器用法详解Android开发之简单文件管理器实现方法浅析Android文件管理器(项目一)Android中创建多线程管理器实例Android使用SmsManager实现短信发送功能android短信管理器SmsManager实例详解