简体   繁体   中英

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.

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 ). The Activity shows up after 5 seconds.

I didn't find any solution on previous SO questions. However, there's an app, Link Bubble , which overcomes this problem. 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:

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) 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.

One possible workaround for this issue is to add the Home/Launcher intent filter to the activity that you are trying to start. 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(). This would bypass the 5 second delay. 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.

android:launchMode="singleInstance"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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