【问题标题】:How to handle home button issue in Android Launcher applications如何处理 Android Launcher 应用程序中的主页按钮问题
【发布时间】:2013-12-30 13:56:47
【问题描述】:

我正在创建一个示例锁屏应用程序,我必须覆盖主页按钮,在我研究了谷歌和 stackoverflow 之后,我得到了结果,做起来很复杂。这里我提到了我在我的应用程序中所做的事情,

  1. 使用广播接收器创建了一个服务,以在屏幕关闭时显示我的锁定屏幕。 - 工作正常。

  2. 为了覆盖主页、菜单、返回和搜索按钮,我使用了以下代码, 希望当应用程序仅成为启动器时我们可以覆盖主页按钮,因此在我的 manifest.xml 中添加了此代码。

        <intent-filter>
            <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.LAUNCHER" /> -->
    
        </intent-filter>
    

在我的活动中我也使用了这个代码

@Override
    public void onAttachedToWindow() {

            // TODO Auto-generated method stub
            this.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG
                            | WindowManager.LayoutParams.FLAG_FULLSCREEN);
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

        super.onAttachedToWindow();
    }

到目前为止,在我的示例应用程序中,我成功完成了上述操作。现在我的问题是,

当我解锁屏幕然后转到任何应用程序,然后单击设备主页按钮,我的锁定屏幕将出现。我厌倦了禁用此功能,但我不知道该怎么做,为此我使用了如下代码,

/* This should come from a preference that let's the user select an activity that can handle the HOME intent */
String packageName = "com.android.launcher";
String packageClass = "com.android.launcher2.Launcher";

Intent home_intent = new Intent(Intent.ACTION_MAIN);
home_intent.addCategory(Intent.CATEGORY_HOME);
home_intent.setComponent(new ComponentName(packageName, packageClass));
home_intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
/* Here you should catch the exception when the launcher has been uninstalled, and let the user save themselves by opening the Market or an app list or something. Users sometimes use root apps to uninstall the system launcher, so your fake launcher is all that is left. Might as well give the poor user a hand. */
startActivity(home_intent);

没有代码可以帮助我,我的确切需求是一旦我解锁屏幕,我需要显示默认主屏幕,直到屏幕关闭屏幕。有什么想法来处理这个问题吗?提前致谢。

【问题讨论】:

    标签: android android-homebutton


    【解决方案1】:

    试试这个解决方案,

    创建一个静态变量flag,当您收到屏幕关闭时的广播时,该变量设置为true

    现在在您的activity 中检查标志是否为真

    @Override
    public void onAttachedToWindow() {
        if(MyService.Flag == true){
        //Continue with your code ...
        //....
        }else{
            finish();
        }
    }
    

    或在适合您的 onCreate 上进行操作

    屏幕解锁后

        //Disable the flag
        MyService.Flag = false;
    

    现在,当您的用户单击 Home 按钮时,将调用活动并再次检查标志,如果其为 false,则调用 finish() 以关闭活动

    【讨论】:

    • 我在我的服务中初始化这个标志?
    • @Aerrow :当您在onReceive() 方法上的广播上接收到它时初始化它,然后在锁定屏幕启动时检查标志,当您按下主页按钮时也会调用您的活动,因此标志检查会帮助你
    • 会出现同样的问题,我做了以下,在MyService.Java中,我初始化了这个,boolean isReleased = false;在我的 onCreate() 我设置 isEnable = true;... 然后在我的活动中,如果 (MyService.isEnable == true) { this.getWindow().setType( WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG | WindowManager.LayoutParams.FLAG_FULLSCREEN) ; this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } 其他 { 完成(); }
    • @Aerrow :不要在onCreate 上将其设置为true,这显然没有效果。在锁屏的广播接收器中设置
    • Girish Nair:按照你的建议,我完全改变了,在我的 Receiver 类中我初始化了布尔值,在 OnReceive mentiod 中我设置为 true,@Override public void onReceive(Context context, Intent intent) { isEnable = true;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    相关资源
    最近更新 更多