近来有个同学要求给他做个进程管理器,用来控制他玩QQ,微信的时间。虽然关于进程管理这种东西存在很大争议,但是既然有人需要它就有存在的意义。
这是我搜索相关资料后自己初步做出来的程序,可以查看当前进程的包名,也可以关闭相应进程。先上代码
首先是布局文件,只有一个ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" >
</ListView>
</RelativeLayout>
然后是菜单文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/clear"
android:orderInCategory="100"
android:showAsAction="never"
android:title="一键清除"/>
<item
android:id="@+id/fresh"
android:orderInCategory="100"
android:showAsAction="never"
android:title="刷新"/>
</menu>
这是主代码
package com.example.progressmanager;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
//显示当前进程的listview
private ListView progress ;
//装载进程数据的数组
private List<String> showprogress = new ArrayList<String>();
private List<RunningAppProcessInfo> infos = new ArrayList<ActivityManager.RunningAppProcessInfo>();
//进程管理器
private ActivityManager am;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
initView();
}
//初始化
private void initView() {
// TODO Auto-generated method stub
//获取服务
am = (ActivityManager) this.getSystemService(this.ACTIVITY_SERVICE);
//初始化控件
progress= (ListView)findViewById(R.id.progress);
//显示listview
makelistview();
progress.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
am.killBackgroundProcesses(infos.get(arg2).processName);
// Toast.makeText(MainActivity.this,infos.get(arg2).processName, Toast.LENGTH_SHORT).show();
// Toast.makeText(MainActivity.this, "sss", Toast.LENGTH_SHORT);
makelistview();
}
});
}
//显示listview
private void makelistview(){
// ActivityManager am = (ActivityManager) this.getSystemService(this.ACTIVITY_SERVICE);
// 获得手机进程列表
// List<RunningAppProcessInfo>
infos = am.getRunningAppProcesses();
//清除上一次的缓存
showprogress.clear();
for (RunningAppProcessInfo runningAppProcessInfo : infos) {
System.out.println(runningAppProcessInfo.processName);
showprogress.add(runningAppProcessInfo.processName);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,showprogress);
progress.setAdapter(adapter);
}
}
private void killprogress(){
// 获得手机进程列表
// List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcessInfo : infos) {
// 去除包含Android包名跟本包名的进程
if(runningAppProcessInfo.processName.indexOf("android") == -1 && runningAppProcessInfo.processName.indexOf(this.getPackageName()) == -1) {
// 关闭进程
am.killBackgroundProcesses(runningAppProcessInfo.processName);
}
}
makelistview();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
/**
* 子菜单项响应代码
*/
switch(item.getItemId()) {
case R.id.clear:
// 设置Activity的Title
killprogress();
break;
case R.id.fresh:
makelistview();
break;
}
return true;
}
}
最后是权限声明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.progressmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.progressmanager.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>