背景
在不适用第三方的前提下,实现可用于微信、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));