本文实例讲述了android源码探索之定制android关机界面的方法。分享给大家供大家参考。具体如下:
在android系统中,长按power键默认会弹出对话框让你选择“飞行模式”,“静音”,“关机”等功能。如下图所示:
但这些功能都对android-x86和其他终端产品就没什么必要了。本文就简单介绍下如何定制关机界面。
我的目标是长按power键,将会关机,弹出“设备将要关机”选择对话框。如果可以选择“是”关机,和“否”返回系统。
按照android源码定制要点中提到的,首先你要对整个系统有全面的了解,找到弹出原来这个选择框的代码,它在这里:
<pre name="code" class="java">frameworks\policies\base\phone\com\android\internal\policy\impl\phonewindowmanager.java
显示对话框调用的代码如下:
1
2
3
4
5
6
7
8
|
runnable mpowerlongpress = new runnable() { public void run() { mshouldturnoffonkeyup = false ; performhapticfeedbacklw( null , hapticfeedbackconstants.long_press, false ); sendclosesystemwindows(system_dialog_reason_global_actions); showglobalactionsdialog(); } }; |
调用showglobalactionsdialog方法之后将会聚到有“飞行模式”、“静音”、“关机”等选项的对话框。
找到这里,我们就知道该做什么了!干掉它,换成我们想要的关机代码,就大功告成了!既然这样,事不宜迟,让我们赶快到showgloabalactiondialog方法中看看关机的部分在哪里!
showglobalactionsdialog的实现部分在这里:
frameworks\policies\base\phone\com\android\internal\policy\impl\globalaction.java
1
2
3
4
5
6
7
8
9
10
11
|
public void showdialog( boolean keyguardshowing, boolean isdeviceprovisioned) { mkeyguardshowing = keyguardshowing; mdeviceprovisioned = isdeviceprovisioned; if (mdialog == null ) { mstatusbar = (statusbarmanager)mcontext.getsystemservice(context.status_bar_service); mdialog = createdialog(); } preparedialog(); mstatusbar.disable(statusbarmanager.disable_expand); mdialog.show(); } |
我们可以很清楚的看到,这里新建了一个mdialog,然后prepare接着就show了它,那么,这个mdialog就是关键了,看看它是怎么被createdialog创建出来的吧,仍然在这个文件中:
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
|
/** * create the global actions dialog. * @return a new dialog. */ private alertdialog createdialog() { msilentmodetoggle = new toggleaction( r.drawable.ic_lock_silent_mode, r.drawable.ic_lock_silent_mode_off, r.string.global_action_toggle_silent_mode, r.string.global_action_silent_mode_on_status, r.string.global_action_silent_mode_off_status) { void willcreate() { // xxx: fixme: switch to ic_lock_vibrate_mode when available menablediconresid = (settings.system.getint(mcontext.getcontentresolver(), settings.system.vibrate_in_silent, 1 ) == 1 ) ? r.drawable.ic_lock_silent_mode_vibrate : r.drawable.ic_lock_silent_mode; } void ontoggle( boolean on) { if (on) { maudiomanager.setringermode((settings.system.getint(mcontext.getcontentresolver(), settings.system.vibrate_in_silent, 1 ) == 1 ) ? audiomanager.ringer_mode_vibrate : audiomanager.ringer_mode_silent); } else { maudiomanager.setringermode(audiomanager.ringer_mode_normal); } } public boolean showduringkeyguard() { return true ; } public boolean showbeforeprovisioning() { return false ; } }; mairplanemodeon = new toggleaction( r.drawable.ic_lock_airplane_mode, r.drawable.ic_lock_airplane_mode_off, r.string.global_actions_toggle_airplane_mode, r.string.global_actions_airplane_mode_on_status, r.string.global_actions_airplane_mode_off_status) { void ontoggle( boolean on) { if ( boolean .parseboolean( systemproperties.get(telephonyproperties.property_inecm_mode))) { miswaitingforecmexit = true ; // launch ecm exit dialog intent ecmdialogintent = new intent(telephonyintents.action_show_notice_ecm_block_others, null ); ecmdialogintent.addflags(intent.flag_activity_new_task); mcontext.startactivity(ecmdialogintent); } else { changeairplanemodesystemsetting(on); } } @override protected void changestatefrompress( boolean buttonon) { // in ecm mode airplane state cannot be changed if (!( boolean .parseboolean( systemproperties.get(telephonyproperties.property_inecm_mode)))) { mstate = buttonon ? state.turningon : state.turningoff; mairplanestate = mstate; } } public boolean showduringkeyguard() { return true ; } public boolean showbeforeprovisioning() { return false ; } }; <span style= "color:#ff0000;" >mitems = lists.newarraylist( // silent mode msilentmodetoggle, // next: airplane mode mairplanemodeon, // last: power off new singlepressaction( com.android.internal.r.drawable.ic_lock_power_off, r.string.global_action_power_off) { </span><span style= "color:#3333ff;" ><u> public void onpress() { // shutdown by making sure radio and power are handled accordingly. shutdownthread.shutdown(mcontext, true ); }</u></span><span style= "color:#ff0000;" > public boolean showduringkeyguard() { return true ; } public boolean showbeforeprovisioning() { return true ; }</span> }); madapter = new myadapter(); final alertdialog.builder ab = new alertdialog.builder(mcontext); ab.setadapter(madapter, this ) .setinversebackgroundforced( true ) .settitle(r.string.global_actions); final alertdialog dialog = ab.create(); dialog.getwindow().settype(windowmanager.layoutparams.type_system_dialog); if (!mcontext.getresources().getboolean( com.android.internal.r.bool.config_sf_slowblur)) { dialog.getwindow().setflags(windowmanager.layoutparams.flag_blur_behind, windowmanager.layoutparams.flag_blur_behind); } dialog.setondismisslistener( this ); return dialog; } |
看看我们发现了什么!!蓝色的部分就是关机调用的函数了!!shutdown方法的第二个参数标识是否弹出询问对话框。你可以选择需要(true)或者不需要(false)。这里我保守一点,还是选个true吧,万一不小心按到关机键呢,呵呵。。。
也就是说,只要我们用
替换掉前面的
就可以大功告成了!还等什么!我们修改
frameworks\policies\base\phone\com\android\internal\policy\impl\phonewindowmanager.java
的源代码如下:
1
2
3
4
5
6
7
8
9
|
runnable mpowerlongpress = new runnable() { public void run() { mshouldturnoffonkeyup = false ; performhapticfeedbacklw( null , hapticfeedbackconstants.long_press, false ); sendclosesystemwindows(system_dialog_reason_global_actions); //showglobalactionsdialog(); shutdownthread.shutdown(mcontext, true ); } }; |
好了,大功告成了!!
是不是就这样完了呢?发现编译不过。。。
细节很重要!!
原来shutdownthread.shutdown(mcontext, true)的引用包没加进来!!幸好有gcc。。。
将上面这个包加到
frameworks\policies\base\phone\com\android\internal\policy\impl\phonewindowmanager.java
中,再次编译,通过,yes!
看看我们的战果吧:
是不是感觉到源码定制的快感和成就感了呢?
这仅仅只是个开始,好戏还在后头呢!!哈哈
希望本文所述对大家的android程序设计有所帮助。