Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

android - 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?

question from:https://stackoverflow.com/questions/5600084/starting-an-activity-from-a-service-after-home-button-pressed-without-the-5-seco

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...