public class TaskStopActivity extends Activity implements OnClickListener {
private RefreshHandler handler;
private List<ApplicationInfo> installApplicationList;//获取已经安装程序列表
private RefreshThread thread;//开启一个线程用于停止
private ProgressDialog mypDialog;//用一个ProgressDialog显示停止进程的进度
private String[] threadFilterArray={"com.android.phone","com.process","system","com.android.launcher"};//在进行停止进程或者服务时我们可以过滤掉我们不希望停止的进程,此数组可以根据需要更改
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mypDialog = new ProgressDialog(this);
mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//进度条格式
mypDialog.setTitle(getString(R.string.notify));//标题
mypDialog.setMessage(getResources().getString(//内容
R.string.progress_tips_content));
mypDialog.setIcon(R.drawable.icon);//图标
mypDialog.setButton(getString(R.string.cancel), this);//添加取消按钮可以打断停止操作
mypDialog.setIndeterminate(false);
mypDialog.setCancelable(true);
mypDialog.show();
handler = new RefreshHandler();
thread = new RefreshThread();
thread.start();
}
/**
* 获取正在运行程序,并结束每一个用户级进程
*/
private void stopAllUserTask() {
PackageManager pm = this.getApplicationContext().getPackageManager();
installApplicationList = pm
.getInstalledApplications(0);//通过参数0获取安装应用的列表包含进程名
List<PackageInfo> installPackageList = pm.getInstalledPackages(0);//通过参数0获取安装应用包等信息包含package名
ActivityManager activityManager = (ActivityManager) this
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> allTaskList = activityManager
.getRunningAppProcesses();//获取正在运行的进程列表
for (int j = 0; j < allTaskList.size(); j++) {
for (int i = 0; i < installPackageList.size(); i++) {
if (allTaskList.get(j).processName
.equals(installApplicationList.get(i).processName)//获取对应位置想要获取进程的进程名
) {
for(int m=0;m<threadFilterArray.length;m++){
if(installPackageList.get(i).packageName.contains(threadFilterArray[m]))//跳过停止过滤数组中对象
break;
else if(m==threadFilterArray.length-1)
closeSpecifyTask(getApplicationInfo(allTaskList.get(j).processName).packageName);//通过进程名获取包名用以停止相应进程
}
}
}
}
}
/**
* 通过一个程序名返回该程序的一个ApplicationInfo对象
*
* @param name
* 程序名
* @return ApplicationInfo
*/
public ApplicationInfo getApplicationInfo(String appName) {
if (appName == null) {
return null;
}
if (installApplicationList.size() != 0) {
for (ApplicationInfo appinfo : installApplicationList) {
if (appName.equals(appinfo.processName)) {
return appinfo;
}
}
}
return null;
}
private class RefreshHandler extends Handler {
public void handleMessage(Message msg) {
// TODO : Update your UI
// 取消进度框
closeActivity();
}
}
class RefreshThread extends Thread {
public void run() {
// TODO : Do your Stuff here.
// procListAdapter = buildProcListAdapter();
try {
stopAllUserTask();
Thread.sleep( 1000 );
} catch ( InterruptedException e ) {
System.out.println( "Thread interrupted..." );
}
// TODOAuto-generated catch block e.printStackTrace(); }
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
}
}
/**
* 根据packagename结束任务
*
* @param packageName
*/
private void closeSpecifyTask(String packageName) {
Log.d("myDebug", packageName);
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
am.killBackgroundProcesses(packageName);//使用此操作工程的Manifest文件中必须添加android.permission.KILL_BACKGROUND_PROCESSES
}//窗口取消按钮操作
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
thread.interrupt();//打断进程
mypDialog.dismiss();
finish();
}
public void closeActivity() {
mypDialog.dismiss();
finish();
}
}
由于是widget组件控制结束所有进程,在结束进程之前我们可以需要筛选一些不应该结束的程序,我们使用一个数组进行保存需要进行过滤的进程列表,而在界面提供给用户的接入的方式是进度条方式,和一个取消按钮打断正在进行终止进程的操作,下面是该activity在Mainifest中的配置:
.....
<activity android:name=".TaskStopActivity" android:theme="@android:style/Theme.Dialog" >
</activity>
......
同时结束进程需要权限,在Mainifest中必须添加权限,配置如下:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
如果想了解android在进程结束方面的知识建议看下以下这篇博文:
http://blog.csdn.net/Zengyangtech/article/details/5733631