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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章