1.實現系統自帶的分享功能
- 使用android系統提供的分享功能:
點擊分享按鈕,系統會自動獲取手機內提供分享功能的App。
只安裝了微信的手機
新安裝了新浪微博的手機
- 代碼實現
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*);
intent.putExtra(Intent.EXTRA_SUBJECT,"share");
intent.putExtra(Intent.EXTRA_TEXT,"successfully");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent,getTitle()));
- 閱讀源碼了解含義
Intent.ACTION_SEND
Activity Action: Deliver some data to someone else. Who the data is being delivered to is not specified; it is up to the receiver of this action to ask the user where the data should be sent.
When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.
Input: getType() is the MIME type of the data being sent. get*Extra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the MIME type of the data in EXTRA_STREAM. Use / if the MIME type is unknown (this will only allow senders that can handle generic data streams). If using EXTRA_TEXT, you can also optionally supply EXTRA_HTML_TEXT for clients to retrieve your text with HTML formatting.
As of JELLY_BEAN, the data being sent can be supplied through setClipData(ClipData). This allows you to use FLAG_GRANT_READ_URI_PERMISSION when sharing content: URIs and other advanced features of ClipData. If using this approach, you still must supply the same data through the EXTRA_TEXT or EXTRA_STREAM fields described below for compatibility with old applications. If you don't set a ClipData, it will be copied there for you when calling startActivity(Intent).
Optional standard extras, which may be interpreted by some recipients as appropriate, are: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT.
Output: nothing.
Constant Value: "android.intent.action.SEND"
- 閱讀源碼可知
系統會找到提供分享接口的程序,并詢問用戶把這些數據要分享到哪里。
分享文字時調用:
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"分享的內容");
分享單張圖片時調用:
String imagePath = Environment.getExternalStorageDirectory()+File.separator+"test.jpg";
//得到文件的uri
Uri imageUri = Uri.fromFile(new File(imagePath));
//用Intent.ACTION_SEND 創建intent對象
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,imageUri);
- 分享文字時向外發送純文本類型:EXTRA_TEXT
- 分享圖片或者文件時向外發送Uri:EXTRA_STREAM
什么是URI?:
在電腦術語中,統一資源標識符(Uniform Resource Identifier,或URI)是一個用于標識某一互聯網資源名稱的字符串。 該種標識允許用戶對任何(包括本地和互聯網)的資源通過特定的協議進行交互操作。URI由包括確定語法和相關協議的方案所定義。
Web上可用的每種資源 -HTML文檔、圖像、視頻片段、程序等 - 由一個通用資源標識符(Uniform Resource Identifier, 簡稱"URI")
進行定位。
startActivity(Intent.createChooser(intent,getTitle()));
上面這句代碼創建了一個Chooser
什么是Chooser?
這里的Intent.createChooser
實際上是創建一個ACTION_CHOOSER
在要分享數據時,會打開一個dialog
,讓用戶去選擇要分享到哪個app。
分享功能的實質—app之間的交互
通過Intent
來發送一些請求,調用相關的應用來處理這些請求,那么相關的應用是怎么知道我們要給它傳遞數據呢?
-
首先我們來詳細的來了解這種app之間交互的
Intent
我們稱這種Intent
為隱式的Intent
,隱式的Intent
需要我們聲明一個ACTION
,?ACTION
中定義了我們想要處理的請求,
設置我們要分享數據的type
也很重要,如果沒有設置type
,或者設置的不正確,會導致程序崩潰,比如我們要分享一張圖片,要是沒有Intent.setType()
,那么要是選擇了地圖APP,我們的程序就會崩潰,所以要設置合理的type
,提高程序的質量。
回到最初的問題,我們要分享到的App,比如微博,微博要為其他的程序組件的請求提供服務,那么微博就需要注冊一個(或者多個),Intent處理者,在組件的manifest
節點添加一個intent-filter
在intent-filter
節點里使用下面的標簽(關聯屬性),就可以執行組件支持的動作,種類和數據:
<Intent-filter>
元素中可以包括子元素<action>
例如
<intent-filter>
<action android:name="com.example.project"/>
<action android:name="com.example.project"/>
action使用 android:name 特性來指定對響應的動作名。動作名必須是獨一無二的字符串,所以,一個好的習慣是使用基于 Java 包的命名方式的命名系統。
category使用 android:category 屬性用來指定在什么樣的環境下動作才被響應。每個 Intent Filter 標簽可以包含多個 category 標簽。你可以指定自定義的種類或使用 Android 提供的標準值,如下所示:
ALTERNATIVE你將在這章的后面所看到的,一個 Intent Filter 的用途是使用動作來幫忙填入上下文菜單。 ALTERNATIVE 種類指定,在某種數據類型的項目上可以替代默認執行的動作。例如,一個聯系人的默認動作時瀏覽它,替代的可能是去編輯或刪除它
SELECTED_ALTERNATIVE與 ALTERNATIVE 類似,但 ALTERNATIVE 總是使用下面所述的 Intent 解析來指向單一的動作。SELECTED_ALTERNATIVE在需要一個可能性列表時使用。
BROWSABLE指定在瀏覽器中的動作。當 Intent 在瀏覽器中被引發,都會被指定成 BROWSABLE 種類。
DEFAULT設置這個種類來讓組件成為 Intent Filter 中定義的 data 的默認動作。這對使用顯式 Intent 啟動的 Activity 來說也是必要的。
GADGET通過設置 GADGET 種類,你可以指定這個 Activity 可以嵌入到其他的 Activity 來允許。
HOMEHOME Activity 是設備啟動(登陸屏幕)時顯示的第一個 Activity 。通過指定 Intent Filter 為 HOME 種類而不指定動作的話,你正在將其設為本地 home 畫面的替代。
LAUNCHER使用這個種類來讓一個 Activity 作為應用程序的啟動項。
datadata 標簽允許你指定組件能作用的數據的匹配;如果你的組件能處理多個的話,你可以包含多個條件。你可以使用下面屬性的任意組合來指定組件支持的數據:
android:host指定一個有效的主機名(例如, com.google )。
android:mimetype允許你設定組件能處理的數據類型。例如,<type android:value=”vnd.android.cursor.dir/*”/>能匹配任何 Android 游標。
android:path有效地 URI 路徑值(例如, /transport/boats/ )
android:port特定主機上的有效端口。
android:scheme需要一個特殊的圖示(例如, content 或 http )。
所以在微博等APP 中要設置相應的intent-filter
,能分享什么數據,那么就提供相應的標簽去接收某種數據類型的數據。
我們自己的app就負責,設置好類型type
然后把它發送出去就行了。