简单的杀死后台进程应用

学习安卓开发已经两个月了,写了个杀死后台进程的小应用,还有很多不完善的地方,希望大家指教下!

[1].[代码] [Java]代码 跳至 [1] [2] [3]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.example.admin.thkiller;
 
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
 
public class MainActivity extends ActionBarActivity {
 
     private Button but_kill;
     private ListView myListView;
     private ActivityManager activityManager;
     private List<ActivityManager.RunningAppProcessInfo> list;
     private int start,end,total;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         this .start = this .getRunningThredas();
         /*
         for(int i=0;i<this.list.size();i++){
             Log.i("Threads:", this.list.get(i).pid + ":" + this.list.get(i).processName+":"+this.list.get(i).describeContents());
         }
         */
         this .reloadListView();
         this .but_kill = (Button) findViewById(R.id.but_kill);
         this .but_kill.setOnClickListener( new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 for ( int j= 0 ;j<MainActivity. this .list.size();j++){ // 循环杀死进程
                     if (! MainActivity. this .list.get(j).processName.contains( "THKiller" ) && ! MainActivity. this .list.get(j).processName.contains( "android" )) {
                         MainActivity. this .activityManager.killBackgroundProcesses(MainActivity. this .list.get(j).processName);
                     }
                 }
 
                 MainActivity. this .end = MainActivity. this .getRunningThredas();
                 MainActivity. this .total = MainActivity. this .start - MainActivity. this .end;
                 ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
                 MainActivity. this .activityManager.getMemoryInfo(outInfo);
                 int lastMemory = ( int )outInfo.availMem/ 1021 / 1024 ;
                 MainActivity. this .reloadListView();
                 Toast.makeText(MainActivity. this , "清除了" +MainActivity. this .total+ "个进程,内存剩余" +lastMemory+ "M!" ,Toast.LENGTH_LONG).show();
             }
         });
     }
     // 获取正在运行的进程数量
     private int getRunningThredas(){
         this .activityManager = (ActivityManager) this .getSystemService(Context.ACTIVITY_SERVICE);
         this .list = activityManager.getRunningAppProcesses();
         return this .list.size();
     }
     // 载入ListView
     private void reloadListView(){
         String proName = null ;
         Drawable proIcon = null ;
         PackageManager packageManager;
 
         this .myListView = (ListView) findViewById(R.id.myListView);
         ArrayList<AppInfo> listData = new ArrayList<AppInfo>();
         for ( int k= 0 ;k< this .list.size();k++){
             AppInfo appInfo = new AppInfo();
             ApplicationInfo packageInfo = null ;
             try {
                 packageManager = getApplicationContext().getPackageManager();
                 packageInfo = getPackageManager().getApplicationInfo( this .list.get(k).processName, 0 );
 
                 appInfo.proName = packageManager.getApplicationLabel(packageInfo).toString();
                 appInfo.proIcon = packageManager.getApplicationIcon(packageInfo);
 
                 Log.i( "test" ,appInfo.proName);
                 listData.add(appInfo);
             } catch (Exception e){
 
             }
         }
         MyAdapter myAdapter = new MyAdapter( this ,listData);
         this .myListView.setAdapter(myAdapter);
 
     }
 
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
         return true ;
     }
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
 
         //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
             return true ;
         }
 
         return super .onOptionsItemSelected(item);
     }
}

[2].[文件] THKiller.zip ~ 4MB    下载(0) 跳至 [1] [2] [3]

文件不存在或者代码语言不存在

[3].[图片] 1025603877.jpg 跳至 [1] [2] [3]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值