Android如何屏蔽home键和recent键

最近在做一个项目的时候,进入一个 Activity后需要暂时屏蔽掉home键和recent键(back键可以在onKeyDown里面处理), 上找了半天,都是针对旧版本android的方法,android5.0以后就不能生效了。


看了半天的SDK和资料,发现有一种方法可以暂时屏蔽掉home键和recent键,如果有办法获取到高级的系统权限,还可以正在的屏蔽掉。废话不多说,直接上代码:

其实代码就一句话:startLockTask,整段代码如下:

import android.app.Activity;

  • import android.os.Bundle;
  • import android.view.KeyEvent;
  • import utils.bobo.com.boboutils.R;
  • public class ScreenPinningActivity extends Activity {
  • @Override
  • protected void onCreate(Bundle savedInstanceState) {
  • super.onCreate(savedInstanceState);
  • setContentView(R.layout.activity_screen_pinning);
  • }
  • @Override
  • public void onResume(){
  • super.onResume();
  • this.startLockTask();
  • }
  • @Override
  • public boolean onKeyDown(int keyCode,KeyEvent event){
  • if(keyCode==KeyEvent.KEYCODE_BACK)
  • return true;
  • return super.onKeyDown(keyCode, event);
  • }
  • }
  • 启动后,会出现如下图的提示,用户点击知道了,home键和recent键就被屏蔽掉了,只有长按back键才能解除这个状态:

            android_protectionLevel=”signature|privileged” />

    mStatusBarManager = (StatusBarManager) getApplicationContext().getSystemService(Context.STATUS_BAR_SERVICE);

    在activity 退出的时候要执行下


    LayoutParams params = new WindowManager.LayoutParams();

  • params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
  • params.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
  • params.width = WindowManager.LayoutParams.FILL_PARENT;
  • params.height = WindowManager.LayoutParams.FILL_PARENT;
  • params.format = PixelFormat.TRANSPARENT;
  • params.gravity=Gravity.LEFT|Gravity.TOP;
  • params.x = 0;
  • params.y = 0;
  • wm.addView(mView, params);


  • 声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

    上一篇 2017年4月22日
    下一篇 2017年4月22日

    相关推荐