[color=blue]在Activity类onKeyDown方法中进入拦截事件[/color]
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("key down", "key code:"+event.getKeyCode());
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (getApplicationInfo().targetSdkVersion
>= Build.VERSION_CODES.ECLAIR) {
event.startTracking();
} else {
onBackPressed();
}
return true;
}else if(keyCode == 7){ [color=blue](快捷键code=7,定位到桌面时钟)[/color]
ComponentName cmp = new ComponentName("com.android.deskclock", "com.android.deskclock.DeskClock");
Intent intent = new Intent();
intent.setComponent(cmp);
startActivity(intent);
return true;
}else if(keyCode == 42){
ComponentName cmp = new ComponentName("com.autonavi.routedemo", "com.autonavi.routedemo.Main");
Intent intent = new Intent();
intent.setComponent(cmp);
startActivity(intent);
return true;
}else if(keyCode == 31){[color=blue](快捷键code=31,定位到日历)[/color]
ComponentName cmp = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
Intent intent = new Intent();
intent.setComponent(cmp);
startActivity(intent);
return true;
}else if(keyCode == KeyEvent.KEYCODE_X){[color=blue](快捷键X,静音/取消静音)[/color]
Log.i("activity onkeydown", "you have clicked the x key");
AudioManager mAudioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
Log.i("activity onkeydown RingerMode before:", ""+mAudioManager.getRingerMode());
boolean state = ((mAudioManager.getRingerMode()&2)==0)?false:true;
Log.i("activity onkeydown state", ""+state);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,state);
mAudioManager.setRingerMode(state==false?2:1);
Log.i("activity onkeydown RingerMode after:", ""+mAudioManager.getRingerMode());
return true;
}else if(keyCode == KeyEvent.KEYCODE_EQUALS){[color=blue](快速启动播放器)[/color]
Log.i("activity onkeydown", "you have clicked the KEYCODE_EQUALS key:"+KeyEvent.KEYCODE_EQUALS);
AudioManager mAudioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,false);
mAudioManager.setRingerMode(2);
mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
}else if(keyCode == KeyEvent.KEYCODE_MINUS){
Log.i("activity onkeydown", "you have clicked the KEYCODE_MINUS key:"+KeyEvent.KEYCODE_MINUS);
AudioManager mAudioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
mAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("key down", "key code:"+event.getKeyCode());
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (getApplicationInfo().targetSdkVersion
>= Build.VERSION_CODES.ECLAIR) {
event.startTracking();
} else {
onBackPressed();
}
return true;
}else if(keyCode == 7){ [color=blue](快捷键code=7,定位到桌面时钟)[/color]
ComponentName cmp = new ComponentName("com.android.deskclock", "com.android.deskclock.DeskClock");
Intent intent = new Intent();
intent.setComponent(cmp);
startActivity(intent);
return true;
}else if(keyCode == 42){
ComponentName cmp = new ComponentName("com.autonavi.routedemo", "com.autonavi.routedemo.Main");
Intent intent = new Intent();
intent.setComponent(cmp);
startActivity(intent);
return true;
}else if(keyCode == 31){[color=blue](快捷键code=31,定位到日历)[/color]
ComponentName cmp = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
Intent intent = new Intent();
intent.setComponent(cmp);
startActivity(intent);
return true;
}else if(keyCode == KeyEvent.KEYCODE_X){[color=blue](快捷键X,静音/取消静音)[/color]
Log.i("activity onkeydown", "you have clicked the x key");
AudioManager mAudioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
Log.i("activity onkeydown RingerMode before:", ""+mAudioManager.getRingerMode());
boolean state = ((mAudioManager.getRingerMode()&2)==0)?false:true;
Log.i("activity onkeydown state", ""+state);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,state);
mAudioManager.setRingerMode(state==false?2:1);
Log.i("activity onkeydown RingerMode after:", ""+mAudioManager.getRingerMode());
return true;
}else if(keyCode == KeyEvent.KEYCODE_EQUALS){[color=blue](快速启动播放器)[/color]
Log.i("activity onkeydown", "you have clicked the KEYCODE_EQUALS key:"+KeyEvent.KEYCODE_EQUALS);
AudioManager mAudioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,false);
mAudioManager.setRingerMode(2);
mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
}else if(keyCode == KeyEvent.KEYCODE_MINUS){
Log.i("activity onkeydown", "you have clicked the KEYCODE_MINUS key:"+KeyEvent.KEYCODE_MINUS);
AudioManager mAudioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
mAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
}