杀死进程
杀死进程:环保代码
当我们退出一个应用程序关闭所有的界面的时候如果这个应用没有后台服务,那么该应用程序还会有个空进程占用手机内存
当手机内存不足的时候才会杀死掉空进程(空进程的优先级最低)
在DDMS下看模拟器可以看到当前运行着的所有进程,并且在online这列下有每个进程的id
第一种:自杀:不能杀死别人
int pid=android.os.Process.myPid();//得到自己这个应用程序的进程id
android.os.Process.killProcess(pid);//接收的参数为pid(进程id),该方法不能杀死别的应用程序
第二种杀死进程:杀死别的应用程序:不能杀死自己
需要的权限: <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
注意:该方法杀不死手机的一些重要应用,比如打电话的应用,只能杀死空进程和后台进程,不能杀死服务进程
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); method(); } //按下返回键回调的方法 @Override public void onBackPressed() { //该方法原本就是结束了当前活动 AlertDialog.Builder builder=new Builder(this); builder.setTitle("提醒"); builder.setMessage("确定要退出当前应用吗?"); builder.setPositiveButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish();//关闭当前的Activity(进程还在) //杀死自己这个应用的进程(实现环保程序) int pid=android.os.Process.myPid();//得到自己这个应用程序的进程id android.os.Process.killProcess(pid);//接收的参数为pid(进程id),该方法不能杀死别的应用程序 } }); builder.setNegativeButton("取消", null); builder.show(); } //杀死别的应用程序的方法,不能杀自己,只能杀死空进程和后台进程,不能杀死服务进程 public void method(){ String packname="com.example.ts.widget";//需要杀死的应用程序的包名 //获取活动管理者 ActivityManager am=(ActivityManager) getSystemService(Service.ACTIVITY_SERVICE); am.killBackgroundProcesses(packname); } }
查看所有进程以及杀死指定的进程的代码:
工程结构如下:
1.布局文件:
1.1 主布局:main.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 为此Activity配置好布局文件 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginBottom="50dip" android:orientation="horizontal"> <TextView android:layout_width="120dip" android:layout_height="wrap_content" android:text="系统可用内存为:"></TextView> <TextView android:id="@+id/tvAvailMemory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700"></TextView> </LinearLayout> <Button android:id="@+id/btProcessInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看系统进程信息"></Button> <Button android:id="@+id/btRunningApp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看所有正在运行的程序"></Button> </LinearLayout>
1.2 查看所有运行的应用listviwe的子条目布局(不具备杀死进程的功能)browse_app_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dip"> <ImageView android:id="@+id/imgApp" android:layout_width="wrap_content" android:layout_height="fill_parent" ></ImageView> <RelativeLayout android:layout_width="fill_parent" android:layout_marginLeft="10dip" android:layout_height="wrap_content"> <TextView android:id="@+id/tvLabel" android:layout_width="wrap_content" android:layout_height="35dip" android:text="AppLable : "></TextView> <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content" android:layout_toRightOf="@id/tvLabel" android:layout_height="35dip" android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="35dip" android:layout_below="@id/tvLabel" android:text="包名:"></TextView> <TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content" android:layout_height="35dip" android:layout_below="@id/tvAppLabel" android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView> <TextView android:id="@+id/tvPId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvName" android:textColor="#FF0000" android:text="所在进程ID:"></TextView> <TextView android:id="@+id/tvProcessId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvPkgName" android:layout_alignLeft="@id/tvPkgName" android:textColor="#FF0000"></TextView> <TextView android:id="@+id/tvPName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvPId" android:textColor="#FF0000" android:text="所在进程名:"></TextView> <TextView android:id="@+id/tvProcessName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvProcessId" android:layout_alignLeft="@id/tvProcessId" android:textColor="#FF0000"></TextView> </RelativeLayout> </LinearLayout>
1.3上面子条目的listview布局文件: browse_app_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/tvInfo" android:layout_width="fill_parent" android:layout_height="30dip" android:textSize="20sp"></TextView> <ListView android:id="@+id/listviewApp" android:layout_width="fill_parent" android:layout_height="fill_parent" ></ListView> </LinearLayout>
1.4 浏览所有运行的进程的ListView子条目(具备杀死进程的功能) browse_process_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="80dip" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程ID : "></TextView> <TextView android:id="@+id/tvProcessPID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dip" android:textColor="#FFD700"></TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程所在UID:"></TextView> <TextView android:id="@+id/tvProcessUID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700"></TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程占用内存:"></TextView> <TextView android:id="@+id/tvProcessMemSize" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700"></TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:orientation="horizontal"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="进程名:"></TextView> <TextView android:id="@+id/tvProcessName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFD700"></TextView> </LinearLayout> </LinearLayout>
1.5 上面对应的ListView布局 browse_process_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tvTotalProcessNo" android:layout_width="fill_parent" android:layout_height="30dip" android:textSize="20sp"></TextView> <ListView android:id="@+id/listviewProcess" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="10dip" ></ListView> </LinearLayout>
2.java代码
2.1 运行的所有应用的信息的实体类 RunningAppInfo.java
import android.content.Intent; import android.graphics.drawable.Drawable; //Model类 ,用来存储应用程序信息 public class RunningAppInfo { private String appLabel; //应用程序标签 private Drawable appIcon ; //应用程序图像 private String pkgName ; //应用程序所对应的包名 private int pid ; //该应用程序所在的进程号 private String processName ; // 该应用程序所在的进程名 public RunningAppInfo(){} public String getAppLabel() { return appLabel; } public void setAppLabel(String appName) { this.appLabel = appName; } public Drawable getAppIcon() { return appIcon; } public void setAppIcon(Drawable appIcon) { this.appIcon = appIcon; } public String getPkgName(){ return pkgName ; } public void setPkgName(String pkgName){ this.pkgName=pkgName ; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getProcessName() { return processName; } public void setProcessName(String processName) { this.processName = processName; } }
2.2 所有运行的进程的信息实体类(带杀死进程的功能) ProcessInfo.java
//Model类 public class ProcessInfo { private int pid; // 进程id Android规定android.system.uid=1000 private int uid; // 进程所在的用户id ,即该进程是有谁启动的 root/普通用户等 private int memSize; // 进程占用的内存大小,单位为kb private String processName; // 进程名 //为图方便,直接声明为公有了 public String pkgnameList[] ; //运行在该进程里的所有应用程序的包名, packagname ; public ProcessInfo(){} public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public int getMemSize() { return memSize; } public void setMemSize(int memSize) { this.memSize = memSize; } public String getProcessName() { return processName; } public void setPocessName(String processName) { this.processName = processName; } }
2.3 主活动类 MainActivity.java
import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.Debug; import android.text.format.Formatter; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.CUPCAKE) public class MainActivity extends Activity { private static String TAG = "AM_MEMORYIPROCESS" ; private ActivityManager mActivityManager = null ; private TextView tvAvailMem ; private Button btProcessInfo ; private Button btRunningApp ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvAvailMem = (TextView)findViewById(R.id.tvAvailMemory) ; btProcessInfo =(Button)findViewById(R.id.btProcessInfo); //跳转到显示进程信息界面,可以杀死指定进程 btProcessInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,BrowseProcessInfoActivity.class); startActivity(intent); } }); btRunningApp = (Button)findViewById(R.id.btRunningApp) ; btRunningApp.setOnClickListener(new View.OnClickListener() { //查看所有运行的应用 @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,BrowseRunningAppActivity.class); startActivity(intent); } }); //获得ActivityManager服务的对象 mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); //获得可用内存信息 String availMemStr = getSystemAvaialbeMemorySize(); Log.i(TAG, "The Availabel Memory Size is"+availMemStr); //显示 tvAvailMem.setText(availMemStr); } //获得系统可用内存信息 private String getSystemAvaialbeMemorySize(){ //获得MemoryInfo对象 MemoryInfo memoryInfo = new MemoryInfo() ; //获得系统可用内存,保存在MemoryInfo对象上 mActivityManager.getMemoryInfo(memoryInfo) ; long memSize = memoryInfo.availMem ; //字符类型转换 String availMemStr = formateFileSize(memSize); return availMemStr ; } //调用系统函数,字符串转换 long -String KB/MB private String formateFileSize(long size){ return Formatter.formatFileSize(MainActivity.this, size); } }
2.4 查看所有运行的应用的适配器类 BrowseRunningAppAdapter.java
import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; //自定义适配器类,提供给listView的自定义view public class BrowseRunningAppAdapter extends BaseAdapter { private List<RunningAppInfo> mlistAppInfo = null; LayoutInflater infater = null; public BrowseRunningAppAdapter(Context context, List<RunningAppInfo> apps) { infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mlistAppInfo = apps ; } @Override public int getCount() { // TODO Auto-generated method stub System.out.println("size" + mlistAppInfo.size()); return mlistAppInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mlistAppInfo.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertview, ViewGroup arg2) { System.out.println("getView at " + position); View view = null; ViewHolder holder = null; if (convertview == null || convertview.getTag() == null) { view = infater.inflate(R.layout.browse_app_item, null); holder = new ViewHolder(view); view.setTag(holder); } else{ view = convertview ; holder = (ViewHolder) convertview.getTag() ; } RunningAppInfo appInfo = (RunningAppInfo) getItem(position); holder.appIcon.setImageDrawable(appInfo.getAppIcon()); holder.tvAppLabel.setText(appInfo.getAppLabel()); holder.tvPkgName.setText(appInfo.getPkgName()); holder.tvProcessId.setText(appInfo.getPid()+"") ; holder.tvProcessName.setText(appInfo.getProcessName()) ; return view; } class ViewHolder { ImageView appIcon; TextView tvAppLabel; TextView tvPkgName; TextView tvProcessId ; TextView tvProcessName ; public ViewHolder(View view) { this.appIcon = (ImageView) view.findViewById(R.id.imgApp); this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel); this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName); this.tvProcessId = (TextView) view.findViewById(R.id.tvProcessId); this.tvProcessName = (TextView) view.findViewById(R.id.tvProcessName); } } }
2.5 查看所有运行的应用的Activity类: BrowseRunningAppActivity.java
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class BrowseRunningAppActivity extends Activity { private static String TAG = "BrowseRunningAppActivity"; private ListView listview = null; private List<RunningAppInfo> mlistAppInfo = null; private TextView tvInfo = null ; private PackageManager pm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.browse_app_list); listview = (ListView) findViewById(R.id.listviewApp); tvInfo = (TextView)findViewById(R.id.tvInfo) ; mlistAppInfo = new ArrayList<RunningAppInfo>(); // 查询某一特定进程的所有应用程序 Intent intent = getIntent(); //是否查询某一特定pid的应用程序 int pid = intent.getIntExtra("EXTRA_PROCESS_ID", -1); if ( pid != -1) { //某一特定经常里所有正在运行的应用程序 mlistAppInfo =querySpecailPIDRunningAppInfo(intent, pid); } else{ // 查询所有正在运行的应用程序信息: 包括他们所在的进程id和进程名 tvInfo.setText("所有正在运行的应用程序有-------"); mlistAppInfo = queryAllRunningAppInfo(); } BrowseRunningAppAdapter browseAppAdapter = new BrowseRunningAppAdapter(this, mlistAppInfo); listview.setAdapter(browseAppAdapter); } // 查询所有正在运行的应用程序信息: 包括他们所在的进程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; } // 某一特定经常里所有正在运行的应用程序 private List<RunningAppInfo> querySpecailPIDRunningAppInfo(Intent intent , int pid) { String[] pkgNameList = intent.getStringArrayExtra("EXTRA_PKGNAMELIST"); String processName = intent.getStringExtra("EXTRA_PROCESS_NAME"); //update ui tvInfo.setText("进程id为"+pid +" 运行的应用程序共有 : "+pkgNameList.length); pm = this.getPackageManager(); // 保存所有正在运行的应用程序信息 List<RunningAppInfo> runningAppInfos = new ArrayList<RunningAppInfo>(); // 保存过滤查到的AppInfo for(int i = 0 ; i<pkgNameList.length ;i++){ //根据包名查询特定的ApplicationInfo对象 ApplicationInfo appInfo; try { appInfo = pm.getApplicationInfo(pkgNameList[i], 0); runningAppInfos.add(getAppInfo(appInfo, pid, processName)); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 0代表没有任何标记; } return runningAppInfos ; } // 构造一个RunningAppInfo对象 ,并赋值 private RunningAppInfo getAppInfo(ApplicationInfo app, int pid, String processName) { RunningAppInfo appInfo = new RunningAppInfo(); appInfo.setAppLabel((String) app.loadLabel(pm)); appInfo.setAppIcon(app.loadIcon(pm)); appInfo.setPkgName(app.packageName); appInfo.setPid(pid); appInfo.setProcessName(processName); return appInfo; } }
2.6 查看所有运行的进程的适配器类 BrowseProcessInfoAdapter.java
import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; //自定义适配器类,提供给listView的自定义view public class BrowseProcessInfoAdapter extends BaseAdapter { private List<ProcessInfo> mlistProcessInfo = null; LayoutInflater infater = null; public BrowseProcessInfoAdapter(Context context, List<ProcessInfo> apps) { infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mlistProcessInfo = apps ; } @Override public int getCount() { // TODO Auto-generated method stub System.out.println("size" + mlistProcessInfo.size()); return mlistProcessInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mlistProcessInfo.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertview, ViewGroup arg2) { System.out.println("getView at " + position); View view = null; ViewHolder holder = null; if (convertview == null || convertview.getTag() == null) { view = infater.inflate(R.layout.browse_process_item, null); holder = new ViewHolder(view); view.setTag(holder); } else{ view = convertview ; holder = (ViewHolder) convertview.getTag() ; } ProcessInfo processInfo = (ProcessInfo) getItem(position); holder.tvPID.setText(processInfo.getPid() +""); holder.tvUID.setText(processInfo.getUid() +"") ; holder.tvProcessMemSize.setText(processInfo.getMemSize()+"KB"); holder.tvProcessName.setText(processInfo.getProcessName()); return view; } class ViewHolder { TextView tvPID ; //进程ID TextView tvUID ; //用户ID TextView tvProcessMemSize ; //进程占用内存大小 TextView tvProcessName ; // 进程名 public ViewHolder(View view) { this.tvPID = (TextView)view.findViewById(R.id.tvProcessPID) ; this.tvUID = (TextView) view.findViewById(R.id.tvProcessUID); this.tvProcessMemSize = (TextView) view.findViewById(R.id.tvProcessMemSize); this.tvProcessName = (TextView)view.findViewById(R.id.tvProcessName) ; } } }
2.7 查看所有运行的进程的Activity类(可以杀死进程) BrowseProcessInfoActivity.java
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Debug; import android.util.Log; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class BrowseProcessInfoActivity extends Activity implements OnItemClickListener{ private static String TAG = "ProcessInfo"; private static final int KILL_PORCESS = 1; private static final int SEARCH_RUNNING_APP = 2; private ActivityManager mActivityManager = null; // ProcessInfo Model类 用来保存所有进程信息 private List<ProcessInfo> processInfoList = null; private ListView listviewProcess; private TextView tvTotalProcessNo ; private String [] dialogItems = new String[] {"杀死该进程","查看运行在该进程的应用程序"} ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.browse_process_list); listviewProcess = (ListView) findViewById(R.id.listviewProcess); listviewProcess.setOnItemClickListener(this); tvTotalProcessNo =(TextView)findViewById(R.id.tvTotalProcessNo); // 获得ActivityManager服务的对象 mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); // 获得系统进程信息 getRunningAppProcessInfo(); // 为ListView构建适配器对象 BrowseProcessInfoAdapter mprocessInfoAdapter = new BrowseProcessInfoAdapter(this, processInfoList); listviewProcess.setAdapter(mprocessInfoAdapter); tvTotalProcessNo.setText("当前系统进程共有:"+processInfoList.size()); } //杀死该进程,并且刷新 @Override public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) { // TODO Auto-generated method stub new AlertDialog.Builder(this).setItems(dialogItems, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //杀死进程 if(which == 0) { //杀死该进程,释放进程占用的空间 mActivityManager.killBackgroundProcesses(processInfoList.get(position).getProcessName()); //刷新界面 getRunningAppProcessInfo() ; BrowseProcessInfoAdapter mprocessInfoAdapter = new BrowseProcessInfoAdapter( BrowseProcessInfoActivity.this, processInfoList); listviewProcess.setAdapter(mprocessInfoAdapter); tvTotalProcessNo.setText("当前系统进程共有:"+processInfoList.size()); } //查看运行在该进程的应用程序 else if(which ==1){ ProcessInfo processInfo = processInfoList.get(position); Intent intent = new Intent() ; intent.putExtra("EXTRA_PKGNAMELIST", processInfo.pkgnameList) ; intent.putExtra("EXTRA_PROCESS_ID", processInfo.getPid()); intent.putExtra("EXTRA_PROCESS_NAME", processInfo.getProcessName()); intent.setClass(BrowseProcessInfoActivity.this, BrowseRunningAppActivity.class); startActivity(intent); } } }).create().show() ; } // 获得系统进程信息 private void getRunningAppProcessInfo() { // ProcessInfo Model类 用来保存所有进程信息 processInfoList = new ArrayList<ProcessInfo>(); // 通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程 List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager .getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) { // 进程ID号 int pid = appProcessInfo.pid; // 用户ID 类似于Linux的权限不同,ID也就不同 比如 root等 int uid = appProcessInfo.uid; // 进程名,默认是包名或者由属性android:process=""指定 String processName = appProcessInfo.processName; // 获得该进程占用的内存 int[] myMempid = new int[] { pid }; // 此MemoryInfo位于android.os.Debug.MemoryInfo包中,用来统计进程的内存信息 Debug.MemoryInfo[] memoryInfo = mActivityManager .getProcessMemoryInfo(myMempid); // 获取进程占内存用信息 kb单位 int memSize = memoryInfo[0].dalvikPrivateDirty; Log.i(TAG, "processName: " + processName + " pid: " + pid + " uid:" + uid + " memorySize is -->" + memSize + "kb"); // 构造一个ProcessInfo对象 ProcessInfo processInfo = new ProcessInfo(); processInfo.setPid(pid); processInfo.setUid(uid); processInfo.setMemSize(memSize); processInfo.setPocessName(processName); //保存所有运行在该应用程序的包名 processInfo.pkgnameList = appProcessInfo.pkgList ; processInfoList.add(processInfo); // 获得每个进程里运行的应用程序(包),即每个应用程序的包名 String[] packageList = appProcessInfo.pkgList; Log.i(TAG, "process id is " + pid + "has " + packageList.length); for (String pkg : packageList) { Log.i(TAG, "packageName " + pkg + " in process id is -->"+ pid); } } } }
效果图:运行的线程(带杀死功能的)
查看运行的应用 没有杀死功能的: