閱讀提醒
本文只是關(guān)于如何實(shí)現(xiàn)Android系統(tǒng)分享狭莱,并非第三方SDK實(shí)現(xiàn)方法
Android開發(fā)時(shí)通過startActivity發(fā)送action為Intent.ACTION_SEND的Intent即很容易就可以實(shí)現(xiàn)系統(tǒng)分享功能迷守,舉個(gè)簡單例子看看:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the Shared text.");
//切記需要使用Intent.createChooser,否則會(huì)出現(xiàn)別樣的應(yīng)用選擇框熟吏,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
從例子中,可以發(fā)現(xiàn)實(shí)現(xiàn)系統(tǒng)分享主要由三部分Action袜腥、Extras和Type組成茬贵。首先將Intent的cation設(shè)置為Intent.ACTION_SEND,其次根據(jù)分享的內(nèi)容設(shè)置不同的Type沼头,然后根據(jù)不同的社交平臺設(shè)置相關(guān)Extras爷绘,最后將Intent發(fā)送出去即可完成系統(tǒng)分享。
以下列舉幾種分享類型:
- 分享文本內(nèi)容
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the Shared text.");
//切記需要使用Intent.createChooser进倍,否則會(huì)出現(xiàn)別樣的應(yīng)用選擇框土至,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
- 分享圖片
//將mipmap中圖片轉(zhuǎn)換成Uri
Uri imgUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" +R.mipmap.ic_launcher);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//其中imgUri為圖片的標(biāo)識符
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.setType("image/*");
//切記需要使用Intent.createChooser,否則會(huì)出現(xiàn)別樣的應(yīng)用選擇框背捌,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
- 分享多張圖片
Uri imgUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" +R.mipmap.ic_launcher);
ArrayList<Uri> imgUris = new ArrayList<Uri>();
imgUris.add(imgUri); //其中imgUri1為第一張圖片的標(biāo)識符
imgUris.add(imgUri); //其中imgUri2為第二張圖片的標(biāo)識符
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
//其中fileUri為文件的標(biāo)識符
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imgUris);
shareIntent.setType("image/*");
//切記需要使用Intent.createChooser毙籽,否則會(huì)出現(xiàn)別樣的應(yīng)用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
- 分享郵件
Uri imgUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" +R.mipmap.ic_launcher);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//其中imgUri為圖片的標(biāo)識符
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.setType("image/*");
//設(shè)置郵件主題
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Here is the email subject");
//郵件內(nèi)容
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the email content");
//切記需要使用Intent.createChooser毡庆,否則會(huì)出現(xiàn)別樣的應(yīng)用選擇框坑赡,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
//EXTRA_BCC:存放郵件密送人地址的字符串?dāng)?shù)組∶纯梗
//EXTRA_CC:存放郵件抄送人地址的字符串?dāng)?shù)組毅否。
//EXTRA_EMAIL:存放郵件地址的字符串?dāng)?shù)組
問題拓展
1.如何將自己的應(yīng)用能夠顯示在系統(tǒng)分享的應(yīng)用選擇框中?
根據(jù)以上介紹蝇刀,我們可以在應(yīng)用清單文件中使用<intent-filter>來完成螟加;
<activity
android:name=".ShareHandleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- 根據(jù)分享類型的需要進(jìn)行修改 -->
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
2.如何監(jiān)聽在應(yīng)用選擇框中,選擇了那個(gè)應(yīng)用?
需要采用BroadcastReceiver來實(shí)現(xiàn):(該方法在部分手機(jī)上可以實(shí)現(xiàn)捆探,并且需要API Level大于等于22)
- 創(chuàng)建BroadcastReceiver
public class ShareDoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
for(String key:intent.getExtras().keySet()){
try {
ComponentName componentInfo = (ComponentName) intent.getExtras().get(key);
PackageManager packageManager = context.getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(componentInfo.getPackageName(), PackageManager.GET_META_DATA));
Log.i("Selected", appName);
} catch (Exception e) {
}
}
}
}
- AndroidManifest中注冊廣播
<receiver android:name=".ShareDoneReceiver"/>
- 獲取廣播進(jìn)行分享
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the Shared text.");
Intent receiver = new Intent(this, ShareDoneReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
//切記需要使用Intent.createChooser然爆,否則會(huì)出現(xiàn)別樣的應(yīng)用選擇框,您可以試試
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box", pendingIntent.getIntentSender());
startActivity(shareIntent);
3.如何為制定應(yīng)用設(shè)置分享type黍图?
List<Intent> targetIntents = new ArrayList<Intent>();
//獲取所有支持ACTION_SEND的應(yīng)用
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
PackageManager pm = getPackageManager();
List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
//對目標(biāo)引用進(jìn)行判斷
if(resInfos != null && resInfos.size() > 0){
for(ResolveInfo info:resInfos){
Intent target = new Intent(Intent.ACTION_SEND);
//需要知道制定應(yīng)用的package name
if ("com.tencent.mm".equals(info.activityInfo.packageName)) {
target.setType("text/plain");
}else{
target.setType("image/*");
//其中imgUri為圖片的標(biāo)識符
target.putExtra(Intent.EXTRA_STREAM, imgUri);
}
target.putExtra(Intent.EXTRA_TEXT, "Here is the email content");
target.setComponent(new ComponentName( info.activityInfo.packageName, info.activityInfo.name));
targetIntents.add(target);
}
}
//創(chuàng)建應(yīng)用選擇框
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
4.如何只顯示指定的應(yīng)用曾雕?
List<Intent> targetIntents = new ArrayList<Intent>();
//獲取所有支持ACTION_SEND的應(yīng)用
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
PackageManager pm =getPackageManager();
List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
//對目標(biāo)引用進(jìn)行判斷
if(resInfos != null && resInfos.size() > 0){
for(ResolveInfo info:resInfos){
if("com.netease.mobimail".equals(info.activityInfo.packageName)
|| "com.google.android.talk".equals(info.activityInfo.packageName)){
Intent target = new Intent(Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TEXT, "Here is the email content");
target.setComponent(new ComponentName( info.activityInfo.packageName, info.activityInfo.name));
targetIntents.add(target);
}
}
}
//創(chuàng)建應(yīng)用選擇框
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);