1. 程式人生 > >ShareSdk實現QQ或微信分享和登陸

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包整合方式,雖然麻煩但是可以直接操作很多功能