Android ApiDemos樣本解析(24):App->Launcher Shortcuts

來源:互聯網
上載者:User

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
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.