ShareSdk实现QQ或微信分享和登陆

下面是比较简单的 QQ 分享和登陆 Demo,集成方式为快速集成(Gradle)。下面主要包括:

  • 根目录配置
  • 项目目录配置
  • 代码配置及调试
  • 注意事项

1. 根目录配置

在根目录添加代码:

classpath 'com.mob.sdk:MobSDK:+'

如下图所示,是项目的根目录:

2. 项目目录配置

项目的 gradele 配置,添加了两个地方,分别为

//ShareSDK添加内容1
apply plugin: 'com.mob.sdk'
//ShareSDK添加内容2
MobSDK {
    appKey "你的appkey"
    appSecret "你的appSecret"

    ShareSDK {
        //平台配置信息
        devInfo {
            SinaWeibo {
                appKey "568898243"
                appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
                callbackUri "http://www.sharesdk.cn"
                shareByAppClient false
            }
            Wechat {
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
            }
            QQ {
                appId "100371282"
                appKey "aed9b0303e3ed1e27bae87c33761161d"
            }
            Wechat {
                id 4
                sortId 4
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
                userName "gh_afb25ac019c9"
                path "pages/index/index.html?id=1"
                withShareTicket true
                miniprogramType 0
                bypassApproval false
                enable true
            }
            WechatMoments {
                id 5
                sortId 5
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
                bypassApproval false
                enable true
            }
        }
    }
}

为了演示,上面只是添加了微博,QQ,微信,微信朋友圈,如果还有别的:点我添加更多

为了方便观察,下面给出我的 gradle 完整代码:

apply plugin: 'com.android.application'

//ShareSDK添加内容1
apply plugin: 'com.mob.sdk'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.qd.sharesdkdemo"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

//ShareSDK添加内容2
MobSDK {
    appKey "你的appkey"
    appSecret "你的appSecret"

    ShareSDK {
        //平台配置信息
        devInfo {
            SinaWeibo {
                appKey "568898243"
                appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
                callbackUri "http://www.sharesdk.cn"
                shareByAppClient false
            }
            Wechat {
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
            }
            QQ {
                appId "100371282"
                appKey "aed9b0303e3ed1e27bae87c33761161d"
            }
            Wechat {
                id 4
                sortId 4
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
                userName "gh_afb25ac019c9"
                path "pages/index/index.html?id=1"
                withShareTicket true
                miniprogramType 0
                bypassApproval false
                enable true
            }
            WechatMoments {
                id 5
                sortId 5
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
                bypassApproval false
                enable true
            }
        }
    }

}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    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'
}

要注意的是,是添加在项目所在的 gradle,不要添加错位置了,如下图:

3. 代码配置及调试

QQ登陆在 Activity 中添加代码:

    private String weChatData = "";//微信登陆获取的数据
    //在你需要登陆的时候调用此方法既可    
    private void loginWeChat() {
        //qq登陆传值QQ.NAME
        final Platform weChat = ShareSDK.getPlatform(QQ.NAME);
        if (weChat.isAuthValid()) {
            weChat.removeAccount(true);
            Toast.makeText(this, "微信登陆", Toast.LENGTH_SHORT).show();
        }
        weChat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, final HashMap<String, Object> hashMap) {
                Log.e("=====", "onComplete: ");
                weChatData = platform.getDb().exportData();//取到所有数据
                if (weChatData != null) {
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                } else {
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.e("=====", "onError: ");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.e("=====", "onCancel: ");
            }
        });
        weChat.SSOSetting(false);
        weChat.showUser(null);
    }

    //要用Handler回到主线程操作UI,否则会报错
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                //微信登陆
                case 1:
                    QQWeChatBind("weixin", weChatData);
                    break;
            }
        }
    };

    //登陆成功,在主页面操作
    private void QQWeChatBind(String type, String data) {
        Log.e("====data", data);
    }

QQ分享在 Activity 中添加代码:

    //在需要的地方调用分享方法即可
    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        //分享平台(此处为QQ)
        oks.setPlatform(QQ.NAME);
        //分享图片
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        //分享标题
        oks.setTitle("标题");
        //分享标题点击链接
        oks.setTitleUrl("[图片]http://www.sina.com/");
        //分享点击链接
        oks.setUrl("http://sharesdk.cn");
        //分享内容
        oks.setText("我是测试评论文本");
        // 启动分享GUI
        oks.show(this);
        //成功失败回调(不用可以不写)
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                //成功回调
                Log.d("ShareSDK", "onComplete ---->  分享成功");
                platform.isClientValid();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                //失败回调
                Log.d("ShareSDK", "onError ---->  分享失败" + throwable.getStackTrace().toString());
                Log.d("ShareSDK", "onError ---->  分享失败" + throwable.getMessage());
                throwable.getMessage();
                throwable.printStackTrace();
            }

            @Override
            public void onCancel(Platform platform, int i) {
                //取消回调
                Log.d("ShareSDK", "onCancel ---->  分享取消");
            }
        });
    }

4. 注意事项

1. QQ 分享和登陆可以直接运行进行调试,但是微信分享和登陆必须要打包签名才可以调试。

2. 快速集成文档

3. 登陆实现方法(必须完成步骤2才可以进行)

4. jar包集成方式,虽然麻烦但是可以直接操作很多功能

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页