获取手机所有正在运行的进程(返回的是List集合)
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
|
package
com.cca.mobilephone.engine;
import
java.util.ArrayList;
import
java.util.List;
import
android.app.ActivityManager;
import
android.app.ActivityManager.RunningAppProcessInfo;
import
android.content.Context;
import
android.content.pm.ApplicationInfo;
import
android.content.pm.PackageInfo;
import
android.content.pm.PackageManager;
import
android.content.pm.PackageManager.NameNotFoundException;
import
android.graphics.drawable.Drawable;
import
com.cca.mobilephone.domain.ProcessInfo;
/**
* 获取所有正在运行的进程信息
* @author Administrator
*
*/
public
class
TaskInfoProvifer {
public
static
List<processinfo> getRunningProcessInfo(Context context){
List<processinfo> process=
new
ArrayList<processinfo>();
ActivityManager am=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
PackageManager pm=context.getPackageManager();
//获取正在运行的进程集合
List<runningappprocessinfo>processrunninginfoinfo=am.getRunningAppProcesses();
//遍历集合
for
(RunningAppProcessInfo runninginfo:processrunninginfoinfo){
ProcessInfo processinfo=
new
ProcessInfo();
//进程包名
String packageName=runninginfo.processName;
processinfo.setPackageName(packageName);
long
menSize=am.getProcessMemoryInfo(
new
int
[]{runninginfo.pid})[
0
].
getTotalPrivateDirty()*
1024
;
processinfo.setMenSize(menSize);
try
{
PackageInfo packageinfo=pm.getPackageInfo(packageName,
0
);
//进程图标
Drawable icon=packageinfo.applicationInfo.loadIcon(pm);
processinfo.setIcon(icon);
//进程名称
String processName=packageinfo.applicationInfo.loadLabel(pm).toString();
processinfo.setProcessName(processName);
//
if
((packageinfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)!=
0
){
//系统进程
processinfo.setUserProcess(
false
);
}
else
{
//用户进程
processinfo.setUserProcess(
true
);
}
}
catch
(NameNotFoundException e) {
e.printStackTrace();
}
process.add(processinfo);
}
return
process;
}
}
</runningappprocessinfo></processinfo></processinfo></processinfo>
|
javabean类的信息
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
|
package
com.cca.mobilephone.domain;
import
android.graphics.drawable.Drawable;
/**
* 进程包含的信息
* @author Administrator
*
*/
public
class
ProcessInfo {
private
String packageName;
private
String processName;
private
Drawable icon;
private
long
menSize;
private
boolean
userProcess;
public
String getPackageName() {
return
packageName;
}
public
void
setPackageName(String packageName) {
this
.packageName = packageName;
}
public
String getProcessName() {
return
processName;
}
public
void
setProcessName(String processName) {
this
.processName = processName;
}
public
Drawable getIcon() {
return
icon;
}
public
void
setIcon(Drawable icon) {
this
.icon = icon;
}
public
long
getMenSize() {
return
menSize;
}
public
void
setMenSize(
long
menSize) {
this
.menSize = menSize;
}
public
boolean
isUserProcess() {
return
userProcess;
}
public
void
setUserProcess(
boolean
userProcess) {
this
.userProcess = userProcess;
}
|
}
区分用户进程和系统进程(这是耗时操作)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
List<processinfo> processinfo=TaskInfoProvifer.getRunningProcessInfo(getApplicationContext());
userprocess=
new
ArrayList<processinfo>();
systemprocess=
new
ArrayList<processinfo>();
for
(ProcessInfo process:processinfo){
if
(process.isUserProcess()){
//用户程序
userprocess.add(process);
}
else
{
//系统程序
systemprocess.add(process);
}
}
//通知界面更新
handler.sendEmptyMessage(
0
);
</processinfo></processinfo></processinfo>
|
打开一个服务,内部注册一个广播接收者,监听锁屏清理进程
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
|
public
class
AutoKillService
extends
Service {
private
InnerScrrenOffReceiver receiver;
private
Timer timer;
private
TimerTask task;
@Override
public
IBinder onBind(Intent intent) {
return
null
;
}
@Override
public
void
onCreate() {
super
.onCreate();
receiver=
new
InnerScrrenOffReceiver();
IntentFilter filter=
new
IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, filter);
/**
* 常用定时器,可设置为定时清理的功能
*/
timer=
new
Timer();
task=
new
TimerTask() {
@Override
public
void
run() {
//System.out.println("每1秒执行一次");
//清理的逻辑
}
};
timer.schedule(task,
0
,
1000
);
}
@Override
public
void
onDestroy() {
super
.onDestroy();
timer.cancel();
task.cancel();
unregisterReceiver(receiver);
receiver=
null
;
}
private
class
InnerScrrenOffReceiver
extends
BroadcastReceiver{
@Override
public
void
onReceive(Context context, Intent intent) {
//System.out.println("哈哈,屏幕锁屏了");
ActivityManager am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<runningappprocessinfo> infos= am.getRunningAppProcesses();
for
(RunningAppProcessInfo info:infos){
//杀死后台进程
am.killBackgroundProcesses(info.processName);
}
}
}
}
</runningappprocessinfo>
|
如何创建一个widget只需要3步
1、定义一个广播接收者继承AppwidgetProvider
2、在清单文件中配置广播接收者
1
2
3
4
5
6
|
<receiver android:name=
"ExampleAppWidgetProvider"
>
<intent-filter>
</action></intent-filter>
<meta-data android:name=
"android.appwidget.provider"
android:resource=
"@xml/example_appwidget_info"
>
</meta-data></receiver>
|
3、建立一个xml文件夹存放资源example_appwidget_info.xml
1
2
|
</appwidget-provider>
|
4、自定义布局example_appwidget的内容
生命周期:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
在手机桌面上创建第一个widget
11
-
05
00
:
46
:
24.817
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
11
-
05
00
:
46
:
24.817
: I/System.out(
1687
): onenable (适合应用程序widget的初始化.)
11
-
05
00
:
46
:
24.817
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
11
-
05
00
:
46
:
24.817
: I/System.out(
1687
): onupdate (只要有新的widget被创建都会调用onupdate方法)
11
-
05
00
:
46
:
27.347
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
如果界面上已经有一个widget被创建,再创建相同的widget
11
-
05
00
:
47
:
34.728
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
11
-
05
00
:
47
:
34.728
: I/System.out(
1687
): onupdate
11
-
05
00
:
47
:
36.629
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
删除一个widget
11
-
05
00
:
48
:
48.019
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
11
-
05
00
:
48
:
48.019
: I/System.out(
1687
): ondelete
最后一个widget被删除
11
-
05
00
:
49
:
25.710
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
11
-
05
00
:
49
:
25.710
: I/System.out(
1687
): ondelete
11
-
05
00
:
49
:
25.710
: I/System.out(
1687
): onreceive 接收到了系统的广播消息
11
-
05
00
:
49
:
25.710
: I/System.out(
1687
): ondisabled (适合做应用程序扫尾的操作,)
|
总结: 不要记生命周期调用的先后顺序.
1
2
3
|
onenable 方法什么时候调用
ondisabled 方法什么时候调用
onupdate方法 在每次创建新的widget的时候都会调用 , 并且当时间片到的时候也会调用
|
创建widget模板
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
|
public
class
MyWidget
extends
AppWidgetProvider {
@Override
public
void
onReceive(Context context, Intent intent) {
//创建第一个桌面widget的时候调用
super
.onReceive(context, intent);
}
//每次创建widget时都会调用
@Override
public
void
onUpdate(Context context, AppWidgetManager appWidgetManager,
int
[] appWidgetIds) {
Intent intent=
new
Intent(context,UpdateWidgetService.
class
);
context.startService(intent);
super
.onUpdate(context, appWidgetManager, appWidgetIds);
}
//删除widget会调用
@Override
public
void
onDeleted(Context context,
int
[] appWidgetIds) {
super
.onDeleted(context, appWidgetIds);
}
//对widget做初始化时调用
@Override
public
void
onEnabled(Context context) {
super
.onEnabled(context);
}
//结束widget是调用,做扫尾工作
@Override
public
void
onDisabled(Context context) {
super
.onDisabled(context);
Intent intent=
new
Intent(context,UpdateWidgetService.
class
);
context.stopService(intent);
}
}
|
桌面启动服务定时更新widget
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
|
public
class
UpdateWidgetService
extends
Service {
private
Timer timer;
private
TimerTask task;
@Override
public
IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return
null
;
}
@Override
public
void
onCreate() {
super
.onCreate();
timer=
new
Timer();
task=
new
TimerTask(){
@Override
public
void
run() {
System.out.println(
"更新widget里面的内容"
);
/*
* 进程间通讯
*/
ComponentName provider=
new
ComponentName(getApplicationContext(), MyWidget.
class
);
/*
* 告诉桌面布局文件去哪里找
*/
RemoteViews views=
new
RemoteViews(getPackageName(), R.layout.process_widget);
views.setTextViewText(R.id.process_count,
"正在运行的软件"
+ProcessInfoUtils.getRunningProcessCount(getApplicationContext())+
"个"
);
String availstr=Formatter.formatFileSize(getApplicationContext(), ProcessInfoUtils.getAvialRam(getApplicationContext()));
views.setTextViewText(R.id.process_memory,
"可用内存:"
+availstr);
am.updateAppWidget(provider, views);
}};
timer.schedule(task,
0
,
5000
);
}
@Override
public
void
onDestroy() {
super
.onDestroy();
timer.cancel();
task.cancel();
timer=
null
;
task=
null
;
}
}
|
逆向小助手的使用:反编译获取素材资源文件,everything可快速查找
反编译 拖拽到目录
多层显示的帧布局,功能多多
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<framelayout android:layout_height=
"0dp"
android:layout_weight=
"1"
android:layout_width=
"match_parent"
>
<listview android:fastscrollenabled=
"true"
android:id=
"@+id/lv_process_listview"
android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
>
</listview>
<linearlayout android:gravity=
"center"
android:id=
"@+id/ll_loading"
android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:orientation=
"vertical"
android:visibility=
"visible"
>
<progressbar android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
>
<textview android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"正在加载中..."
>
</textview></progressbar></linearlayout>
<textview android:background=
"#00FF00"
android:id=
"@+id/tv_process_biaoshi"
android:layout_height=
"wrap_content"
android:layout_width=
"match_parent"
android:text=
"用户进程"
android:textcolor=
"#000000"
android:textsize=
"15sp"
>
</textview></framelayout>
|
杀死所有运行的进程
1
2
3
4
5
6
7
8
|
ActivityManager am=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<runningappprocessinfo> infos=am.getRunningAppProcesses();
for
(RunningAppProcessInfo info:infos){
am.killBackgroundProcesses(info.processName);
//参数为包名
}
</runningappprocessinfo>
|
抽屉控件
(重下往上拉),也可指定拉升的高度
1
2
3
4
5
6
7
8
9
|
<slidingdrawer android:content=
"@+id/my_content"
android:handle=
"@+id/my_handle"
android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
>
<imageview android:id=
"@id/my_handle"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:src=
"@drawable/ic_launcher"
>
<linearlayout android:gravity=
"center"
android:id=
"@id/my_content"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:orientation=
"vertical"
>
<textview android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"我是一个"
>
<progressbar android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
>
</progressbar></textview></linearlayout>
</imageview></slidingdrawer>
|
也可重右往左拉
1
2
|
<slidingdrawer android:content=
"@+id/my_content"
android:handle=
"@+id/my_handle"
android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:orientation=
"horizontal"
>
</slidingdrawer>
|
界面的切换(一个切面不同的布局切换)
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
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<linearlayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:orientation=
"vertical"
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<linearlayout android:gravity=
"center"
android:layout_height=
"wrap_content"
android:layout_width=
"match_parent"
android:orientation=
"horizontal"
>
<textview android:background=
"@drawable/tab_left_pressed"
android:gravity=
"center_vertical"
android:id=
"@+id/app_unlock"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"未加锁软件"
android:textsize=
"20sp"
>
<textview android:background=
"@drawable/tab_right_default"
android:gravity=
"center_vertical"
android:id=
"@+id/app_locked"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"已加锁软件"
android:textsize=
"20sp"
>
</textview></textview></linearlayout>
<framelayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
>
<linearlayout android:id=
"@+id/ll_loading"
android:layout_gravity=
"center"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:orientation=
"vertical"
android:visibility=
"visible"
>
<progressbar android:layout_gravity=
"center"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
>
<textview android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"正在加载数据..."
>
</textview></progressbar></linearlayout>
<linearlayout android:id=
"@+id/ll_appunlock"
android:layout_height=
"wrap_content"
android:layout_width=
"match_parent"
android:orientation=
"vertical"
>
<textview android:id=
"@+id/tv_unlock_count"
android:layout_height=
"wrap_content"
android:layout_width=
"match_parent"
android:text=
"未加锁软件"
>
<listview android:id=
"@+id/lv_unlock"
android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
>
</listview>
</textview></linearlayout>
<linearlayout android:id=
"@+id/ll_applocked"
android:layout_height=
"wrap_content"
android:layout_width=
"match_parent"
android:orientation=
"vertical"
android:visibility=
"gone"
>
<textview android:id=
"@+id/tv_locked_count"
android:layout_height=
"wrap_content"
android:layout_width=
"match_parent"
android:text=
"加锁软件"
>
<listview android:id=
"@+id/lv_locked"
android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
>
</listview>
</textview></linearlayout>
</framelayout>
</linearlayout>
|