Android中使用ActivityManager可以获得进程信息,并对进程进行管理,如结束进程等。本文使用ActivityManager获得进程列表,并结束选中的进程。
首先,看看布局文件。
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
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
>
<LinearLayout
android:layout_width=
"fill_parent"
android:layout_height=
"match_parent"
android:layout_weight=
"5"
>
<TextView
android:id=
"@+id/textView1"
style=
"@style/my_style"
android:layout_width=
"match_parent"
android:layout_height=
"fill_parent"
android:layout_weight=
"3"
/>
<TextView
android:id=
"@+id/textView2"
style=
"@style/my_style"
android:layout_width=
"match_parent"
android:layout_height=
"fill_parent"
android:layout_gravity=
"center_vertical|center_horizontal"
android:layout_weight=
"3"
android:gravity=
"center_vertical|center_horizontal"
android:text=
"进程号"
/>
<TextView
android:id=
"@+id/textView3"
style=
"@style/my_style"
android:layout_width=
"match_parent"
android:layout_height=
"fill_parent"
android:layout_weight=
"2"
android:gravity=
"center_vertical|center_horizontal"
android:text=
"进程名"
/>
<TextView
android:id=
"@+id/textView4"
style=
"@style/my_style"
android:layout_width=
"match_parent"
android:layout_height=
"fill_parent"
android:layout_weight=
"3"
android:gravity=
"center_vertical|center_horizontal"
android:text=
"内存"
/>
</LinearLayout>
<ListView
android:id=
"@id/android:list"
android:layout_width=
"fill_parent"
android:layout_height=
"match_parent"
android:layout_weight=
"1"
>
</ListView>
<Button
android:id=
"@+id/btn_pro"
style=
"@style/my_style"
android:layout_width=
"fill_parent"
android:layout_height=
"match_parent"
android:layout_weight=
"5"
android:text=
"结束"
/>
</LinearLayout>
|
然后是Java文件。
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
package
com.hzhi.sysinfor;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
android.app.ActivityManager;
import
android.app.AlertDialog;
import
android.app.ListActivity;
import
android.app.ProgressDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.content.Intent;
import
android.content.pm.PackageInfo;
import
android.content.pm.PackageManager;
import
android.graphics.drawable.Drawable;
import
android.net.Uri;
import
android.os.Bundle;
import
android.os.Debug;
import
android.util.Log;
import
android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.Button;
import
android.widget.CheckBox;
import
android.widget.CompoundButton;
import
android.widget.CompoundButton.OnCheckedChangeListener;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
process
extends
ListActivity{
Button btn_stop;
PackageManager pkg_man;
list_pro list_ada;
ActivityManager am;
// 获得所有的进程列表
public
void
get_pro(){
list_ada =
new
list_pro(
this
);
// 包管理器
am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// 获取手机内所有应用
List<ActivityManager.RunningAppProcessInfo> pi = am.getRunningAppProcesses();
for
(
int
i=
0
; i<pi.size(); i++){
ActivityManager.RunningAppProcessInfo pii =
(ActivityManager.RunningAppProcessInfo) pi.get(i);
int
[] mem =
new
int
[] {pii.pid};
Debug.MemoryInfo[] mi = am.getProcessMemoryInfo(mem);
list_ada.addItem(String.valueOf(pii.pid),
pii.processName,
String.valueOf(mi[
0
].dalvikPrivateDirty) +
"KB"
,
false
);
}
setListAdapter(list_ada);
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_process);
get_pro();
btn_stop = (Button) findViewById(R.id.btn_pro);
btn_stop.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
for
(
int
j=
0
; j<list_ada.list_data.size(); j++){
// 该应用需要删除
if
(list_ada.list_data.get(j).is_chk){
String name = (String)
list_ada.list_data.get(j).txt_name.getText();
final
String pro = (String)
list_ada.list_data.get(j).txt_name.getText();
new
AlertDialog.Builder(process.
this
)
.setTitle(
"结束进程"
)
.setMessage(
"确定结束"
+ name +
"吗?"
)
.setPositiveButton(
"是"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface arg0,
int
arg1) {
unload(pro);
get_pro();
}
})
.setNegativeButton(
"否"
,
null
)
.show();
}
}
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
public
boolean
unload (String n){
boolean
res =
true
;
am.killBackgroundProcesses(n);
Toast.makeText(process.
this
,
"进程"
+ n +
"已经结束!"
, Toast.LENGTH_LONG)
.show();
return
res;
}
}
// pro列表
class
list_pro
extends
BaseAdapter
implements
OnClickListener{
public
Context ctx;
public
List<item_pro> list_data;
public
list_pro(Context context){
ctx = context;
list_data =
new
ArrayList<item_pro>();
}
@Override
public
int
getCount() {
// TODO Auto-generated method stub
return
list_data.size();
}
@Override
public
Object getItem(
int
arg0) {
// TODO Auto-generated method stub
return
list_data.get(arg0);
}
@Override
public
long
getItemId(
int
arg0) {
// TODO Auto-generated method stub
return
list_data.indexOf(arg0);
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
item_pro my_item;
Integer i =
0
;
if
(convertView ==
null
)
{
my_item =
new
item_pro(ctx,
(String)list_data.get(position).txt_no.getText(),
(String)list_data.get(position).txt_name.getText(),
(String)list_data.get(position).txt_size.getText(),
list_data.get(position).is_chk);
}
else
{
my_item = (item_pro)convertView;
my_item.txt_no.setText(list_data.get(position).txt_no.getText());
my_item.txt_name.setText(list_data.get(position).txt_name.getText());
my_item.txt_size.setText(list_data.get(position).txt_size.getText());
}
CheckBox chk_item = (CheckBox) my_item.chk_pro;
chk_item.setOnClickListener(
this
);
chk_item.setChecked(list_data.get(position).is_chk);
chk_item.setTag(position);
return
my_item;
}
public
void
addItem(String txt_no, String txt_name,
String str_size, Boolean bol_chk)
{
list_data.add(
new
item_pro(ctx,txt_no,txt_name,str_size,bol_chk));
}
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
CheckBox c = (CheckBox) v;
int
pos = Integer.parseInt(v.getTag().toString());
list_data.get(pos).is_chk = c.isChecked();
}
}
// pro列表的一行
class
item_pro
extends
LinearLayout{
public
CheckBox chk_pro;
// 进程号
public
TextView txt_no;
// 进程名
public
TextView txt_name;
// 占用内存大小
public
TextView txt_size;
// 是否选中
public
boolean
is_chk;
public
item_pro(Context ctx, String str_no, String str_name,
String str_size, Boolean bol_chk)
{
super
(ctx);
this
.setOrientation(HORIZONTAL);
int
hei =
80
;
chk_pro =
new
CheckBox(ctx);
addView(chk_pro,
new
LinearLayout.LayoutParams((
int
)(MainActivity.wid_scr*
0.2
),hei));
txt_no =
new
TextView(ctx);
txt_no.setText(str_no);
addView(txt_no,
new
LinearLayout.LayoutParams((
int
)(MainActivity.wid_scr*
0.2
),hei));
txt_name =
new
TextView(ctx);
txt_name.setText(str_name);
addView(txt_name,
new
LinearLayout.LayoutParams((
int
)(MainActivity.wid_scr*
0.4
),hei));
txt_size =
new
TextView(ctx);
txt_size.setText(str_size);
addView(txt_size,
new
LinearLayout.LayoutParams((
int
)(MainActivity.wid_scr*
0.2
),hei));
is_chk = bol_chk;
}
}
|
Java文件实现了三个类。process类是整个Activity的类,list_pro是适配器类,item_pro是列表里面的一行。
在process类的get_pro()方法里面,语句am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)获得了一个ActivityManager,List<ActivityManager.RunningAppProcessInfo> pi = am.getRunningAppProcesses()获得了系统的所有进程,并存储在列表pi里面。语句ActivityManager.RunningAppProcessInfo pii = (ActivityManager.RunningAppProcessInfo) pi.get(i)获得了每一个进程信息,pii.processName是该进程的进程名(通常是该进程对应的包名),mi[0].dalvikPrivateDirty是该进程占用的内存大小(通常以KB为单位)。在unload (String n)方法里面,am.killBackgroundProcesses(n)结束了进程名为n的进程,不过要在AndroidManifest.xml中加入android.permission.KILL_BACKGROUND_PROCESSES权限,否则会出现权限错误。
运行效果如下。
需要注意的是,系统进程是不能结束的。