您的位置:首页 > 移动开发 > Android开发

对在Android的状态栏添加Menu,Back,Home的改进

2010-11-04 11:02 489 查看
在状态栏上添加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单独进行处理。


if

 
(RESV_KEY_HOME
 
=
=
 
mResvKeyCode)
 


{
 


 
 
 
 
Log
.
d(TAG,
 
"

HOME

 

button

 

Intent!

"

);
 


 
 
 
 
Intent
 
intent
=
 
new

 
Intent(Intent
.
ACTION_MAIN);
 


 
 
 
 
intent
.
setFlags(Intent
.
FLAG_ACTIVITY_NEW_TASK);
 


 
 
 
 
intent
.
addCategory(Intent
.
CATEGORY_HOME);
 


 
 
 
 
mService
.
sendIntent2(intent);
 


 
}
 


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方法

1
void

 
sendIntent2(Intent
 
intent)
 

2
{
 

3
 
 
 
 
mContext
.
startActivity(intent);
 

4
}

2、第二个问题的修正思想是使用Animation逐帧动画来使得按钮从完成 正常态按下后-变化至高亮态-恢复正常态 这一周期。

a. 先在res/layout(与status_bar.xml同目录)中添加三个animation动画xml配置文件。

分别是

animation_home.xml:


<
?xml
version
=
"
1.0
"
encoding
=
"
utf-8
"
?
>
 


<
animation-list
xmlns:android
=
" http://schemas.android.com/apk/res/android "


 
 
 
android:oneshot
=
"
true
"
>
 


 
 
 
 
<
item
 


 
 
 
 
 
 
 
android:duration
=
"
80
"
 


 
 
 
 
 
 
 
android:drawable
=
"
@drawable/stat_home
"
 
>
 


 
 
 
 
<
/item
>
 


 
 
 
 
<
item
 


 
 
 
 
 
 
 
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:


<
?xml
version
=
"
1.0
"
encoding
=
"
utf-8
"
?
>
 


<
animation-list
xmlns:android
=
" http://schemas.android.com/apk/res/android "


 
 
 
android:oneshot
=
"
true
"
>
 


 
 
 
 
<
item
 


 
 
 
 
 
 
 
android:duration
=
"
80
"
 


 
 
 
 
 
 
 
android:drawable
=
"
@drawable/stat_back
"
 
>
 


 
 
 
 
<
/item
>
 


 
 
 
 
<
item
 


 
 
 
 
 
 
 
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:


<
?xml
version
=
"
1.0
"
encoding
=
"
utf-8
"
?
>
 


<
animation-list
xmlns:android
=
" http://schemas.android.com/apk/res/android "


 
 
 
android:oneshot
=
"
true
"
>
 


 
 
 
 
<
item
 


 
 
 
 
 
 
 
android:duration
=
"
80
"
 


 
 
 
 
 
 
 
android:drawable
=
"
@drawable/stat_menu
"
 
>
 


 
 
 
 
<
/item
>
 


 
 
 
 
<
item
 


 
 
 
 
 
 
 
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配置文件:

1  
<
?xml
version
=
"
1.0
"
encoding
=
"
utf-8
"
?
>
 

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=
"
wrap_content
"
 

44 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
wrap_content
"
 

45 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_gravity=
"
top
"
 
 

46 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:background
=
"
@layout/animation_menu
"
 

47 
 
 
 
 
 
 
 
 
 
 
 
 
 
/
>
 

48 

49 
 
 
 
 
 
 
 
 
 
 
 
 
<
ImageView
 

50 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:id
=
"
@+id/status_back
"
 

51 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_width=
"
wrap_content
"
 

52 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
wrap_content
"
 

53 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_gravity=
"
top
"
 
 

54 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:background
=
"
@layout/animation_back
"
 

55 
 
 
 
 
 
 
 
 
 
 
 
 
 
/
>
 

56 
 
 
 
 
 
 
 
 
<!--
Ethan.zhao
 
:
 
End
 
Add
 
three
 
button
 
on
 
status_bar
-->
 
 

57 
 
 
 
 
<
/LinearLayout
>
 

58 
 
 
 
 
<
LinearLayout
android:id
=
"
@+id/ticker
"
 

59 
 
 
 
 
 
 
 
android:layout
_width=
"
fill_parent
"
 

60 
 
 
 
 
 
 
 
android:layout
_height=
"
fill_parent
"
 

61 
 
 
 
 
 
 
 
android:paddingLeft
=
"
6dip
"
 

62 
 
 
 
 
 
 
 
android:animationCache
=
"
false
"
 

63 
 
 
 
 
 
 
 
android:orientation
=
"
horizontal
"
 
>
 

64 
 
 
 
 
 
 
 
 
<
ImageSwitcher
android:id
=
"
@+id/tickerIcon
"
 

65 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_width=
"
wrap_content
"
 

66 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
fill_parent
"
 

67 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_marginRight=
"
8dip
"
 

68 
 
 
 
 
 
 
 
 
 
 
 
 
>
 

69 
 
 
 
 
 
 
 
 
 
 
 
 
<
com.android.server.status.AnimatedImageView
 

70 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_width=
"
25dip
"
 

71 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
25dip
"
 

72 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/
>
 

73 
 
 
 
 
 
 
 
 
 
 
 
 
<
com.android.server.status.AnimatedImageView
 

74 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_width=
"
25dip
"
 

75 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
25dip
"
 

76 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/
>
 

77 
 
 
 
 
 
 
 
 
<
/ImageSwitcher
>
 

78 
 
 
 
 
 
 
 
 
<
com.android.server.status.TickerView
android:id
=
"
@+id/tickerText
"
 

79 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_width=
"
0dip
"
 

80 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_weight=
"
1
"
 

81 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
wrap_content
"
 

82 
 
 
 
 
 
 
 
 
 
 
 
android:paddingTop
=
"
2dip
"
 

83 
 
 
 
 
 
 
 
 
 
 
 
android:paddingRight
=
"
10dip
"
>
 

84 
 
 
 
 
 
 
 
 
 
 
 
 
<
TextView
 

85 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_width=
"
fill_parent
"
 

86 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
wrap_content
"
 

87 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:singleLine
=
"
true
"
 

88 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:textColor
=
"
#ff000000
"
 
/
>
 

89 
 
 
 
 
 
 
 
 
 
 
 
 
<
TextView
 

90 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_width=
"
fill_parent
"
 

91 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:layout
_height=
"
wrap_content
"
 

92 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:singleLine
=
"
true
"
 

93 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
android:textColor
=
"
#ff000000
"
 
/
>
 

94 
 
 
 
 
 
 
 
 
<
/com.android.server.status.TickerView
>
 

95 
 
 
 
 
<
/LinearLayout
>
 

96 

97 
 
 
 
 
<
com.android.server.status.DateView
android:id
=
"
@+id/date
"
 

98 
 
 
 
 
 
 
 
android:layout
_width=
"
wrap_content
"
 

99 
 
 
 
 
 
 
 
android:layout
_height=
"
fill_parent
"
 

100
 
 
 
 
 
 
 
android:singleLine
=
"
true
"
 

101
 
 
 
 
 
 
 
android:textSize
=
"
16sp
"
 

102
 
 
 
 
 
 
 
android:textStyle
=
"
bold
"
 

103
 
 
 
 
 
 
 
android:gravity
=
"
center_vertical|left
"
 

104
 
 
 
 
 
 
 
android:paddingLeft
=
"
6px
"
 

105
 
 
 
 
 
 
 
android:paddingRight
=
"
6px
"
 

106
 
 
 
 
 
 
 
android:textColor
=
"
?android:attr/textColorPrimaryInverse
"
 

107
 
 
 
 
 
 
 
android:background
=
"
@drawable/statusbar_background
"
 

108
 
 
 
 
 
 
 
 
/
>
 

109
<
/com.android.server.status.StatusBarView
>

c . 修改StatusBarView.java文件,加入Animation相关代码:

添加三个AnimationDrawable:

1
AnimationDrawable
 
mHomeAnimation;
 

2
AnimationDrawable
 
mBackAnimation;
 

3
AnimationDrawable
 
mMenuAnimation;

在onFinishInflate()方法中

1
mHomeAnimation
 
=
 
(AnimationDrawable)
 
mHomeIcon
.
getBackground();
 

2
mBackAnimation
 
=
 
(AnimationDrawable)
 
mBackIcon
.
getBackground();
 

3
mMenuAnimation
 
=
 
(AnimationDrawable)
 
mMenuIcon
.
getBackground();

updateResvKeyIcon这个方法,改造成如下这样:


private

 
int

 
updateResvKeyIcon(
int

 
key)
 
 


{
 
 


 
 
 
 
if

(key
 
=
=
 
RESV_KEY_BACK)


 
 
 
 
{
 
 


 
 
 
 
 
 
 
 
mBackAnimation
.
run();
 


 
 
 
 
}


 
 
 
 
else

 
if

(key
 
=
=
 
RESV_KEY_HOME)


 
 
 
 
{
 
 


 
 
 
 
 
 
 
 
mHomeAnimation
.
run();
 

10
 
 
 
 
}

11
 
 
 
 
else

 
if

(key
 
=
=
 
RESV_KEY_MENU)

12
 
 
 
 
{
 
 

13
 
 
 
 
 
 
 
 
mMenuAnimation
.
run();
 

14
 
 
 
 
}
 
 

15
 
 
 
 
return

 
0

;
 
 

16
}
 
 

d. 注释掉所有的updateResvKeyIcon方法,仅保留以下一个,并加上判断语句。


if(mResvKeyState
 
=
=
 
-
1

)
 
//
 
remembered
 
key
 
state,
 
no
 
reserve
 
 


  
 
 
 
{
 
 


  
 
 
 
   
switch

(getResvKeyArea(event))
{
 
 


  
 
 
 
   
 
 
 
 
 
 
case

 
RESV_KEY_HOME
:
 
 


  
 
 
 
   
 
 
 
 
 
 
case

 
RESV_KEY_BACK
:
 
 


  
 
 
 
   
 
 
 
 
 
 
case

 
RESV_KEY_MENU
:
 
 


  
 
 
 
   
 
 
 
 
 
 
{
 
 


  
 
 
 
   
 
 
 
 
 
 
 
 
 
mResvKeyState
 
=
 
event
.
getAction();
 
 


  
 
 
 
   
 
 
 
 
 
 
 
 
 
mResvKeyCode
 
 
=
 
getResvKeyArea(event);
 
 

10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
i
f

 
(mResvKeyState
=
=
MotionEvent
.
ACTION_DOWN)
 
updateResvKeyIcon(mResvKeyCode);
//
这句改成这样

 
 
附件下载如下:
http://download.csdn.net/source/2807668
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息