Android中為APP建立捷徑的原理(自己的理解)

來源:互聯網
上載者:User

Android中為APP建立捷徑的原理(自己的理解)
從圖上可以看出,Android大致分7步完成捷徑的建立: 第一步:Android系統的launcher程式會調用它的pickShortcut()方法去啟動系統的pickActivity程式(應用); 第二步:pickActivity程式(應用)啟動後會調用它的CheckIntentFilter()方法,去在系統中尋找可以建立捷徑的應用有哪些,並且列舉出來。只要第三方                     App用<Intent-filter>標籤進行了相應的註冊(具體如何註冊請看下面的代碼)就可以被發現並列舉出來; 第三步:調用Choseitem()方法選擇建立誰的捷徑; 第四步:完成第三步之後,pickActivity程式(應用)會將選擇的訊息通過Intent返回給系統的launcher; 第五步:launcher程式獲得pickActivity返回的訊息後,就會知道建立誰的捷徑,通過調用ProcessShortcut()方法去啟動第三方App中負責建立捷徑                     的Activity,這個Activity就是第二步中我們提到的用<Intent-filter>標籤進行了註冊的Activity; 第六步:第三方App中負責建立捷徑的Activity會將捷徑的名稱,表徵圖和點擊後跳轉路徑通過Intent返回給launcher; 第七部:launcher收到返回的訊息後調用本身的ComPleteAddShortcut()方法完成捷徑的建立,並顯示在案頭上;   分析完原理後,那麼作為第三方開發人員應該完成哪幾步呢? 我們只需完成如下2步就ok了,其他的事情系統會為我們去做: 首先:用<Intent-filter>標籤進行註冊 <activity android:name=".CreatShortCut">    <intent-filter>        <action android:name="android.intent.action.CREATE_SHORTCUT"/>    </intent-filter></activity>其中的“CreatShortCut”是負責建立捷徑的Activity的名字。 然後:向Launcher返回相關資料 複製代碼public class CreatShortCut extends Activity {     /**     * Description     *      * @param savedInstanceState     * @see android.app.Activity#onCreate(android.os.Bundle)     */     @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);         if (getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)) {             Intent _returnIntent = new Intent();            _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "csx");// 快速鍵的名字            _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,// 快速鍵的ico                    Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));            _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,                    MainActivity.class));// 快速鍵的跳轉Intent             setResult(RESULT_OK, _returnIntent);// 發送            finish();// 關閉本Activity         }     }}

聯繫我們

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