Startup Speed ​​at Startup

I am developing an Android application that starts as a service to run

other applications in a certain state.

There are many chances to use the startActivity function.

I noticed that sometimes it takes too much time to start another application using the startActivity function.

Please review the following two cases.

case 1: Menu β†’ Back key β†’ Home β†’ startActivity

case 2: Menu β†’ Home button β†’ Home β†’ startActivity

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

But in the second case, it takes 3 ~ 5 seconds to start a new application.

I tried many times, but the results were the same.

Please help me solve this problem.

Why does it take so long only in the second case?

My code is:

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

}

+6
source share

Source: https://habr.com/ru/post/900950/


All Articles