阅读提醒
本文只是关于如何实现Android系统分享,并非第三方SDK实现方法
Android开发时通过startActivity发送action为Intent.ACTION_SEND的Intent即很容易就可以实现系统分享功能,举个简单例子看看:
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,否则会出现别样的应用选择框,您可以试试
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
从例子中,可以发现实现系统分享主要由三部分Action、Extras和Type组成。首先将Intent的cation设置为Intent.ACTION_SEND,其次根据分享的内容设置不同的Type,然后根据不同的社交平台设置相关Extras,最后将Intent发送出去即可完成系统分享。
以下列举几种分享类型:
- 分享文本内容
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,否则会出现别样的应用选择框,您可以试试
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
- 分享图片
//将mipmap中图片转换成Uri
Uri imgUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" +R.mipmap.ic_launcher);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//其中imgUri为图片的标识符
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.setType("image/*");
//切记需要使用Intent.createChooser,否则会出现别样的应用选择框,您可以试试
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为第一张图片的标识符
imgUris.add(imgUri); //其中imgUri2为第二张图片的标识符
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
//其中fileUri为文件的标识符
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imgUris);
shareIntent.setType("image/*");
//切记需要使用Intent.createChooser,否则会出现别样的应用选择框,您可以试试
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为图片的标识符
shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
shareIntent.setType("image/*");
//设置邮件主题
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Here is the email subject");
//邮件内容
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the email content");
//切记需要使用Intent.createChooser,否则会出现别样的应用选择框,您可以试试
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
startActivity(shareIntent);
//EXTRA_BCC:存放邮件密送人地址的字符串数组。
//EXTRA_CC:存放邮件抄送人地址的字符串数组。
//EXTRA_EMAIL:存放邮件地址的字符串数组
问题拓展
1.如何将自己的应用能够显示在系统分享的应用选择框中?
根据以上介绍,我们可以在应用清单文件中使用<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" />
<!-- 根据分享类型的需要进行修改 -->
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
2.如何监听在应用选择框中,选择了那个应用?
需要采用BroadcastReceiver来实现:(该方法在部分手机上可以实现,并且需要API Level大于等于22)
- 创建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"/>
- 获取广播进行分享
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,否则会出现别样的应用选择框,您可以试试
shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box", pendingIntent.getIntentSender());
startActivity(shareIntent);
3.如何为制定应用设置分享type?
List<Intent> targetIntents = new ArrayList<Intent>();
//获取所有支持ACTION_SEND的应用
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
PackageManager pm = getPackageManager();
List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
//对目标引用进行判断
if(resInfos != null && resInfos.size() > 0){
for(ResolveInfo info:resInfos){
Intent target = new Intent(Intent.ACTION_SEND);
//需要知道制定应用的package name
if ("com.tencent.mm".equals(info.activityInfo.packageName)) {
target.setType("text/plain");
}else{
target.setType("image/*");
//其中imgUri为图片的标识符
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);
}
}
//创建应用选择框
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
4.如何只显示指定的应用?
List<Intent> targetIntents = new ArrayList<Intent>();
//获取所有支持ACTION_SEND的应用
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
PackageManager pm =getPackageManager();
List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
//对目标引用进行判断
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);
}
}
}
//创建应用选择框
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);