简体   繁体   中英

Starting an activity from a service after HOME button pressed without the 5 seconds delay

I'm experiencing the problem described in this Android issue: http://code.google.com/p/android/issues/detail?id=4536

Simply put, after pressing the HOME button, android prevents services and broadcast-receivers from calling startActivity for 5 seconds.

I've also noticed that (well, theoretically), having the following permission :

"android.permission.STOP_APP_SWITCHES"

allows you to call resumeAppSwitches (which is defined in ActivityManagerService). Looking at the latest version of ActivityManagerService, this code is removed.

The question: How to launch an activity using startActivity without this 5 second delay?

Here is a solution I found.

Put your intent that you want to start immediately in a PendingIntent, then call the send() Method on it.

So instead of this

Intent intent = new Intent(context, A.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

just do this

Intent intent = new Intent(context, A.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent pendingIntent =
                        PendingIntent.getActivity(context, 0, intent, 0);
                try {
                    pendingIntent.send();
                } catch (PendingIntent.CanceledException e) {
                    e.printStackTrace();
                }

I don't think there is a way to do it with the current APIs. I think that is how they intended it to work so that an app cannot force itself back open when the user exits with a home key press. You could add the home/ launcher intent to the filter for whatever activity it is you are trying to start. Then the user would have the choice to basically treat that app as though it is a homescreen. Then it would get launched with no delay at all whenever the user presses the home button(They'd have to select it from the list that will pop up asking which app they want to use to complete this action, but they could check always use this app to take this step away in the future.)

I am using AlarmManager to start Activity immediatly from Service. Activity starts without delay, even if you have pressed home button before.

Tested on Android 5.0.1 (Galaxy Alpha).

Don't work at 6.0.1 (Nexus 7 2013) :-(

Don't work at 4.1.2 (Galaxy S II) :-(

Don't work at 4.3 (ASUS MeMO Pad FHD 10 ME302C) :-(

@TargetApi(Build.VERSION_CODES.KITKAT)
private void startActivity() {

    Intent intent = new Intent(this, Main.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    long now = Calendar.getInstance().getTimeInMillis();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, now, pendingIntent);
    else
        alarmManager.set(AlarmManager.RTC_WAKEUP, now, pendingIntent);
}

I am intrigued by this "feature" also and how to avoid it. Reading the post: http://code.google.com/p/android/issues/detail?id=4536 (read the comment #10).

I quote the relevant part below:

Workarounds are:

1) Don't use an activity, do everything from a service.

2) Have some kind of intermediate Home (WidgetLocker HomeHelper, QuickDesk, PowerStrip, etc). These do a startActivity immediate to start the "real" Home and this bypasses the 5 second rule. This is a bad idea as Android prioritizes keeping the system Home in memory but not whatever secondary Home the intermediate set. So it can lead to Launcher reloads which is no fun. Plus it's very confusing to users.

3) Root can start activities during this period.

Among those, I believe the best way to do it is to create a "Home Helper"-like activity . So, instead of starting a new activity, you would call this one instead . This is specially true, since you are creating a launcher app.

As I said in my previous comment to the question, I would contact the WidgetLocker developer about it. Alternatively, you can use APK Manager to see how he implemented it (he even encouraged the use of the APK Manager to create different mods to his app, the link to the xda-developers thread is in the comment above)

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