【问题标题】:Disable home button - Android禁用主页按钮 - Android
【发布时间】:2015-03-19 18:18:47
【问题描述】:

我正在使用 VOIP 开发应用程序。拨打电话时,我不希望用户转到主屏幕。我通过覆盖 onbackpressed 方法禁用了单击后退按钮。但我无法弄清楚如何禁用主页按钮。

我试过了

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

但它给我抛出了异常。我希望代码适用于所有版本的 android。

提前致谢。

【问题讨论】:

标签: android


【解决方案1】:

实际上你不能这样做,电源按钮也是如此。它是系统按钮,用户必须始终可以访问,独立于应用程序的目的。想象一下,您的应用程序将卡住(滞后)或失去互联网连接。整个设备将被阻止,因为用户无法返回主菜单。用户的唯一方法 - 是关机设备。所以这被 Android 系统架构禁用了。即使默认通话和 voip 应用程序也不会这样做,也请尝试遵循此规则。 此外,许多供应商通过硬件对其按钮的实现方式不同,因此该按钮具有不同的行为。 至于替代方案,您可以将您的活动设置为全屏,并在拨打电话时显示用户警告消息,而不是关闭您的应用程序。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    可以,但您的应用必须由系统密钥签名。我反编译了一个向导应用程序并找到了一个禁用主页按钮的代码示例。我在我的向导应用程序中使用了这段代码,但我不能保证它会在任何地方都有效。我在使用 Android 5、6 和 7 的 STB 上检查了此代码。

        private void disableHomeButton(Context context){
        ContentResolver contentResolver = context.getContentResolver();
        try {
            if (Build.VERSION.SDK_INT < 17) {
                Settings.System.putInt(contentResolver, Settings.System.DEVICE_PROVISIONED, 0);
            } else {
                Settings.Global.putInt(contentResolver, Settings.Global.DEVICE_PROVISIONED, 0);
            }
            Settings.Secure.putInt(contentResolver, Settings.Secure.USER_SETUP_COMPLETE, 0);
        }
        catch(SecurityException e){
    
        }
    }
    
    private void enableHomeButton(Context context){
        ContentResolver contentResolver = context.getContentResolver();
        try {
            if (Build.VERSION.SDK_INT < 17) {
                Settings.System.putInt(contentResolver, Settings.System.DEVICE_PROVISIONED, 1);
            } else {
                Settings.Global.putInt(contentResolver, Settings.Global.DEVICE_PROVISIONED, 1);
            }
            Settings.Secure.putInt(contentResolver, Settings.Secure.USER_SETUP_COMPLETE, 1);
        }
        catch(SecurityException e){
    
        }
    }
    

    AndroidManifest.xml

    <manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="tv.test.wizard"
    android:sharedUserId="android.uid.system">
    
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    
    
    ....
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      相关资源
      最近更新 更多