Android應用中使用及實現系統“分享”介面執行個體_Android

來源:互聯網
上載者:User

為了應用的推廣、傳播,很多的應用中都有“分享”功能,一個按鈕,點擊後會出現簡訊、微博等等一切實現了分享功能的應用列表。這一篇文章主要介紹怎麼調用分享功能和怎麼實現分享介面讓自己應用出現分享列表中。Android應用中能很方便的完成這些功能,這也正是Android的偉大之處,他能很簡單的完成應用之間的溝通以相互整合。

調用分享功能

1、分享文本

分享功能使用的隱式啟動Activity的方法,這裡的Action使用的是 ACTION_SEND。

Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(sendIntent); 

效果如下圖的圖一。

2、改變分享列表標題

使用上面的分享方式分享列表標題為“使用一下內容完成操作”,Android中提供了Intent.createChooser() , 這樣能一直顯示分享挑選清單,並且修改了分享列表標題內容。

Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 

使用Intent.createChooser()的好處:

If you callIntent.createChooser() for the intent, Android will always display the chooser. This has some advantages:

  • Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
  • If no applications match, Android displays a system message.
  • You can specify a title for the chooser dialog.

           

分享功能不只是Intent.EXTRA_TEXT,還可以 EXTRA_EMAIL ,EXTRA_CC , EXTRA_BCC  ,EXTRA_SUBJECT . 只需要接受方完成響應資料接受。

3、分享圖片

分享功能還支援二進位內容(Binary Content),但是多數是處理的圖片,因為shareIntent.setType("image/jpeg")這一項設定了內容類型。可也以是其他類型,需要接受方支援。

Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); shareIntent.setType("image/jpeg"); startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to))); 

4、分享圖片列表

分享功能不僅支援單張圖片,還支援圖片列表,這裡還是說的範圍太窄了,應該聲明不僅僅是圖片。

ArrayList<Uri> imageUris = new ArrayList<Uri>(); imageUris.add(imageUri1); // Add your image URIs here imageUris.add(imageUri2);  Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "Share images to..")); 

實現分享功能

上面說的都是怎麼調用分享功能,以下就開始寫怎麼實現分享功能,讓我們的應用也出現在分享列表中。前面也說了分享功能是使用隱式調用Activtiy實現的,Activity需要聲明 <intent-filter> 。

聲明intent-filter

<activity       android:name="com.example.sharedemo.ShareActivity"       android:label="@string/app_name" >       <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-filter,當然可以更多,這裡只是舉個例子,

處理接收資料
聲明了intent-filter,響應的Activity就要處理響應的資料,樣本如下:

public class ShareActivity extends Activity{    @Override   protected void onCreate(Bundle savedInstanceState) {     // TODO Auto-generated method stub     super.onCreate(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);     String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);     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     }   } } 

通過聲明intent-filter,處理接受到的資料就能完成分享的接收功能。

更多

上面只做了分享功能簡單的說明,伴隨著Android api的升級,也出現了一些新的完成“分享”功能的方法,比如 ShareActionProvider ,更多請參考。

demo下載:demo

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

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