Android之進程查看,關閉(可操作所有進程):
- public class NotificationDemoActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ActivityManager am = (ActivityManager) this.getSystemService(this.ACTIVITY_SERVICE);
- // 獲得手機進程列表
- List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
- for (RunningAppProcessInfo runningAppProcessInfo : infos) {
- System.out.println(runningAppProcessInfo.processName);
- // 去除包含Android包名跟本包名的進程
- if(runningAppProcessInfo.processName.indexOf("android") == -1 && runningAppProcessInfo.processName.indexOf(this.getPackageName()) == -1) {
- // 關閉進程
- am.killBackgroundProcesses(runningAppProcessInfo.processName);
- }
- }
- }
- }
權限增加:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission>
關閉進程時要防止關閉了系統進程及不該禁止的進程
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11