Android-Launcher開發之ShortCut(1)

來源:互聯網
上載者:User

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);


 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.