如何禁用home键

发布于 2024-09-26 19:18:04 字数 40 浏览 1 评论 0原文

我想锁定屏幕。我想禁用主页键并仅使用返回键。我该如何实现这个目标?

I'd like to lock the screen. I want to disable the home key and only use the back key. How do I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

他不在意 2024-10-03 19:18:04

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

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

Use this method to disable the Home key in android

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}
遥远的绿洲 2024-10-03 19:18:04

我找到了解决 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"/>

现在您的应用程序是备用启动器应用程序。

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

pm disable com.android.launcher2

现在按 Home 键将始终保留在同一屏幕中。

I found a way to tackle the HOME key.
For your application set the manifest as

<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"/>

Now your application is an alternate Launcher application.

Use the adb, and disable the launcher application using package manager

pm disable com.android.launcher2

Now the Home key press will always stay in the same screen.

耶耶耶 2024-10-03 19:18:04

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

好吧,这本来应该是一个很难回答的问题。但这里有一个方法可以破解它。

在您的活动中覆盖以下方法,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

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

@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;
}

This solution works up to 3.x only.

Okay, this was supposed to be a hard question. But here is a way to crack it.

Override the below method in your Activity,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this,

@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;
}
堇色安年 2024-10-03 19:18:04

将此代码添加到您的 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();
    }
}

这不是锁定“home”按钮,而是用户无法长时间切换到另一个应用程序(超过 100 毫秒),也许这就是您的情况想。

Add this code to your MainActivity class:

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();
    }
}

It's not a locking for 'home' button, but user can't switch to another app for a long time (more than 100 milliseconds), maybe this is what you want.

宁愿没拥抱 2024-10-03 19:18:04

要禁用主页按钮,请尝试以下操作:

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

通知栏下拉的问题可以通过隐藏通知栏来解决,如下所示:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.xxxx);
    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
    ....
}

或在清单中为您的活动或应用程序设置全屏主题:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

To disable home button, try this:

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

Problem with notification bar pulled down can be solved by hiding notification bar like here:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.xxxx);
    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
    ....
}

or set fullscreen theme for your Activity or Application in manifest:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文