个人原创文章,请尊重原创,转载请注明出处:吴磊的简书:http://www.jianshu.com/p/a4c29ec5712f
前言
最近在做项目的时候,大部分项目的分享功能都是通过微信、微博之类的SDK实现分享,又或是利用ShareSdk来做,很少使用系统自带的分享功能,碰到现在的项目只要求用系统的分享即可,虽然简单,但是一下子无从下手。经过网上的搜索研究,最终解决,此博文一为学习笔记,二为也要使用此功能的同学指引方向。
需求
直接调用系统的分享功能,分享文本、图片等信息到指定的平台。
实现
- 首先获取系统中所有的支持此分享的应用,以下为文本分享为例:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resolveInfos = thisActivity.getPackageManager().queryIntentActivities(shareIntent, PackageManager.MATCH_DEFAULT_ONLY);
- 其次要找到你要分享的应用的包名,你可以直接网上搜,可以在上面的 resolveInfos中查看所有可分享的应用,然后看到包名,以下列举几个常用的应用包名:
- QQ:com.tencent.mobileqq (包括QQ分享、QQ收藏、发送到电脑)
- 微信:com.tencent.mm (包括微信好友、微信收藏)
- 微博:com.sina.weibo (发布微博)
筛选代码如下:
if (resolveInfos.isEmpty()) {
return;
}
List<Intent> targetIntents = new ArrayList<>();
for (ResolveInfo info : resolveInfos) {
ActivityInfo ainfo = info.activityInfo;
switch (ainfo.packageName) {
case "com.tencent.mm":
addShareIntent(targetIntents, ainfo);
break;
case "com.tencent.mobileqq":
addShareIntent(targetIntents, ainfo);
break;
case "com.sina.weibo":
addShareIntent(targetIntents, ainfo);
break;
}
}
其中的addShareIntent 方法如下:
private void addShareIntent(List<Intent> list, ActivityInfo ainfo) {
Intent target = new Intent(Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TITLE, "这里是标题");
target.putExtra(Intent.EXTRA_TEXT, "这是我的分享内容");
target.setPackage(ainfo.packageName);
target.setClassName(ainfo.packageName, ainfo.name);
list.add(target);
}
- 最后拉起分享选择dialog:
if (targetIntents == null || targetIntents.size() == 0) {
return;
}
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "请选择分享平台");
if (chooserIntent == null) {
return;
}
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(thisActivity, "找不到该分享应用组件", Toast.LENGTH_SHORT).show();
}
以上就实现了指定应用的系统分享功能。完整代码如下:
public void onShareClicked() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resolveInfos = thisActivity.getPackageManager().queryIntentActivities(shareIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfos.isEmpty()) {
return;
}
List<Intent> targetIntents = new ArrayList<>();
for (ResolveInfo info : resolveInfos) {
ActivityInfo ainfo = info.activityInfo;
switch (ainfo.packageName) {
case "com.tencent.mm":
addShareIntent(targetIntents, ainfo);
break;
case "com.tencent.mobileqq":
addShareIntent(targetIntents, ainfo);
break;
case "com.sina.weibo":
addShareIntent(targetIntents, ainfo);
break;
}
}
if (targetIntents == null || targetIntents.size() == 0) {
return;
}
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "请选择分享平台");
if (chooserIntent == null) {
return;
}
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
try {
startActivity(chooserIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(thisActivity, "找不到该分享应用组件", Toast.LENGTH_SHORT).show();
}
}
private void addShareIntent(List<Intent> list, ActivityInfo ainfo) {
Intent target = new Intent(Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TITLE, "网盘搜索");
target.putExtra(Intent.EXTRA_TEXT, "这是我的分享内容");
target.setPackage(ainfo.packageName);
target.setClassName(ainfo.packageName, ainfo.name);
list.add(target);
}
欢迎指正代码中的错误,共同进步!
个人原创文章,请尊重原创,转载请注明出处:吴磊的简书:http://www.jianshu.com/p/a4c29ec5712f
欢迎联系我、提供工作机会:Github:https://github.com/MondeoWu/GestureUnlockE-mail:331948214@qq.comQQ:331948214
网友评论