實現思路:
WindowManagerService循環讀取下面按鍵消息並分發給窗口,在消息分發前會在PhoneWindowManager.interceptKeyBeforeQueueing方法中進行消息的過濾。所以該實現方式爲在消息分發前的interceptKeyBeforeQueueing方法中監聽當前按鍵爲音量下鍵,若是當前狀態爲鎖屏狀態,並按鍵爲音量下鍵,且兩次按鍵間隔時間小於800ms時發送抓拍廣播--"com.bill.snapshot"。
具體實現方式以下:
/framework/base/policy/src/com/android/policy/impl/PhoneWindowManager.java
private long lastTime=0;
public int interceptKeybeforeQueueing(KeyEvent event,int policyFlags,boolean isScreenOn){
if(event.getKeyCode()==KeyEvent.KEYCODE_VOLUME_DOWN && event.isDwon()){
if(isKeyguardLocked()){
if((System.currentTimeMillis()-lastTime) < 800){
Intent intent=new Intent("com.bill.snapshot");
mContext.sendBroadcast(intent);
}
lastTime=System.currentTimeMillis();
}
}
......
}