本文實例講述了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程序設計有所幫助。