Android應(yīng)用中使用及實(shí)現(xiàn)系統(tǒng)“分享”接口實(shí)例
為了應(yīng)用的推廣、傳播,很多的應(yīng)用中都有“分享”功能,一個(gè)按鈕,點(diǎn)擊后會(huì)出現(xiàn)短信、微博等等一切實(shí)現(xiàn)了分享功能的應(yīng)用列表。這一篇文章主要介紹怎么調(diào)用分享功能和怎么實(shí)現(xiàn)分享接口讓自己應(yīng)用出現(xiàn)分享列表中。Android應(yīng)用中能很方便的完成這些功能,這也正是Android的偉大之處,他能很簡(jiǎn)單的完成應(yīng)用之間的溝通以相互整合。
調(diào)用分享功能
1、分享文本
分享功能使用的隱式啟動(dòng)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、改變分享列表標(biāo)題
使用上面的分享方式分享列表標(biāo)題為“使用一下內(nèi)容完成操作”,Android中提供了Intent.createChooser() , 這樣能一直顯示分享選擇列表,并且修改了分享列表標(biāo)題內(nèi)容。
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 . 只需要接受方完成響應(yīng)數(shù)據(jù)接受。
3、分享圖片
分享功能還支持二進(jìn)制內(nèi)容(Binary Content),但是多數(shù)是處理的圖片,因?yàn)閟hareIntent.setType("image/jpeg")這一項(xiàng)設(shè)置了內(nèi)容類型??梢惨允瞧渌愋?,需要接受方支持。
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、分享圖片列表
分享功能不僅支持單張圖片,還支持圖片列表,這里還是說的范圍太窄了,應(yīng)該聲明不僅僅是圖片。
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.."));
實(shí)現(xiàn)分享功能
上面說的都是怎么調(diào)用分享功能,以下就開始寫怎么實(shí)現(xiàn)分享功能,讓我們的應(yīng)用也出現(xiàn)在分享列表中。前面也說了分享功能是使用隱式調(diào)用Activtiy實(shí)現(xiàn)的,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,當(dāng)然可以更多,這里只是舉個(gè)例子,
處理接收數(shù)據(jù)
聲明了intent-filter,響應(yīng)的Activity就要處理響應(yīng)的數(shù)據(jù),示例如下:
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,處理接受到的數(shù)據(jù)就能完成分享的接收功能。
更多
上面只做了分享功能簡(jiǎn)單的說明,伴隨著Android api的升級(jí),也出現(xiàn)了一些新的完成“分享”功能的方法,比如 ShareActionProvider ,更多請(qǐng)參考。
demo下載:demo
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Volley實(shí)現(xiàn)上傳文件功能
這篇文章主要介紹了Android使用Volley實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android開發(fā)之高德地圖實(shí)現(xiàn)定位
本篇文章主要介紹了Android中高德地圖實(shí)現(xiàn)定位的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04Android實(shí)現(xiàn)頂部底部雙導(dǎo)航界面功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)頂部\底部雙導(dǎo)航界面功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android 按后退鍵退出Android程序的實(shí)現(xiàn)方法
本篇文章介紹了,在Android中按后退鍵退出Android程序的實(shí)現(xiàn)方法。需要的朋友參考下2013-04-04Android中關(guān)于屏幕的三個(gè)小眾知識(shí)(寬屏適配、禁止截屏和保持屏幕常亮)
這篇文章主要給大家介紹了Android中關(guān)于屏幕的三個(gè)小眾知識(shí),分別是寬屏適配、禁止截屏和保持屏幕常亮的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考學(xué)習(xí),下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12android 實(shí)現(xiàn)側(cè)邊彈窗特效代碼
側(cè)邊彈窗是在左邊,需要定位好位置,實(shí)現(xiàn)原理其實(shí)就是進(jìn)出動(dòng)效,用位移加透明度效果來控制,下面通過代碼給大家介紹android 實(shí)現(xiàn)側(cè)邊彈窗,需要的朋友參考下吧2021-06-06Android中將Bitmap對(duì)象以PNG格式保存在內(nèi)部存儲(chǔ)中的方法
在Android中進(jìn)行圖像處理的任務(wù)時(shí),有時(shí)我們希望將處理后的結(jié)果以圖像文件的格式保存在內(nèi)部存儲(chǔ)空間中,本文以此為目的,介紹將Bitmap對(duì)象的數(shù)據(jù)以PNG格式保存下來的方法2017-08-08Android實(shí)現(xiàn)樹形層級(jí)ListView
這篇文章主要介紹了Android實(shí)現(xiàn)樹形層級(jí)ListView的相關(guān)資料,需要的朋友可以參考下2016-02-02android中ProgressDialog與ProgressBar的使用詳解
本篇文章是對(duì)android中ProgressDialog與ProgressBar的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06