http://blog.csdn.net/caszhao/archive/2010/11/04/5986743.aspx
在状态栏上添加Menu,Back,Home三个按钮的方法,在下文有良好的阐述。详见:
《在android的状态栏(statusbar)中增加menu,home和back快捷键的方法》http://blog.csdn.net/freshui/archive/2010/07/15/5738115.aspx
在使用过程中,存在两个问题:
- HOME键在我的机器上无效
- 连击按钮时,由于有时Intent未响应,导致弹起操作未响应直接诶被WindowsManager抛掉。这样就出现了虽然手已离开屏幕,但按钮出现高亮的状态。
1、对于第一个问题,我的方法是把home键单独进行处理,向launcher发送一个启动activity的Intent,这样可以直接回到桌面上。在原方法的基础上,对Home单独进行处理。
2? {?
3? ????Log.d(TAG,?"HOME?button?Intent!");?
4? ????Intent?intent=?new?Intent(Intent.ACTION_MAIN);?
5? ????intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);?
6? ????intent.addCategory(Intent.CATEGORY_HOME);?
7? ????mService.sendIntent2(intent);?
8? ?}?
9? else?
10 {?
11 ????Log.d(TAG,?"other?two?buttons?Intent!");?
12 ????Intent?intent?=?new?Intent(Intent.ACTION_ICONKEY_CHANGED);?
13 ????intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);?
14 ????intent.putExtra("keycode",???mResvKeyCode);?
15 ????mService.sendIntent(intent);?
16 }
在StatusBarService.java中添加sendIntent2方法
2 {?
3 ???? mContext.startActivity(intent);?
4 }
2、第二个问题的修正思想是使用Animation逐帧动画来使得按钮从完成 正常态按下后-变化至高亮态-恢复正常态 这一周期。
a. 先在res/layout(与status_bar.xml同目录)中添加三个animation动画xml配置文件。
分别是
animation_home.xml:
2? <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
3? ??? android:oneshot="true">?
4? ????<item?
5? ??????? android:duration="80"?
6? ??????? android:drawable="@drawable/stat_home"?>?
7? ????</item>?
8? ????<item?
9? ??????? android:duration="80"?
10 ??????? android:drawable="@drawable/stat_home_pressed"?>?
11 ????</item>?
12 ????<item?
13 ??????? android:duration="80"?
14 ??????? android:drawable="@drawable/stat_home"?>?
15 ????</item>?
16 </animation-list>
animation_back.xml:
2? <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
3? ??? android:oneshot="true">?
4? ????<item?
5? ??????? android:duration="80"?
6? ??????? android:drawable="@drawable/stat_back"?>?
7? ????</item>?
8? ????<item?
9? ??????? android:duration="80"?
10 ??????? android:drawable="@drawable/stat_back_pressed"?>?
11 ????</item>?
12 ????<item?
13 ??????? android:duration="80"?
14 ??????? android:drawable="@drawable/stat_back"?>?
15 ????</item>?
16 </animation-list>
17
18 ?
19
animation_menu.xml:
2? <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
3? ??? android:oneshot="true">?
4? ????<item?
5? ??????? android:duration="80"?
6? ??????? android:drawable="@drawable/stat_menu"?>?
7? ????</item>?
8? ????<item?
9? ??????? android:duration="80"?
10 ??????? android:drawable="@drawable/stat_menu_pressed"?>?
11 ????</item>?
12 ????<item?
13 ??????? android:duration="80"?
14 ??????? android:drawable="@drawable/stat_menu"?>?
15 ????</item>?
16 </animation-list>
17
18
这三个xml文件分别对应着 home back menu的动画过程
b . 在res/layout/status_bar.xml中修改Imageview的background指向上述三个xml配置文件:
2?? <!--????android:background="@drawable/status_bar_closed_default_background"?-->?
3?? <com.android.server.status.StatusBarView xmlns:android="http://schemas.android.com/apk/res/android"?
4?? ??? android:background="@drawable/statusbar_background"?
5?? ??? android:orientation="vertical"?
6?? ??? android:focusable="true"?
7?? ??? android:descendantFocusability="afterDescendants"?
8?? ????>?
9??
10? ????<LinearLayout android:id="@+id/icons"?
11? ??????? android:layout_width="fill_parent"?
12? ??????? android:layout_height="fill_parent"?
13? ??????? android:orientation="horizontal">?
14? ?????????<!--Ethan.zhao?:?Start?Add?home?button?on?status_bar-->????
15? ???????????????<ImageView?
16? ??????????????? android:id="@+id/status_home"?
17? ???????????????? android:layout_width="wrap_content"??
18? ???????????????? android:layout_height="wrap_content"??
19? ???????????????? android:layout_gravity="top"?
20? ???????????????? android:background="@layout/animation_home"?
21? ????????????/>?
22? ?????????<!--Ethan.zhao?:?Start?Add?home?button?on?status_bar-->????????
23?
24? ????????<com.android.server.status.IconMerger android:id="@+id/notificationIcons"?
25? ??????????? android:layout_width="0dip"?
26? ??????????? android:layout_weight="1"?
27? ??????????? android:layout_height="fill_parent"?
28? ??????????? android:layout_alignParentLeft="true"?
29? ??????????? android:paddingLeft="6dip"?
30? ??????????? android:gravity="center_vertical"?
31? ??????????? android:orientation="horizontal"/>??
32?
33? ????????<LinearLayout android:id="@+id/statusIcons"?
34? ??????????? android:layout_width="wrap_content"?
35? ??????????? android:layout_height="fill_parent"?
36? ??????????? android:layout_alignParentRight="true"?
37? ??????????? android:paddingRight="6dip"?
38? ??????????? android:gravity="center_vertical"?
39? ??????????? android:orientation="horizontal"/>?
40? ?????????<!--Ethan.zhao?:?Start?Add?menu\back?button?on?status_bar-->?
41? ????????????<ImageView?
42? ??????????????? android:id="@+id/status_menu"?
43? ??????????????? android:layout_width="color: