1

I use a service to launch applications by gestures. That works fine. But when I press the homebutton (doesn't matter when and where) then the launch of the app (right after pressing the homebutton) from the service takes about 2-3 seconds. It is also possible to launch the app 2-3 seconds after pressing the homebutton, then the app starts immediately. Another way is to open a random app before launching an app from the service. Then there also is no delay.

To summarize:

After pressing the homebutton at anytime, a launch of an app from the service (right after pressing the homebutton) with the code below, needs about 2-3 seconds. Launching the app 2-3 seconds after pressing the homebutton launches the app immediately.

startActivity(getPackageManager().getLaunchIntentForPackage(item));
Toast.makeText(getApplicationContext(), "TestTest", Toast.LENGTH_SHORT).show(); //just for testing  

The Toast-Message (right after the startActivity-Command) always shows up immediately, so the service is working all the time, even right after pressing the hombutton.

That problem is really strange. How can the homebutten "block" the startActivity-Command in the service for a few seconds?

Any ideas? Thx

2 Answers2

0

I have the same problem.

The only thing I managed to discover is that the waiting period is caused by the thread named waitForActivityStart. That is what you can see if you start method profiling with ddms.

The strange thing about it is that in that thread nothing gets executed - no cpu time is used. It simply waits for a few seconds - or at least thats how it looks like.

I am sorry I cant provide the complete solution, I hope this can help someone to continue with the research, since I am stuck here also.

tzrnee
  • 55
  • 4
  • 1
    Thx for this useful answer. I researched a lot the last days and I found an interesting link: http://stackoverflow.com/questions/5600084/starting-an-activity-from-a-service-after-home-button-pressed-without-the-5-seco It looks like it's not that easy to avoid the delay time. – user3284588 Feb 15 '14 at 14:46
  • I see. So that is exactly what happenes, as I described it: there is this thread that actually does nothing, simply waits for a few seconds. – tzrnee Feb 17 '14 at 10:26
-2

not sure, first make your activity launch mode single task.and when your activity goes background then call the service method to launch it again.

User10001
  • 1,295
  • 2
  • 18
  • 30