封装分享至微信、QQ功能

背景
在不适用第三方的前提下,实现可用于微信、QQ分享的功能
前期准备
前往腾讯开放平台进行账号注册获取所需的AppId;具体如何获取便不加赘述了,有疑问可自行百度
微信分享只需要在build.gradle文件中,添加如下依赖即可:

dependencies {
    compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}

QQ分享需下载jar包然后引入工程(下载传送门
引入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));

demo入口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DerMing_You

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值