Android 作業系統對於<intent-filter>含有下列屬性的Activity會在應用程式管理器(Launcher)顯示一項,一般這個Activity對應於某個應用的主Activity。
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
此外,如果使用者想在裝置的Home Screen上添加應用的捷徑,可以在Launcher中長按這個應用的表徵圖,Android系統會自動為該應用在Home Screen上添加一個捷徑,名稱和表徵圖和在Launcher中的一樣。
除了支援指嚮應用(主Activity)的捷徑外,Android可以在Home Screen上定義指向Application中任意Activity的捷徑。
比如說你的應用中有個功能使用者可能會經常使用,比如說地圖中查詢地址,正常情況下使用者需要先啟動主Activity,可能需要經過幾次菜單選擇或是其它方式才能進到這個功能,使用者可能感覺到不方便,這是可以為這個功能在Home Screen建立一個捷徑,使用者按這個捷徑後會直接進入這個功能介面,即使這個Activity不是主Activity。
Launcher Shortcuts就是介紹了如何為某個非主Activity在Home Screen上建立一個捷徑。
實現這個捷徑,可以分下面幾步來完成:
1.為需要建立捷徑的Activity的<intent-filter>添加<action android:name=”android.intent.action.CREATE_SHORTCUT” /> ,標識這個Activity可以支援在Home Screen上添加捷徑。Launcher Shortcuts 是採用的是activity-alias,activity-alias為Target的別名,類似於Activity.
2.添加相應使用者添加捷徑的代碼,一般在Activity的onCreate方法中為Activity安裝捷徑:
[java]
1. if (Intent.ACTION_CREATE_SHORTCUT.equals(action))
2. {
3. setupShortcut();
4. finish();
5. return;
6. }
7.
8. ...
9. private void setupShortcut() {
10. // First, set up the shortcut intent.
11. //For this example, we simply create an intent that
12. // will bring us directly back to this activity.
13. //A more typical implementation would use a
14. // data Uri in order to display a more specific result,
15. //or a custom action in order to
16. // launch a specific operation.
17.
18. Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
19. shortcutIntent.setClassName(this, this.getClass().getName());
20. shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");
21.
22. // Then, set up the container intent (the response to the caller)
23.
24. Intent intent = new Intent();
25. intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
26. intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
27. Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
28. this, R.drawable.app_sample_code);
29. intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
30.
31. // Now, return the result to the launcher
32.
33. setResult(RESULT_OK, intent);
34. }
如果使用者想為某個Activity建立捷徑,方法是在Home Screen的空白處長按,這時Android會顯示使用者可以選擇添加的案頭種類列表,選擇Shortcut(捷徑)後,Android會列出所有定義了android.intent.action.CREATE_SHORTCUT的所有應用:
此時如果選擇ApiDemos,那麼Add to Home Screen會啟動LauncherShortcuts Activity,發出請求的Intent的action 會設定為Intent.ACTION_CREATE_SHORTCUT,因此可以在onCreate中使用Intent.ACTION_CREATE_SHORTCUT.equals(action)來判斷請求是來自Add to Home Screen還是使用者選擇App->Launcher Shorts。如果是來自Add to Home Screen,Launcher Shortcuts則為本Activity建立一個捷徑setupShortcut,然後退出。
Add to Home Screen 發出Intent請其後(運行startActivityForResult),預期從LauncherShortcuts返回一個結果,這也是為什麼setupShortcut需要返回一個結果。對應建立的捷徑的Intent至少需要定義三個參數:SHORTCUT_INTENT (value: Intent), SHORTCUT_NAME (value: String), SHORTCUT_ICON (value: Bitmap)或SHORTCUT_ICON_RESOURCE (value: ShortcutIconResource).
本例是為當前Activity(LauncherShortcuts)建立捷徑,實際應用中可以根據需要為別的Activity或是提供一個列表供使用者來選擇需建立的捷徑。
本例在Home Screen建立一個Sample 捷徑,選擇該捷徑後,直接進入Launcher Shortcuts,而無需從App再進入Launcher Shortcuts
作者:mapdigit