Android-Launcher開發之ShortCut(1)
以下源碼來自Launcher2.3的例子
1.預設每個應用的主Activity都會內建 ,表示該應用安裝到Launcher時點擊開啟該Activity
2.Launcher2源碼的時序圖如下:(在圖中,我們可以看到 建立shortcut需要準備2方面東西,一個action.,另一個是我們檢索後返回的intent)
2.1.當想在案頭手動建立shortcut,就必須在AndroidManifest.xml檔案中添加一個標籤.
如下.我們建立一個ShortCutActivity用來處理shortcut的建立
2.2並在Activity中處理顯示的shortCut樣式的返回Intent
/** * @author Lean @date:2014-8-25 */public class ShortCutActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)) {Intent returnIntent=new Intent();returnIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,R.drawable.ic_launcher));returnIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,A simple shortCut);returnIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(this,MainActivity.class));setResult(RESULT_OK,returnIntent);finish();}}}
3.以上的shortcut只能手動添加,如果想動態添加shortCut 就必鬚髮送廣播.Android Launcher2源碼提供了如下
這也表示,我們發送廣播必須聲明許可權,還有指定,於是 在我們的應用程式AndroidManifest.xml裡 添加
同時在代碼調用(同時,動態添加shortcut也必須指定其樣式和操作意圖)
Intent intent=new Intent();intent.setAction(com.android.launcher.action.INSTALL_SHORTCUT);intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,R.drawable.ic_launcher);intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,a auto sample);intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(MainActivity.this,MainActivity.class));sendBroadcast(intent);