简体   繁体   English

按主页按钮后从系统覆盖窗口启动活动

[英]Starting an Activity from a system overlay window after pressing home button

I have a system overlay window, a floating view like Facebook's Chat Head. 我有一个系统覆盖窗口,像Facebook的聊天头一样的浮动视图。

When user presses the window, an Activity will be started to show the content. 当用户按下窗口时,将启动一个活动以显示内容。

The problem is that if the user leave my app by pressing home button, then the Activity cannot be started within 5 seconds due to system restriction ( https://code.google.com/p/android/issues/detail?id=4536 ). 问题是,如果用户通过按主页按钮离开我的应用程序,则由于系统限制,无法在5秒内启动活动( https://code.google.com/p/android/issues/detail?id=4536 )。 The Activity shows up after 5 seconds. 活动在5秒后显示。

I didn't find any solution on previous SO questions. 我之前的SO问题没有找到任何解决方案。 However, there's an app, Link Bubble , which overcomes this problem. 然而,有一个应用程序, Link Bubble ,它克服了这个问题。 When user presses the floating bubble view, an Activity can always popup immediately. 当用户按下浮动气泡视图时,活动总是可以立即弹出。

Does anyone know how to make this? 有谁知道如何做到这一点?

This is LayoutParams of my system overlay window: 这是我系统覆盖窗口的LayoutParams:

windowParams = new WindowManager.LayoutParams(
            width, height,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | 
               WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
                   WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
            PixelFormat.TRANSLUCENT);

windowParams.gravity = Gravity.TOP | Gravity.LEFT;
windowParams.x = 0;
windowParams.y = 0;

I got WindowManager with 我得到了WindowManager

windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);

and add the floating view with 并添加浮动视图

windowManager.addView(MY_VIEW, windowParams);

where MY_VIEW has an OnTouchListener that starts an Activity after user pressed it. 其中MY_VIEW有一个OnTouchListener,在用户按下它后启动一个Activity。

One possible workaround for this issue is to add the Home/Launcher intent filter to the activity that you are trying to start. 此问题的一种可能解决方法是将Home / Launcher intent过滤器添加到您尝试启动的活动。 You would do this by adding this 你可以通过添加它来做到这一点

<intent-filter>

    <!-- The following two intent-filters are the key to set homescreen -->
    <category android:name="android.intent.category.HOME" />   
    <category android:name="android.intent.category.DEFAULT" /> 


</intent-filter>

When you do this, the user will basically have the option of using the app as a home screen. 执行此操作时,用户基本上可以选择将应用程序用作主屏幕。 This would then result in no delay whatsoever whenever the activity is started. 无论何时开始活动,这都将导致无延迟。 This is the closest unrooted solution to this problem. 这是此问题最接近无根的解决方案。

Update 更新

Another possible solution is to have an intermediate helper activity which in turn has a startActivity(). 另一种可能的解决方案是具有中间辅助活动,该辅助活动又具有startActivity()。 This would bypass the 5 second delay. 这将绕过5秒的延迟。 Make sure that the intermediate activity has a transparent layout to avoid confusion for the user. 确保中间活动具有透明布局,以避免混淆用户。

First, add this in your launcher activity. 首先,在启动器活动中添加此项。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(false);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Then, add this to your overlay activity in AndroidManifest.xml. 然后,将其添加到AndroidManifest.xml中的叠加层活动中。

android:launchMode="singleInstance"

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

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