Android ApiDemos示例解析(24):App->Launcher Shortcuts 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的快捷方式。
Android ApiDemos示例解析(24):App->Launcher Shortcuts 比如說你的應用中有個功能使用者可能會經常使用,比如說地圖中查詢位址,正常情況下使用者需要先啟動主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安裝快捷方式:
檢視源代碼 列印 幫助
1
if
(Intent.ACTION_CREATE_SHORTCUT.equals(action))
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.
18
Intent shortcutIntent =
new
Intent(Intent.ACTION_MAIN);
19
shortcutIntent.setClassName(
this
,
this
.getClass().getName());
20
shortcutIntent.putExtra(EXTRA_KEY,
"ApiDemos Provided This Shortcut"
);
22
// Then, set up the container intent (the response to the caller)
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);
31
// Now, return the result to the launcher
33
setResult(RESULT_OK, intent);
如果使用者想為某個Activity建立快捷方式,方法是在Home Screen的空白處長按,這時Android會顯示使用者可以選擇添加的桌面種類清單,選擇Shortcut(快捷方式)後,Android會列出所有定義了android.intent.action.CREATE_SHORTCUT的所有應用:
Android ApiDemos示例解析(24):App->Launcher Shortcuts 此時如果選擇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
Android ApiDemos示例解析(24):App->Launcher Shortcuts « Android ApiDemos示例解析(2... Android ApiDemos示例解析(2... »