為了應用的推廣、傳播,很多的應用中都有“分享”功能,一個按鈕,點擊后會出現短信、微博等等一切實現了分享功能的應用列表。這一篇文章主要介紹怎么調用分享功能和怎么實現分享接口讓自己應用出現分享列表中。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 ,更多請參考。
參考:
http://developer.android.com/training/sharing/index.html
