6

I am developing a android application which is running as service to start

another applications at a specific condition.

There are many chances to use a function 'startActivity'.

I noticed that sometimes it takes too long time to launch another application using startActivity.

Please, see the next two cases.

case 1: Menu -> Back key -> Home -> startActivity

case 2: Menu -> Home key -> Home -> startActivity

In first case, there is no delay to launch a new application.

But in second case, it takes 3~5 secs to launch a new application.

I tried many times, but results was same.

Please help me to solve this problem.

Why does it take long time only in second case?

My code:

    public class LauncherService extends Service implements OnTouchListener {
long timeStamp;

 public void onCreate() {
     super.onCreate();
     Button mButton;
     mButton = new Button(this);
     mButton.setId(1);
     mButton.setOnTouchListener(this);

     mButton.setText("");

     WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSPARENT);

     mButton.setBackgroundColor(Color.argb(0x00, 0x00, 0x00, 0x00));

     WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
     wm.addView(mButton, params);
 }

 public void launchActivity(){
     String pkgname ="some package name";
     String comname ="some component name";
     ComponentName name = new ComponentName(pkgname, comname);

     Intent i=new Intent(Intent.ACTION_MAIN);

     i.addCategory(Intent.CATEGORY_LAUNCHER);
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |       Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
     i.setComponent(name);
     getApplication().startActivity(i);
 }

public boolean onTouch(View v, MotionEvent arg1) {
    switch(v.getId()){
        case 1:
            {
                long now = System.currentTimeMillis();
                if((now - timeStamp <= 500)){
                    launchActivity();
                    break;
                }
                timeStamp = now;

            }
            break;
    }
    return false;
}

public IBinder onBind(Intent arg0) {
    return null;
}

}

lunaticapple
  • 79
  • 1
  • 5
  • Your best bet is to show some code. Your terminology sounds like it might be a bit off and at least with code you may be better understood. BTW an activity encompasses a bit more than just a function. One thing a user can do. http://developer.android.com/reference/android/app/Activity.html Also, Welcome to Stackoverflow and I bet Seoul is freezing this time of the year. – Terrance Nov 07 '11 at 16:23
  • 2
    Thanks for your comment. I edited post with full source code. There is nothing to special with my code. Run as a service, just launch some application on catching a double touch motion on the screen. BTW winter is just around the corner in Seoul. It is getting colder. – lunaticapple Nov 08 '11 at 00:43
  • 1
    I found something... In second case, if I wait about 5secs after entering a home screen and start a new application, there is no delay to launching it. At first I guessed this is because of some kind of task switching delay.(when a homekey is pressed, current task stack is going to background.) But if I try to start new application via shortcut on a home screen, not via my application service, it's ok. There is no delay to start a new application. I am very confused. – lunaticapple Nov 08 '11 at 07:10
  • This issue discussed on StackOverflow previously, see http://stackoverflow.com/questions/8163399/reason-for-5-sec-delay-to-show-an-activity-on-pressing-the-home-button, http://stackoverflow.com/questions/5600084/starting-an-activity-from-a-service-after-home-button-pressed-without-the-5-seco. To make long story short: as said at https://code.google.com/p/android/issues/detail?id=4536#c10, it's feature introduced to prevent intent flooding. – Andrey Starodubtsev Jan 06 '16 at 10:40

0 Answers0