Android Intent.ACTION_CHOOSER 與 Intent.ACTION_PICK_ACTIVITY

來源:互聯網
上載者:User

1.Intent.ACTION_CHOOSER = “android.intent.action.CHOOSER” 其作用是顯示一個Activity選取器。

Intent提供了一個靜態createChooser方法,讓我們能夠更方便的建立這樣一個Intent。具體的用法樣本可以參考Launcher應用裡Launcher.java的startWallpaper函數:

   private void startWallpaper() {

        closeAllApps(true);

        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);

        Intent chooser = Intent.createChooser(pickWallpaper,

                getText(R.string.chooser_wallpaper));

 

        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);

    }

這裡是要找到所有能處理Intent.ACTION_SET_WALLPAPER請求的activity,其字串表示為android.intent.action.SET_WALLPAPER。使用Eclipse搜尋之後,在以下應用的AndroidManifest.xml檔案都找到了能處理這個請求的activity:

packages/apps/Gallery

packages/apps/Launcher2

packages/wallpapers/LivePicker

所以,在Home介面“按Menu鍵”--“點擊壁紙”後自然就能在一個對話方塊裡列出這些應用,讓使用者選擇到哪裡去設定壁紙了,如所示。



 

在中,使用者點擊任意一個清單項目,都會啟用其對應的Activity。使用者佈建壁紙的操作會在新啟動的Activity裡完成,例如使用者點擊中的“照片”項,則會進入到Gallery應用,在Gallery裡完成壁紙設定,見。



 

2. Intent.ACTION_PICK_ACTIVITY介紹及兩者區別

Intent.ACTION_PICK_ACTIVITY = “android.intent.action.PICK_ACTIVITY”。

乍一看去,真看不出它和 Intent.ACTION_CHOOSER有什麼區別。為了弄清這一點,筆者特地做了個實驗,把上面設定壁紙的intent action改為Intent.ACTION_PICK_ACTIVITY,運行出來的介面如。



從介面上看去,和Intent.ACTION_CHOOSER的表現方式基本一致,但是點擊以後卻沒有像Intent.ACTION_CHOOSER一樣啟動所選的Activity。筆者很快從Android源碼裡找到了原因。

對intent.ACTION_PICK_ACTIVITY action的處理位於Settings應用裡,如下XML所示。

<activity android:name="ActivityPicker"

                android:label="@string/activity_picker_label"

                android:theme="@*android:style/Theme.Dialog.Alert"

                android:finishOnCloseSystemDialogs="true">

            <intent-filter>

                <action android:name="android.intent.action.PICK_ACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

咱們再到com.android.settings.ActivityPicker類裡去看個究竟。

/**

     * Handle clicking of dialog item by passing back

     * {@link #getIntentForPosition(int)} in {@link #setResult(int, Intent)}.

     */

    public void onClick(DialogInterface dialog, int which) {

        Intent intent = getIntentForPosition(which);

        setResult(Activity.RESULT_OK, intent);

        finish();

    }

這下很明白了,咱們點擊清單項目的時候,它沒有用intent啟動相應的activity,而是將它返回了。這就是它與Intent.ACTION_CHOOSER action的區別所在。同樣地,咱們也可以從原始碼裡找到Intent.ACTION_CHOOSER的真實處理流程。跟上面流程差不多,到源碼裡搜尋“android.intent.action.CHOOSER”就能很快找到入口並追蹤下去。對於Intent.ACTION_CHOOSER,筆者在ResolverActivity內也找到一個onClick方法,貼出重要代碼如下。

public void onClick(DialogInterface dialog, int which) {

        ResolveInfo ri = mAdapter.resolveInfoForPosition(which);

        Intent intent = mAdapter.intentForPosition(which);

 

    。。。。。。

 

     if (intent != null) {

            startActivity(intent);

        }

        finish();

    }

跟前面推斷的一樣,它最後直接啟動了使用者選擇的清單項目對應的activity。

 

3.文檔闡述

看看文檔裡對它們各自的闡述,本人英文水平不高,所以直接貼了。

ACTION_CHOOSER

Input: No data should be specified. get*Extra must have a EXTRA_INTENT  field containing the Intent being executed, and can optionally have a EXTRA_TITLE  field containing the title text to display in the chooser.

Output: Depends on the protocol of EXTRA_INTENT.

 

ACTION_PICK_ACTIVITY

Input: get*Extra field EXTRA_INTENT  is an Intent used with PackageManager.queryIntentActivities  to determine the set of activities from which to pick.

Output: Class name of the activity that was selected.

 

4.Intent.ACTION_PICK_ACTIVITY執行個體

不用到網上翻來翻去,Android源碼是最好的學習資料。Launcher應用裡的添加檔案夾(userFolder)和LiverFolder時用的就是這個action。在此不詳述。

相關文章

聯繫我們

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