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