2.* 版本:
主要就是重写
onAttachedToWindow
() 和
onKeyDown
(...,...) 这两个方法。
前者是起到拦截作用的,后者是监听到HOME按键事件后的动作,如果没动作可以不要 onKeyDown。
还要注意,要加权限:
android.permission.DISABLE_KEYGUARD
">
// 转载请注明出处: http://aking86.iteye.com/admin/blogs/1317186
虽然网上有很多,但介绍的都不是很详细,也不算深入,我在这里研究了下,并把日志给捞了出来。
里面包括 HOME拦截, BACK拦截,打捞日志。
@Override
public void onAttachedToWindow
() {
System.out.println("Page01 -->onAttachedToWindow");
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
@Override
public boolean onKeyDown
(int keyCode, KeyEvent event) {
System.out.println("Page01 -->onKeyDown: keyCode: " + keyCode);
if (KeyEvent.KEYCODE_HOME
== keyCode) {
System.out.println("HOME has been pressed yet ...");
// android.os.Process.killProcess(android.os.Process.myPid());
Toast.makeText(getApplicationContext(), "按了HOME 键...",
Toast.LENGTH_LONG).show();
} else if (KeyEvent.KEYCODE_BACK
== keyCode) {
System.out.println("BACK has been pressed yet ...");
//按了返回
}
return super.onKeyDown(keyCode, event); // 不会回到 home 页面
}
通过打印日志,还得出一个结论:
onAttachedToWindow() 方法只会执行一次,也就是在第一次 onResume() 之后,以后就不再执行了。
4.*版本:
在AndroidManifest.xml里面加上权限
- public class LockLayer {
- private Activity mActivty;
- private WindowManager mWindowManager;
- private View mLockView;
- private LayoutParams mLockViewLayoutParams;
- private static LockLayer mLockLayer;
- private boolean isLocked;
-
- public static synchronized LockLayer getInstance(Activity act){
- if(mLockLayer == null){
- mLockLayer = new LockLayer(act);
- }
- return mLockLayer;
- }
-
- private LockLayer(Activity act) {
- mActivty = act;
- init();
- }
-
- private void init(){
- isLocked = false;
- mWindowManager = mActivty.getWindowManager();
- mLockViewLayoutParams = new LayoutParams();
- mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;
- mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;
-
- mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
-
- mLockViewLayoutParams.flags = 1280;
- }
- public synchronized void lock() {
- if(mLockView!=null&&!isLocked){
- mWindowManager.addView(mLockView, mLockViewLayoutParams);
- }
- isLocked = true;
- }
- public synchronized void unlock() {
- if(mWindowManager!=null&&isLocked){
- mWindowManager.removeView(mLockView);
- }
- isLocked = false;
- }
- public synchronized void setLockView(View v){
- mLockView = v;
- }
- }
还有个问题是mLockViewLayoutParams.flags = 1280;1280这个值具体是哪个变量还不知道,有知道的留个言
阅读(1601) | 评论(0) | 转发(0) |