日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【Android Developers Training】 33. 接收來自

系統(tǒng) 2495 0

注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛安卓而產(chǎn)生了翻譯的念頭,純屬個人興趣愛好。

原文鏈接: http://developer.android.com/training/sharing/receive.html


既然你的應(yīng)用可以向其它應(yīng)用發(fā)送數(shù)據(jù),那么你的應(yīng)用也可以接收來自其它應(yīng)用的數(shù)據(jù)。您需要思考一下用戶是如何與你的應(yīng)用交互的,以及你希望接收來自其它應(yīng)用什么樣的數(shù)據(jù)。例如,一個社交網(wǎng)絡(luò)應(yīng)用可能會期望收到來自其它應(yīng)用的文本內(nèi)容,比如一個感興趣的網(wǎng)頁URL。而 Google+ Android application 即接收文本和單個或多個圖像。這樣一來,用戶就可以輕松的再Google+上發(fā)布來自Android圖庫應(yīng)用中的照片了。

?

一). 更新你的清單文件

intent過濾器會告知系統(tǒng),應(yīng)用組件期望接收什么樣的intents。如在課程 Sending Simple Data to Other Apps (博客鏈接: http://www.cnblogs.com/jdneo/p/3473170.html )你構(gòu)造一個具有 ACTION_SEND 的intent類似。你可以創(chuàng)建intent過濾器來接收這一行為的intent。你在你的清單文件中使用 <intent-filter> 標簽定義一個intent過濾器。例如,如果你的應(yīng)用能處理接收文本內(nèi)容,一個單一的任何類型的圖片,或者多張不同類型的圖片,你的清單文件看上去應(yīng)該是這樣的:

      
        <
      
      
        activity 
      
      
        android:name
      
      
        =".ui.MyActivity"
      
      
        >
      
      
        <
      
      
        intent-filter
      
      
        >
      
      
        <
      
      
        action 
      
      
        android:name
      
      
        ="android.intent.action.SEND"
      
      
        />
      
      
        <
      
      
        category 
      
      
        android:name
      
      
        ="android.intent.category.DEFAULT"
      
      
        />
      
      
        <
      
      
        data 
      
      
        android:mimeType
      
      
        ="image/*"
      
      
        />
      
      
        </
      
      
        intent-filter
      
      
        >
      
      
        <
      
      
        intent-filter
      
      
        >
      
      
        <
      
      
        action 
      
      
        android:name
      
      
        ="android.intent.action.SEND"
      
      
        />
      
      
        <
      
      
        category 
      
      
        android:name
      
      
        ="android.intent.category.DEFAULT"
      
      
        />
      
      
        <
      
      
        data 
      
      
        android:mimeType
      
      
        ="text/plain"
      
      
        />
      
      
        </
      
      
        intent-filter
      
      
        >
      
      
        <
      
      
        intent-filter
      
      
        >
      
      
        <
      
      
        action 
      
      
        android:name
      
      
        ="android.intent.action.SEND_MULTIPLE"
      
      
        />
      
      
        <
      
      
        category 
      
      
        android:name
      
      
        ="android.intent.category.DEFAULT"
      
      
        />
      
      
        <
      
      
        data 
      
      
        android:mimeType
      
      
        ="image/*"
      
      
        />
      
      
        </
      
      
        intent-filter
      
      
        >
      
      
        </
      
      
        activity
      
      
        >
      
    

Note:

更多intent過濾器以及intent解析的內(nèi)容,可以閱讀: Intents and Intent Filters

當另一個應(yīng)用通過構(gòu)造一個intent并且將它傳遞給 startActivity() ,來嘗試分享任何這些數(shù)據(jù)時,你的應(yīng)用將會在intent選擇器中列出來。如果用戶選擇了你的應(yīng)用,響應(yīng)的activity(在上例中是“ .ui.MyActivity )將會被啟動。之后合理地處理內(nèi)容就要看你的代碼和UI的設(shè)計了。

?

二). 處理接收的內(nèi)容

為了處理 Intent 發(fā)送的數(shù)據(jù),可以調(diào)用 getIntent() 來獲取 Intent 對象。一旦你獲取了對象,你可以檢查它的內(nèi)容來決定下一步做什么。記住如果該activity能夠被其他系統(tǒng)的某個部分啟動,比如啟動器,當你要檢驗這個intent時,你需要將這部分內(nèi)容考慮進去。

      
        void
      
      
         onCreate (Bundle savedInstanceState) {

    ...

    
      
      
        //
      
      
         Get intent, action and MIME type
      
      

    Intent intent =
      
         getIntent();

    String action 
      
      =
      
         intent.getAction();

    String type 
      
      =
      
         intent.getType();



    
      
      
        if
      
       (Intent.ACTION_SEND.equals(action) && type != 
      
        null
      
      
        ) {

        
      
      
        if
      
       ("text/plain"
      
        .equals(type)) {

            handleSendText(intent); 
      
      
        //
      
      
         Handle text being sent
      
      

        } 
      
        else
      
      
        if
      
       (type.startsWith("image/"
      
        )) {

            handleSendImage(intent); 
      
      
        //
      
      
         Handle single image being sent
      
      
                }

    } 
      
      
        else
      
      
        if
      
       (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != 
      
        null
      
      
        ) {

        
      
      
        if
      
       (type.startsWith("image/"
      
        )) {

            handleSendMultipleImages(intent); 
      
      
        //
      
      
         Handle multiple images being sent
      
      
                }

    } 
      
      
        else
      
      
         {

        
      
      
        //
      
      
         Handle other intents, such as being started from the home screen
      
      
            }

    ...

}




      
      
        void
      
      
         handleSendText(Intent intent) {

    String sharedText 
      
      =
      
         intent.getStringExtra(Intent.EXTRA_TEXT);

    
      
      
        if
      
       (sharedText != 
      
        null
      
      
        ) {

        
      
      
        //
      
      
         Update UI to reflect text being shared
      
      
            }

}




      
      
        void
      
      
         handleSendImage(Intent intent) {

    Uri imageUri 
      
      =
      
         (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

    
      
      
        if
      
       (imageUri != 
      
        null
      
      
        ) {

        
      
      
        //
      
      
         Update UI to reflect image being shared
      
      
            }

}




      
      
        void
      
      
         handleSendMultipleImages(Intent intent) {

    ArrayList
      
      <Uri> imageUris =
      
         intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);

    
      
      
        if
      
       (imageUris != 
      
        null
      
      
        ) {

        
      
      
        //
      
      
         Update UI to reflect multiple images being shared
      
      
            }

}
      
    

Caution:

檢驗接收的數(shù)據(jù)要額外關(guān)注,因為你永遠都預測不到其他應(yīng)用會發(fā)給你什么數(shù)據(jù)。例如,錯誤的MIME類型可能被設(shè)置,或者發(fā)送的圖片可能特別大。另外,要記住在另一個線程執(zhí)行二進制數(shù)據(jù),而不是在主線程(“ UI線程 ”)。

更新一個UI可能簡單到填充一個 EditText ,也有可能復雜到在一幅圖片上應(yīng)用一個濾鏡效果。下一步如何執(zhí)行完全取決于你的應(yīng)用。

【Android Developers Training】 33. 接收來自其它應(yīng)用的簡單數(shù)據(jù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 潜江市| 波密县| 益阳市| 登封市| 乡宁县| 西宁市| 高清| 阜平县| 永安市| 南昌市| 即墨市| 清水河县| 五家渠市| 长武县| 荥阳市| 乐山市| 文登市| 沁水县| 丹寨县| 桦甸市| 玛沁县| 张家界市| 页游| 丹阳市| 红安县| 元氏县| 东乡县| 东安县| 华阴市| 内江市| 天等县| 洪江市| 南昌县| 和林格尔县| 大洼县| 和政县| 饶平县| 米易县| 淮北市| 安新县| 山东省|