简体   繁体   English

如何在android中禁用主页按钮?

[英]how to disable home button in android?

i want to disable android device home button when my app runs. 我希望在我的应用运行时禁用Android设备home button

i have tried following code but it din't help : 我试过以下代码,但它没有帮助:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            Toast.makeText(this, "You pressed the home button!",
                    Toast.LENGTH_LONG).show();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

     @Override
     protected void onNewIntent(Intent intent) {
     super.onNewIntent(intent);
     if (Intent.ACTION_MAIN.equals(intent.getAction())) {
     Log.i("MyLauncher", "onNewIntent: HOME Key");

     }
     }

I think this can be done in another style if suits your requirement. 如果符合您的要求,我认为这可以用另一种风格完成。 By creating your activity as home activity. 通过将您的活动创建为家庭活动。 If you want to disable home button and show your custom application activity as launcher when home button is pressed. 如果要禁用主页按钮,并在按下主页按钮时将自定义应用程序活动显示为启动器。 Just add these lines in manifest for that activity for which you want your launcher. 只需在清单中为要启动启动器的活动添加这些行。

<activity
            android:name="com.example.TempActivity"
            android:clearTaskOnLaunch="true"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:stateNotNeeded="true" >
             <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
</activity>

After adding and run your application. 添加并运行您的应用程序后。 if user presses the home button, Android will ask for which launcher you want your home. 如果用户按下主页按钮,Android将询问您想要哪个启动器。 Then your have to select your application launcher ALWAYS not ONLY ONCE. 然后你必须选择你的应用程序启动器,而不仅仅是一次。

if you want fully disable the user, so that he cant move to another screen then set theme to fullscreen with NoTitlebar. 如果你想完全禁用用户,那么他就无法移动到另一个屏幕,然后使用NoTitlebar将主题设置为全屏。

You can use Android-HomeKey-Locker to disable HOME KEY and other system keys(such as BACK KEY and MENU KEY) 您可以使用Android-HomeKey-Locker禁用HOME KEY和其他系统键(例如BACK KEY和MENU KEY)

NOTE 注意

It seems that the solution only works on devices with hard home key 似乎该解决方案仅适用于具有硬主键的设备

Hope this will help you in your application. 希望这会对您的申请有所帮助。 Thanks. 谢谢。

You cannot disable home button. 您无法禁用主页按钮。

There is no way to intercept the home button on Android, unless you make your app the home screen. 除非您将应用程序设置为主屏幕,否则无法拦截Android上的主页按钮。 This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit. 这是出于安全原因,因此恶意应用程序无法通过覆盖可以退出的所有按钮来接管您的设备。

Home button is one sure short way to navigate to home screen. 主页按钮是导航到主屏幕的一种简便方法。

If you want to handle the HOME button, implement a home screen. 如果要处理HOME按钮,请实现主屏幕。

Not able disable Home button on specific android devices 无法禁用特定Android设备上的主页按钮

Check the answer by commonsware 通过commonsware检查答案

You can use below method to disable the Home key in android: 您可以使用以下方法禁用Android中的Home键:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM