2011-9-27 21:43:19

 

2011-9-27 21:43:19

在Android系统中,应用程序是由Launcher启动起来的,其实,Launcher本身也是一个应用程序,其它的应用程序安装后,

就会Launcher的界面上出现一个相应的图标,点击这个图标时,Launcher就会对应的应用程序启动起来。


Launcher的源代码工程在packages/apps/Launcher2目录下,负责启动其它应用程序的源代码实现在src/com/android/launcher2/Launcher.java文件中:

 


public final class Launcher extends Activity
  implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {


监听和长按监听
 ......

 /**
 * Launches the intent referred by the clicked shortcut.
 *
 * @param v The view representing the clicked shortcut.
 */
 public void onClick(View v) {
  Object tag = v.getTag();
  if (tag instanceof ShortcutInfo) {
   // Open shortcut
   final Intent intent = ((ShortcutInfo) tag).intent;
   int[] pos = new int[2];
   v.getLocationOnScreen(pos);
   intent.setSourceBounds(new Rect(pos[0], pos[1],
    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
   startActivitySafely(intent, tag);
  } else if (tag instanceof FolderInfo) {
   ......
  } else if (v == mHandleView) {
   ......
  }
 }

 void startActivitySafely(Intent intent, Object tag) {
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  try {
   startActivity(intent);
  } catch (ActivityNotFoundException e) {
   ......
  } catch (SecurityException e) {
   ......
  }
 }

 ......

}


startActivitySafely 就加上了一个try catch

 它的默认Activity是MainActivity
 
 
<activity android:name=".MainActivity" 
      android:label="@string/app_name"> 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 


 因此,这里的intent包含的信息为:action = "android.intent.action.Main",category="android.intent.category.LAUNCHER", cmp="shy.luo.activity/.MainActivity",
 
 
 
表示它要启动的Activity为shy.luo.activity.MainActivity。Intent.FLAG_ACTIVITY_NEW_TASK表示要在一个新的Task中启动这个Activity,注意,Task是Android系统中的概念,

它不同于进程Process的概念。简单地说,一个Task是一系列Activity的集合,这个集合是以堆栈的形式来组织的,遵循后进先出的原则。

事实上,Task是一个非常复杂的概念,有兴趣的读者可以到官网http://developer.android.com/guide/topics/manifest/activity-element.html查看相关的资料。

这里,我们只要知道,这个MainActivity要在一个新的Task中启动就可以了。

 

launcher和新的Task

发布了363 篇原创文章 · 获赞 11 · 访问量 37万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章