封装分享至微信、QQ功能
2019-03-14 17:07
477 查看
背景
在不适用第三方的前提下,实现可用于微信、QQ分享的功能
前期准备
前往腾讯开放平台进行账号注册获取所需的AppId;具体如何获取便不加赘述了,有疑问可自行百度。
微信分享只需要在build.gradle文件中,添加如下依赖即可:
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' }
QQ分享需下载jar包然后引入工程(下载传送门)
引入jar包,并添加引用
是否成功引入可以到build文件中查看
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' ... implementation 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' implementation files('libs/mta-sdk-1.6.2.jar') implementation files('libs/open_sdk_r5756.jar') implementation 'org.greenrobot:eventbus:3.1.1' }
用于分享的实体类
public class ShareBean { private String title; private String subtitle; private String imgUrl; private String appUrl; private String shareTitle; private String qqAppName; private boolean otherOpera; /** * 不修改布局中的分享标题(默认使用) * @param title * @param subtitle * @param imgUrl * @param appUrl */ public ShareBean(String title, String subtitle, String imgUrl, String appUrl) { this.title = title; this.subtitle = subtitle; this.imgUrl = imgUrl; this.appUrl = appUrl; } /** * QQ分享成功以后 是否有其他操作 * @param title * @param subtitle * @param imgUrl * @param appUrl * @param shareTitle * @param otherOpera true 有其他操作 false 没有其他操作 */ public ShareBean(String title, String subtitle, String imgUrl, String appUrl, String shareTitle , boolean otherOpera) { this.title = title; this.subtitle = subtitle; this.imgUrl = imgUrl; this.appUrl = appUrl; this.shareTitle = shareTitle; this.otherOpera = otherOpera; } public String getAppUrl() { return appUrl; } public void setAppUrl(String appUrl) { this.appUrl = appUrl; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSubtitle() { return subtitle; } public void setSubtitle(String subtitle) { this.subtitle = subtitle; } public String getShareTitle() { return shareTitle; } public void setShareTitle(String shareTitle) { this.shareTitle = shareTitle; } public String getQqAppName() { return qqAppName; } public void setQqAppName(String qqAppName) { this.qqAppName = qqAppName; } public boolean isOtherOpera() { return otherOpera; } public void setOtherOpera(boolean otherOpera) { this.otherOpera = otherOpera; } }
实现回调 IUiListener
public class ShareUiListener implements IUiListener { private final Context mContext; private boolean mOtherOpera; public ShareUiListener(Context context){ mContext=context; } /** * * @param context * @param otherOpera true 分享成功有其他操作 false 没有其他操作 */ public ShareUiListener(Context context,boolean otherOpera){ mContext=context; mOtherOpera=otherOpera; } /** * 分享成功 * * @param o */ @Override public void onComplete(Object o) { if (!mOtherOpera){ ToastUtils.showShortToast(mContext, "分享成功"); }else { EventBus.getDefault().post(new ShareEventBus(true)); } } /** * 分享取消 */ @Override public void onCancel() { ToastUtils.showShortToast(mContext, "取消分享"); } /** * 分享异常 * * @param uiError */ @Override public void onError(UiError uiError) { ToastUtils.showShortToast(mContext, uiError.errorMessage); } }
定义用于分享的弹框 ShareDialog(可到Demo中查看,欢迎指出不恰当之处)
然后只需要在想分享的地方之间调用就行
new ShareDialog().show(mContext, new ShareBean(getString(R.string.share_title), getString(R.string.share_sub_title), shareImageUrl, encodeUrl));
相关文章推荐
- andriod 实现新浪、QQ场地、朋友微信圈、微信朋友分享功能
- [置顶] 在Android中使App快速、简单地支持新浪微博、微信、QQ、facebook等十几个主流社交平台的分享功能
- android 微信 以及QQ的SDK接入分享功能遇到的些问题小结
- HTML5 实现分享功能(QQ 微信 微博)
- android(微博 微信 qq) 分享和第三分认证登录的封装
- 苹果审核reject理由大全(12):qq、微信的登录、分享等功能
- Ionic 分享功能(微博 微信 QQ)
- [置顶] android studio 打包签名apk,完成微信和qq的的第三方分享功能
- Android实现截图分享qq 微信功能
- 在Android中使App快速、简单地支持新浪微博、微信、QQ、facebook等十几个主流社交平台的分享功能
- 关于android微信QQ等平台分享功能实现中遇到的问题处理
- 网页分享功能 支持 微信二维码 qq空间 qq好友 新浪微博 百度贴吧 豆瓣 人人
- 关于QQ、新浪微博、微信的分享功能的实现
- 微信 qq微博分享功能
- 在Android中使App快速、简单地支持新浪微博、微信、QQ、facebook等十几个主流社交平台的分享功能
- 获取第三方登录(相关信息 新浪微博&微信&QQ)&&登录和分享接口功能
- 微信js-sdk分享功能接口常用逻辑封装示例
- Android微博,微信,QQ分享功能的实现
- 微信、qq、微博第三方分享集成封装代码
- Android 微信分享与QQ分享功能