实现思路:
WindowManagerService循环读取下面按键消息并分发给窗口,在消息分发前会在PhoneWindowManager.interceptKeyBeforeQueueing方法中进行消息的过滤。因此该实现方式为在消息分发前的interceptKeyBeforeQueueing方法中监听当前按键为音量下键,如果当前状态为锁屏状态,并按键为音量下键,且两次按键间隔时间小于800ms时发送抓拍广播--"com.bill.snapshot"。
具体实现方式如下:
/framework/base/policy/src/com/android/policy/impl/PhoneWindowManager.java
private long lastTime=0;
private Keyguard mKeyguardManager;
private Keyguard getKeyguardManager(){
if(mKeyguardManager==null){
mKeyguardManager=(KeyguradManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
}
return mKeyguradManager;
}
public int interceptKeybeforeQueueing(KeyEvent event,int policyFlags,boolean isScreenOn){
if(event.getKeyCode()==KeyEvent.KEYCODE_VOLUME_DOWN && event.isDwon()){
if(getKeyguardManager().inKeyguardRestrictedInputMode()){
if((System.currentTimeMillis()-lastTime) < 800){
Intent intent=new Intent("com.bill.snapshot");
mContext.sendBroadcast(intent);
}
lastTime=System.currentTimeMillis();
}
}
......
}