如何禁用主页键

pen*_*ang 33 android android-homebutton

我想锁定屏幕.我想禁用主页键,只使用后退键.我该如何做到这一点?

ASH*_*ASH 30

使用此方法禁用Android中的Home键

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}
Run Code Online (Sandbox Code Playgroud)

  • 它抛出非法状态异常的例外 (6认同)
  • 这似乎不适用于现代Android版本.它会抛出一个异常,即在添加窗口后无法更改窗口的类型. (6认同)
  • 这是否适用于全屏SurfaceView和MediaPlayer?好像它没有.哦,我明白了:在这种情况下TYPE_KEYGUARD_DIALOG有效. (5认同)
  • 通知栏仍然显示,并且在关闭通知栏后,按主页键返回主屏幕.有什么工作吗?Theme.Black.NoTitleBar和requestWindowFeature(Window.FEATURE_NO_TITLE)不起作用. (2认同)

ami*_*ser 22

我找到了解决HOME键的方法.对于您的应用程序,将清单设置为

<action android:name="android.intent.action.MAIN" />              
<category android:name="android.intent.category.HOME" />                 
<category android:name="android.intent.category.DEFAULT" />               
<category android:name="android.intent.category.MONKEY"/>
Run Code Online (Sandbox Code Playgroud)

现在您的应用程序是备用Launcher应用程序.

使用adb,并使用包管理器禁用启动器应用程序

pm disable com.android.launcher2
Run Code Online (Sandbox Code Playgroud)

现在Home键按下将始终保持在同一屏幕中.


And*_*lva 13

此解决方案仅适用于3.x.

好吧,这应该是一个难题.但这是破解它的方法.

在您的Activity中覆盖以下方法,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}
Run Code Online (Sandbox Code Playgroud)

现在处理这样的关键事件,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     Log.i("Home Button","Clicked");
    }
   if(keyCode==KeyEvent.KEYCODE_BACK)
   {
        finish();
   }
 return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 我的应用程序崩溃了:java.lang.IllegalArgumentException:添加窗口后无法更改窗口类型. (4认同)

Sta*_* BZ 8

将此代码添加到您的MainActivity班级:

Timer timer;
MyTimerTask myTimerTask;

@Override
protected void onResume() {
    super.onResume();

    if (timer != null) {
        timer.cancel();
        timer = null;
    }
}

@Override
protected void onPause()
{
    if (timer == null) {
        myTimerTask = new MyTimerTask();
        timer = new Timer();
        timer.schedule(myTimerTask, 100, 100);
    }

    super.onPause();
}

private void bringApplicationToFront()
{
    KeyguardManager myKeyManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    if( myKeyManager.inKeyguardRestrictedInputMode())
        return;

    Log.d("TAG", "====Bringging Application to Front====");

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    try
    {
        pendingIntent.send();
    }
    catch (PendingIntent.CanceledException e)
    {
        e.printStackTrace();
    }
}

public void onBackPressed() {
    // do not call super onBackPressed.
}

class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        bringApplicationToFront();
    }
}
Run Code Online (Sandbox Code Playgroud)

这不是"主页"按钮的锁定,但用户无法长时间(超过100毫秒)切换到另一个应用程序,也许这就是你想要的.