当前位置:操作系统 > 安卓/Android >>

如何在Android App中屏蔽(拦截)Home按键及其他按键

编写一个Acitivity,如下所示:
关键点1:屏蔽HOME按键,在onAttachedToWindow中设置FLAG_HOMEKEY_DISPATCHED
关键点2:屏蔽除Power , Home外的按键,覆盖dispatchKeyEvent方法,并返回为true
public class LockByKeyGuard extends Activity {
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
       }
       @Override
       public void onAttachedToWindow() {
       //关键:在onAttachedToWindow中设置FLAG_HOMEKEY_DISPATCHED
              this.getWindow().addFlags(WindowManager.LayoutParams. FLAG_HOMEKEY_DISPATCHED);
              super.onAttachedToWindow();
       }
 
       @Override
       public boolean dispatchKeyEvent(KeyEvent event) {
              // 返回true,不响应其他key
              return true;
       }
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,