最近在写一个锁屏应用,来代替系统默认的锁屏。
大概的实现的方法是创建一个service来响应屏幕变黑的广播(例如自己按下power键的时候),接受到广播之后,创建并显示自己的锁屏activity,其中要通过KeyguardManager来关掉系统的默认锁屏。
然后是屏蔽home键,在网上搜索到的方法是重写onAttachedToWindow(),并加入this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
然后才可以在onKeyDown中捕获到home键。
问题来了,我在虚拟机上运行没问题,home键可以捕捉到,但是在我的手机上调试的时候(手机是MIUI android2.3.7的系统),却不能捕获home键,难道是MIUI修改了android源码,使得home键不能通过以上的方法截获?
------解决方案--------------------
给楼主朋友提供另一种解决方式
就是使用
public void onPause(){
super.onPause();
};
这个事件处理
------解决方案--------------------
HOME键你的收不到事件的。这个不用想了,除非你手机的BSP厂商在HOME键按下的时候,发了相应的broadcast。
虽然系统的key code定义了HOME的值,但上层的APP是收不到的。
------解决方案--------------------
还有,这是android这样设计的,因为考虑到完全性,HOME就是直接回到lanucher,所以google认为通常不需要APP去做特别的事情。
如果你实在想知道home键的事件处理,你可以参考系统的源码:
Frameworks\base\policy\src\com\android\internal\policy\impl\PhoneWindowManger.java
的interceptKeyBeforeDispatching方法。
它有如下代码:
if (mHomePressed) {
// If we have released the home key, and didn't do anything else
// while it was pressed, then it is time to go home!
if (keyCode == KeyEvent.KEYCODE_HOME) {
if (!down) {
mHomePressed = false;
if (!canceled) {
// If an incoming call is ringing, HOME is totally disabled.
// (The user is already on the InCallScreen at this point,
// and his ONLY options are to answer or reject the call.)
boolean incomingRinging = false;
try {
ITelephony telephonyService = getTelephonyService();
if (telephonyService != null) {
incomingRinging = telephonyService.isRinging();
}
} catch (RemoteException ex) {
Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
}
if (incomingRinging) {
Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
} else {
launchHomeFromHotKey();
}