Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

like image 847
Lior Avatar asked Apr 08 '11 19:04

Lior


People also ask

What happens to activity when home button is pressed?

When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.

Which method is not called when we press the Home button from an activity?

Note: onDestroy() method not call after press Home Button.


1 Answers

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();                 } 
like image 104
Chinibin Avatar answered Sep 23 '22 12:09

Chinibin