Android 实现微信,微博,微信朋友圈,QQ分享的功能
程序员文章站
2024-02-27 18:56:33
android 实现微信,微博,微信朋友圈,qq分享的功能
一、去各自所在的开发者平台注册相应的key值;引入相关jar包、权限等
二、shareutil工具类...
android 实现微信,微博,微信朋友圈,qq分享的功能
一、去各自所在的开发者平台注册相应的key值;引入相关jar包、权限等
二、shareutil工具类
import android.app.activity; import android.content.context; import android.content.pm.packageinfo; import android.content.pm.packagemanager; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import com.sina.weibo.sdk.api.imageobject; import com.sina.weibo.sdk.api.textobject; import com.sina.weibo.sdk.api.weibomultimessage; import com.sina.weibo.sdk.api.share.iweiboshareapi; import com.sina.weibo.sdk.api.share.sendmultimessagetoweiborequest; import com.sina.weibo.sdk.api.share.weibosharesdk; import com.sina.weibo.sdk.auth.authinfo; import com.sina.weibo.sdk.auth.oauth2accesstoken; import com.sina.weibo.sdk.auth.weiboauthlistener; import com.sina.weibo.sdk.exception.weiboexception; import com.tencent.connect.share.qqshare; import com.tencent.mm.sdk.modelmsg.sendmessagetowx; import com.tencent.mm.sdk.modelmsg.wxmediamessage; import com.tencent.mm.sdk.modelmsg.wxwebpageobject; import com.tencent.mm.sdk.openapi.iwxapi; import com.tencent.mm.sdk.openapi.wxapifactory; import com.tencent.tauth.iuilistener; import com.tencent.tauth.tencent; import java.util.list; import cn.hnshangyu.maker.r; import cn.hnshangyu.maker.weibo.accesstokenkeeper; import cn.hnshangyu.maker.weibo.constants; /** * ============================================ * 文件名:shareutil.java * 日期:2016-08-17 14:58 * 描述:分享帮助类 * 版本:1.0 * * ============================================ */ public class shareutil { /** * 微博微博分享接口实例 */ public static iweiboshareapi mweiboshareapi; //微信分享 public static final string weixin_share_app_id = "wx0c********88488d"; public static iwxapi weixin_api; public static final string qq_app_id = "11****5659"; public static tencent mtencent; public static void shareqq(activity activity, string sharepath, iuilistener listener) { //qq分享 mtencent = tencent.createinstance(qq_app_id, activity); if (isqqclientavailable(activity)) { // string content = activity.getresources().getstring(r.string.ivqrcode_content); final bundle params = new bundle(); params.putint(qqshare.share_to_qq_key_type, qqshare.share_to_qq_type_default); params.putstring(qqshare.share_to_qq_title, "######"); params.putstring(qqshare.share_to_qq_summary, "#############..."); params.putstring(qqshare.share_to_qq_target_url, sharepath); // params.putstring(qqshare.share_to_qq_image_url, "https://www.baidu.com/img/bd_logo1.png"); mtencent.sharetoqq(activity, params, listener); } else { uiutils.showtoast(activity, "请检查是否安装最新版qq!"); } } /** * 判断qq是否可用 * * @param context * @return */ private static boolean isqqclientavailable(context context) { final packagemanager packagemanager = context.getpackagemanager(); list<packageinfo> pinfo = packagemanager.getinstalledpackages(0); if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { string pn = pinfo.get(i).packagename; if (pn.equals("com.tencent.mobileqq")) { return true; } } } //**如果返回false qq的轻聊版也会显示安装新版本,true他会自动下载新版本 return true; } public static void shareweibo(final activity activity, string sharepath) { //微博 mweiboshareapi = weibosharesdk.createweiboapi(activity, constants.app_key); // 1. 初始化微博的分享消息 weibomultimessage weibomessage = new weibomultimessage(); weibomessage.mediaobject = getimageobj(activity); weibomessage.textobject = gettextobj(activity, sharepath); sendmultimessagetoweiborequest request = new sendmultimessagetoweiborequest(); // 用transaction唯一标识一个请求 request.transaction = string.valueof(system.currenttimemillis()); request.multimessage = weibomessage; if (mweiboshareapi.isweiboappinstalled() && mweiboshareapi.isweiboappsupportapi()) { mweiboshareapi.registerapp(); // 将应用注册到微博客户端 // 3. 发送请求消息到微博,唤起微博分享界面 mweiboshareapi.sendrequest(activity, request); } else { authinfo mauthinfo = new authinfo(activity, constants.app_key, constants.redirect_url, constants.scope); oauth2accesstoken accesstoken = accesstokenkeeper.readaccesstoken(activity); string token = ""; if (accesstoken != null) { token = accesstoken.gettoken(); } mweiboshareapi.sendrequest(activity, request, mauthinfo, token, new weiboauthlistener() { @override public void oncomplete(bundle bundle) { oauth2accesstoken newtoken = oauth2accesstoken.parseaccesstoken(bundle); accesstokenkeeper.writeaccesstoken(activity, newtoken); uiutils.showtoast(activity, "onauthorizecomplete token = " + newtoken.gettoken()); } @override public void onweiboexception(weiboexception e) { uiutils.showtoast(activity, "分享出错"); } @override public void oncancel() { uiutils.showtoast(activity, "分享取消"); } }); } } private static textobject gettextobj(activity activity, string sharepath) { textobject textobject = new textobject(); textobject.text = "##################...@" + sharepath; bitmap bitmap = bitmapfactory.decoderesource(activity.getresources(), r.mipmap.ic_launcher); textobject.setthumbimage(bitmap); return textobject; } /** * 创建图片消息对象。 * * @return 图片消息对象。 */ private static imageobject getimageobj(activity activity) { imageobject imageobject = new imageobject(); // bitmapdrawable bitmapdrawable = (bitmapdrawable) mimageview.getdrawable(); //设置缩略图。 注意:最终压缩过的缩略图大小不得超过 32kb。 bitmap bitmap = bitmapfactory.decoderesource(activity.getresources(), r.mipmap.ic_launcher); imageobject.setimageobject(bitmap); return imageobject; } static sendmessagetowx.req req; private static final int thumb_size = 150; public static void sharewechatcircle(activity activity, string sharepath) { if (linkweixin(activity, sharepath)) { req.scene = sendmessagetowx.req.wxscenetimeline; weixin_api.sendreq(req); } } public static void sharewechat(activity activity, string sharepath) { if (linkweixin(activity, sharepath)) { req.scene = sendmessagetowx.req.wxscenesession; weixin_api.sendreq(req); } } //微信分享 private static boolean linkweixin(activity activity, string sharepath) { // 通过wxapifactory工厂,获取iwxapi的实例 weixin_api = wxapifactory.createwxapi(activity, weixin_share_app_id, true); if (weixin_api.iswxappinstalled() && weixin_api.iswxappsupportapi()) { weixin_api.registerapp(weixin_share_app_id); wxwebpageobject webpageobject = new wxwebpageobject(); webpageobject.webpageurl = sharepath; wxmediamessage msg = new wxmediamessage(); msg.mediaobject = webpageobject; bitmap bitmap = bitmapfactory.decoderesource(activity.getresources(), r.mipmap.ic_launcher); msg.setthumbimage(bitmap); msg.title = "######"; msg.description = "###################..."; req = new sendmessagetowx.req(); req.transaction = buildtransaction("webpage"); req.message = msg; return true; } else { uiutils.showtoast(activity, "请检查是否安装最新版微信!"); return false; } } private static string buildtransaction(final string type) { return (type == null) ? string.valueof(system.currenttimemillis()) : type + system.currenttimemillis(); } }
三、分享界面
import android.content.intent; import android.graphics.bitmap; import android.os.bundle; import android.support.annotation.nullable; import android.view.view; import android.widget.imageview; import com.google.zxing.writerexception; import com.sina.weibo.sdk.api.share.baseresponse; import com.sina.weibo.sdk.api.share.iweibohandler; import com.sina.weibo.sdk.api.share.iweiboshareapi; import com.sina.weibo.sdk.api.share.weibosharesdk; import com.sina.weibo.sdk.constant.wbconstants; import com.tencent.tauth.iuilistener; import com.tencent.tauth.tencent; import com.tencent.tauth.uierror; import butterknife.bindview; import cn.hnshangyu.maker.r; import cn.hnshangyu.maker.base.baseactivity; import cn.hnshangyu.maker.ui.personal.zxing.encodinghandler; import cn.hnshangyu.maker.util.shareutil; import cn.hnshangyu.maker.util.uiutils; import cn.hnshangyu.maker.weibo.constants; public class invitefriendsactivity extends baseactivity implements iweibohandler.response { @bindview(r.id.iv_qrcode) imageview ivqrcode; @bindview(r.id.iv_share_qq) imageview ivshareqq; @bindview(r.id.iv_share_weibo) imageview ivshareweibo; @bindview(r.id.iv_share_weixin) imageview ivshareweixin; @bindview(r.id.iv_share_circle_friends) imageview ivsharecirclefriends; private bitmap qrcodebitmap; /** * 微博微博分享接口实例 */ private iweiboshareapi mweiboshareapi = null; //分享链接 private string sharepath; @override protected int getlayoutid() { return r.layout.activity_invite_friends; } @override protected void onnewintent(intent intent) { super.onnewintent(intent); // 从当前应用唤起微博并进行分享后,返回到当前应用时,需要在此处调用该函数 // 来接收微博客户端返回的数据;执行成功,返回 true,并调用 // {@link iweibohandler.response#onresponse};失败返回 false,不调用上述回调 mweiboshareapi.handleweiboresponse(intent, this); } /** * 初始化控件(必须实现此方法) */ @override protected void initview() { sharepath = getresources().getstring(r.string.ivqrcode_content); onshowtopback(false); onshowtitleback(true); settitletext(r.string.invite_friends); showqrcode(); // 创建微博分享接口实例 mweiboshareapi = weibosharesdk.createweiboapi(this, constants.app_key); // 注册第三方应用到微博客户端中,注册成功后该应用将显示在微博的应用列表中。 // 但该附件栏集成分享权限需要合作申请,详情请查看 demo 提示 // note:请务必提前注册,即界面初始化的时候或是应用程序初始化时,进行注册 mweiboshareapi.registerapp(); initlistener(); } @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); // 当 activity 被重新初始化时(该 activity 处于后台时,可能会由于内存不足被杀掉了), // 需要调用 {@link iweiboshareapi#handleweiboresponse} 来接收微博客户端返回的数据。 // 执行成功,返回 true,并调用 {@link iweibohandler.response#onresponse}; // 失败返回 false,不调用上述回调 if (savedinstancestate != null) { mweiboshareapi.handleweiboresponse(getintent(), this); } } private void showqrcode() { //生成二维码图片,第一个参数是二维码的内容,第二个参数是正方形图片的边长,单位是像素 // string content = "http://m.360buyimg.com/mobilecms/s300x98_jfs/t1363/77/1381395719/60705/ce91ad5c/55dd271an49efd216.jpg"; // content = getresources().getstring(r.string.ivqrcode_content); try { qrcodebitmap = encodinghandler.createqrcode(sharepath, 400); ivqrcode.setimagebitmap(qrcodebitmap); } catch (writerexception e) { e.printstacktrace(); } } private void initlistener() { ivshareqq.setonclicklistener(this); ivshareweibo.setonclicklistener(this); ivshareweixin.setonclicklistener(this); ivsharecirclefriends.setonclicklistener(this); } @override protected void oninitclick(view v) { super.oninitclick(v); switch (v.getid()) { case r.id.iv_share_qq: shareutil.shareqq(mactivity, sharepath, sharelistener); break; case r.id.iv_share_weibo: shareutil.shareweibo(mactivity, sharepath); break; case r.id.iv_share_weixin: shareutil.sharewechat(mactivity, sharepath); break; case r.id.iv_share_circle_friends: shareutil.sharewechatcircle(mactivity, sharepath); break; } } @override public void onresponse(baseresponse baseresponse) { if (baseresponse != null) { switch (baseresponse.errcode) { case wbconstants.errorcode.err_ok: uiutils.showtoast(mcontext, "分享成功"); break; case wbconstants.errorcode.err_cancel: uiutils.showtoast(mcontext, "分享取消"); break; case wbconstants.errorcode.err_fail: uiutils.showtoast(mcontext, "分享失败"); break; } } } private iuilistener sharelistener = new iuilistener() { @override public void oncomplete(object o) { uiutils.showtoast(mcontext, "分享成功"); } @override public void onerror(uierror uierror) { uiutils.showtoast(mcontext, "分享出错"); } @override public void oncancel() { } }; protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == com.tencent.connect.common.constants.request_qq_share) { tencent.onactivityresultdata(requestcode, resultcode, data, sharelistener); } } }
四、constants类
/** * 该类定义了微博授权时所需要的参数。 */ public interface constants { /** * 当前 应用的 app_key,第三方应用应该使用自己的 app_key 替换该 app_key */ public static final string app_key = "16#####007"; /** * 当前 应用的回调页,第三方应用可以使用自己的回调页。 * <p/> * <p> * 注:关于授权回调页对移动客户端应用来说对用户是不可见的,所以定义为何种形式都将不影响, * 但是没有定义将无法使用 sdk 认证登录。 * 建议使用默认回调页:https://api.weibo.com/oauth2/default.html * </p> */ // public static final string redirect_url = "http://www.sina.com"; public static final string redirect_url = "https://api.weibo.com/oauth2/default.html"; /** * scope 是 oauth2.0 授权机制中 authorize 接口的一个参数。通过 scope,平台将开放更多的微博 * 核心功能给开发者,同时也加强用户隐私保护,提升了用户体验,用户在新 oauth2.0 授权页中有权利 * 选择赋予应用的功能。 * <p/> * 我们通过新浪微博开放平台-->管理中心-->我的应用-->接口管理处,能看到我们目前已有哪些接口的 * 使用权限,高级权限需要进行申请。 * <p/> * 目前 scope 支持传入多个 scope 权限,用逗号分隔。 * <p/> * 有关哪些 openapi 需要权限申请,请查看:http://open.weibo.com/wiki/%e5%be%ae%e5%8d%9aapi * 关于 scope 概念及注意事项,请查看:http://open.weibo.com/wiki/scope */ public static final string scope = "email,direct_messages_read,direct_messages_write," + "friendships_groups_read,friendships_groups_write,statuses_to_me_read," + "follow_app_official_microblog," + "invitation_write"; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!