在startActivity上启动速度

时间:2011-11-07 16:16:32

标签: android performance android-activity

我正在开发一个作为服务启动的Android应用程序

特定条件下的其他应用程序。

有很多机会使用'startActivity'功能。

我注意到有时使用startActivity启动另一个应用程序需要很长时间。

请看下两个案例。

案例1:菜单 - >返回键 - >首页 - > startActivity

案例2:菜单 - >主页键 - >首页 - > startActivity

在第一种情况下,启动新应用程序没有延迟。

但在第二种情况下,启动新应用程序需要3~5秒。

我尝试了很多次,但结果是一样的。

请帮我解决这个问题。

为什么只在第二种情况下需要很长时间?

我的代码:

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

}

0 个答案:

没有答案