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

Solution 1:

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();
                }

Solution 2:

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