前言:實(shí)現(xiàn)分享功能的幾個(gè)辦法
- 調(diào)用系統(tǒng)的分享功能
- 通過(guò)第三方SDK钉蒲,如ShareSDK切端,友盟等
- 自行使用各自平臺(tái)的SDK,比如QQ顷啼,微信踏枣,微博各自的SDK
一、調(diào)用系統(tǒng)的分享功能
本文只是關(guān)于如何實(shí)現(xiàn)Android系統(tǒng)分享线梗,并非第三方SDK實(shí)現(xiàn)方法
實(shí)例
Android開(kāi)發(fā)時(shí)通過(guò)startActivity發(fā)送action為Intent.ACTION_SEND的Intent即很容易就可以實(shí)現(xiàn)系統(tǒng)分享功能,舉個(gè)簡(jiǎ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.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ù)不同的社交平臺(tái)設(shè)置相關(guān)Extras煮嫌,最后將Intent發(fā)送出去即可完成系統(tǒng)分享。
以下列舉幾種分享類(lèi)型:
- 分享文本內(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)識(shí)符
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)識(shí)符
imgUris.add(imgUri); //其中imgUri2為第二張圖片的標(biāo)識(shí)符
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
//其中fileUri為文件的標(biāo)識(shí)符
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)識(shí)符
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ù)組
問(wèn)題拓展
- 如何將自己的應(yīng)用能夠顯示在系統(tǒng)分享的應(yīng)用選擇框中?
根據(jù)以上介紹赋除,我們可以在應(yīng)用清單文件中使用<intent-filter>來(lái)完成阱缓;
<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ù)分享類(lèi)型的需要進(jìn)行修改 -->
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
- 如何監(jiān)聽(tīng)在應(yīng)用選擇框中,選擇了那個(gè)應(yīng)用举农?
需要采用BroadcastReceiver來(lái)實(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中注冊(cè)廣播
<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);
- 如何為制定應(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);
//對(duì)目標(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)識(shí)符
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);
//對(duì)目標(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);