Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 信息共享:接收來自其他應用的內容

編輯:Android 開發實例

正如上節課《Android 信息共享之內容發布》所講,你的應用可以向其他 APP 發送內容;同樣地,你的應用也能接收並處理其他 APP 發來的內容。設計之初,你就該想清楚:如何讓 APP 與用戶互動,以及你的 APP 主要處理哪些類型的數據。例如,一個社交類 APP 可能對純文本數據(如網頁 URL)感興趣;而 Google+ Android 應用則同時關注純文本與單張或多張圖片數據,這樣用戶在 Gallery 瀏覽圖片時,便能順手發一條帶圖的 Google+ 動態。

更新 Manifest 文件

Intent filters 主要用來告訴系統你的 APP 關注哪些類型的消息。如前面章節所講,構造發送內容的 Intent 時需把 Action 設為 ACTION_SEND,那麼要接收內容,同樣得告訴系統你的 APP 對 ACTION_SEND 感興趣。你需要在 <intent-filter> 標簽中定義關注的 intent filter。比如你的 APP 能處理純文本、通用類型的單張圖片,或混合類型的圖片列表,那麼 manifest 可能是這樣的:

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

注:想了解更多關於 Intent 與 Intent Filter 的知識,可參閱《Intents and Intent Filters》。

假如其他 APP 發送了與上述 intent filter 相匹配的數據(通過聲明 Intent、設置 Action、填充數據、調用 startActivity()),那麼你的 APP 就會出現在 intent chooser(選擇器)中。當用戶選中你的應用,對應的 activity(如上文聲明的 .ui.MyActivity)便會被啟動,具體如何處理收到的數據,就取決於你的設計了。

處理接收到的數據

要拿到其他 APP 發來的數據,首先需要調用 getIntent() 獲得包含數據的 Intent 對象,拿到後便可讀取其中的值來決定下一步動作。有一點需格外留意:如果你的 Activity 有多個啟動入口(比如用戶既能從主界面 launcher 啟動,也能從分享流程進入),你就得小心應對,因為不同入口的 Intent 所包含的內容並不相同。

示例代碼:

 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 } }

注意:對於其他 APP 發來的數據,你必須格外小心地檢查和處理,因為你無法預知對方會發送什麼。例如它們可能設了錯誤的 MIME 類型,或傳來一張體積龐大的圖片;此外,處理二進制數據時最好另起獨立線程,而不要放在主線程(main "UI" thread)中,以免界面阻塞。

UI 的更新可以很簡單,比如把收到的純文本顯示到 EditText 中;也可以很複雜,比如對收到的圖片做圖像變換後再展示。具體怎麼處理,全看你的 APP 設計。

參考文摘:

https://developer.android.com/training/sharing/receive.html

原文:https://blog.zhourunsheng.com/2012/01/ | 潤物無聲

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved