#define KEY_MUTE113
#define KEY_F1 59
#define KEY_F2 60
#define KEY_BRIGHTNESSDOWN 224
#define KEY_BRIGHTNESSUP 225
3>.framework/core/res/res/values/attrs.xml
此文件为linux映射到应用的值,以供应用程序调用
<enum name="KEYCODE_F1" value="131" />
<enum name="KEYCODE_F2" value="132" />
<enum name="KEYCODE_VOLUME_MUTE" value="164" />
<!--add by huanggenda-->
<enum name="KEY_BRIGHTNESSDOWN" value="211" />
<enum name="KEY_BRIGHTNESSUP" value="212" />
<!--the end add by huanggenda-->
4>.在framework/base/policy/src/com/android/impl/PhoneWindowManager.java
按键code察看调试:
//modify false to true for debug
if (true) {
Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " down=" + down + " repeatCount="
+ repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed);
}
//The function of F2 and F1 add by huanggenda
if(keyCode == KeyEvent.KEYCODE_F2)
Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " F2");
if(keyCode == KeyEvent.KEYCODE_F3)
Log.d(TAG, "interceptKeyTi keyCode=" + keyCode +" F3");
if(keyCode == KeyEvent.KEYCODE_BRIGHTNESSDOWN)
Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " BRIGHTNESSDOWN");
if(keyCode == KeyEvent.KEYCODE_BRIGHTNESSUP)
Log.d(TAG, "interceptKeyTi keyCode=" + keyCode +" BRIGHTNESSUP");
//The end by huanggenda
5>.framework/base/media/java/android/media/AudioManager.java
处理音量键方法:
/**
* @hide
*/
public void handleKeyDown(int keyCode, int stream) {
boolean mute = isStreamMute(AudioManager.STREAM_MUSIC);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
/* *
* if audio is mute, turn off mute.
* Add by huanggenda
* */
if (mute) {
setStreamMute(AudioManager.STREAM_MUSIC, false);
int volume = getStreamVolume(AudioManager.STREAM_MUSIC);
if (volume == 0) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
setStreamMute(AudioManager.STREAM_MUSIC, true);
break;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
setStreamMute(AudioManager.STREAM_SYSTEM, false);
setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
}
} else {
setStreamMute(AudioManager.STREAM_SYSTEM, false);
setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_SAME,
AudioManager.FLAG_SHOW_UI);
break;
}
}
/* The end to add by huanggenda */
/*
* Adjust the volume in on key down since it is more
* responsive to the user.
*/
int flags = FLAG_SHOW_UI | FLAG_VIBRATE;
if (mVolumeControlStream != -1) {
stream = mVolumeControlStream;
flags |= FLAG_FORCE_STREAM;
}
adjustSuggestedStreamVolume(
keyCode == KeyEvent.KEYCODE_VOLUME_UP
? ADJUST_RAISE
: ADJUST_LOWER,
stream,
flags);
/* *
* If the volume is 0, turn on mute.
* Add by huanggenda
* */
if (stream == AudioManager.STREAM_MUSIC) {
int v = getStreamVolume(AudioManager.STREAM_MUSIC);
mute = isStreamMute(AudioManager.STREAM_MUSIC);
if (!mute && (v == 0)) {
setStreamMute(AudioManager.STREAM_SYSTEM, true);
setStreamMute(AudioManager.STREAM_MUSIC, true);
setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
adjustStreamVolume(stream,
AudioManager.ADJUST_SAME,
AudioManager.FLAG_SHOW_UI);
}
}
/* The end to add by huanggenda */
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
// TODO: Actually handle MUTE.
mute = !mute;
setStreamMute(AudioManager.STREAM_SYSTEM, mute);
setStreamMute(AudioManager.STREAM_MUSIC, mute);
setStreamMute(AudioManager.STREAM_NOTIFICATION, mute);
adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_SAME,
AudioManager.FLAG_SHOW_UI);
break;
}
}