您的位置:首页 > 移动开发

Android调用系统分享和指定app分享-微信朋友圈图文分享和qq分享

2018-02-14 23:24 537 查看
1.调用系统分享
根据每个手机的情况不同,打开的分享面板内容也是不一样的。其实本质上,分享的目标app是插件化开发,这样调用分享的时候,才能打开对应的Activity,而不需要打开一个完整的app进程。
/**
* 调用本地分享文本
*/
private void showLocationShare(int request) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "这是分享的内容!");//注意:这里只是分享文本内容
sendIntent.setType("text/plain");
startActivityForResult(sendIntent, request);
}调用系统的分享功能可以不用申请API集成,比较方便,但是不好的地方就是没有回调可以知道是否分享了,分享是否成功了。

2.指定app分享
首先要知道app的包名和Activity的名称。通过以下方法获得手机上全部可用于分享的app的包名和Activity名称。
Intent share = new Intent(android.content.Intent.ACTION_SEND);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> list=packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
for(ResolveInfo info:list){
MyUtils.log(""+info.activityInfo.packageName+"---"+info.activityInfo.name);
}得到包名和activity名称后,指定目标app分享。以QQ和微信举例。
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareTittle + "\n" + shareContent + "\n" + shareUrl);
sendIntent.setType("text/plain");
// sendIntent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//微信朋友
// sendIntent.setClassName("com.tencent.mobileqq", "cooperation.qqfav.widget.QfavJumpActivity");//保存到QQ收藏
// sendIntent.setClassName("com.tencent.mobileqq", "cooperation.qlink.QlinkShareJumpActivity");//QQ面对面快传
// sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.qfileJumpActivity");//传给我的电脑
sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");//QQ好友或QQ群
// sendIntent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,仅支持分享图片
startActivityForResult(sendIntent, QUN_QUEST);
3.Android系统分享-微信朋友圈分享
Bitmap decodeRecource = BitmapFactory.decodeResource(getResources(), R.drawable.app_icon);
File file = writeFileByBitmap2(decodeRecource);
Uri img = Uri.fromFile(file);//获得一张图片的Uri分享单张图片到朋友圈
Intent send = new Intent();
<strong>send.setAction(Intent.ACTION_SEND)</strong>;
send.putExtra(Intent.<strong>EXTRA_STREAM</strong>, img);
send.setType("image/*");
send.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,仅支持分享图片
startActivityForResult(send, WX_QUEST);分享多张图片到朋友圈
ArrayList<Uri> uriList = new ArrayList<Uri>();
uriList.add(img);
uriList.add(img);

Intent send = new Intent();
<span style="color:#ff6666;"><strong>send.setAction(Intent.ACTION_SEND_MULTIPLE);</strong></span>
send.putExtra(Intent.EXTRA_STREAM, <strong>uriList</strong>);
send.setType("image/*");
send.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,仅支持分享图片
startActivityForResult(send, WX_QUEST);
微信分享图片+文字
用上面的方法添加图片分享,如果要添加文字,再添加一句代码就可以
send.putExtra("<span style="color:#ff0000;"><strong>Kdescription</strong></span>", shareContent+shareUrl);

本来因为昨天熬夜了今天想要早点休息,但是想到原地踏步就是退步,还是打开了电脑,随便写点笔记也是好的。想起这两天碰到的事情,用一句话概括,同时时刻提醒自己。
Everything that you've learnt and all the hardships you've suffered will all come in handy at some point in your life.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息