安卓QQ登录

有兴趣的可以看下我另一篇文章:安卓微信登录

有关资源的Jar包:QQ登录jar包

1.首先在manifest里添加参数:

      <!--QQ登录配置 start-->
        <activity
            android:name="com.tencent.tauth.AuthActivity"
            android:launchMode="singleTask"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <!--222222是腾讯的测试接口,可替换成你申请的APPId如:1106210336-->
                <data android:scheme="tencent222222" />
            </intent-filter>
        </activity>
        <!--SDK_V2.0引入了AssistActivity,开发者需在androidManifest.xml中注册。代码如下:-->
        <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <!--QQ登录配置 end-->

2.登录前准备


2.1先注册Tencent:

private Tencent mTencent;
// Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。
// 其中APP_ID是分配给第三方应用的appid如:1106210336,类型为String,我现在用的是官网测试id:222222
mTencent = Tencent.createInstance("1106210336", this.getApplicationContext());
//如果session无效,就开始做登录操作
if (!mTencent.isSessionValid()) {
    qqLogin();
}

2.2开始登录并得到数据:

   /*qq登录*/
    private void qqLogin() {
        //返回数据的监听
        IUiListener qqListen = new IUiListener() {
            @Override
            public void onComplete(Object o) {
                Logs.v(tag + "63 " + o.toString() + "  openId:" + mTencent.getOpenId());
                JSONObject jsonObject = (JSONObject) o;
                try {
                    String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
                    String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
                    String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
                    if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(openId)) {
                        mTencent.setAccessToken(token, expires);
                        mTencent.setOpenId(openId);
                        QQ_uid = openId;
                        getQQMsg();
                    }
                } catch (Exception e) {
                    Logs.e(tag+"77 "+e);
                }
            }

            @Override
            public void onError(UiError uiError) {
                Logs.e(tag + "81 " + uiError.errorDetail);
            }

            @Override
            public void onCancel() {
                ToastUtil.showShort("取消了");
                Logs.d(tag + "87  取消了");
            }
        };
        //注销登录mTencent.logout(this);
   /*     通过这行代码,SDK实现了QQ登录;第二个参出是一个表示获得哪些权限的字符串:
        如:"get userinfo,add t"多个权限用逗号隔开,all代表所有权限。*/
        mTencent.login(Thirdlogin.this, "all", qqListen);
    }

    /*获取个人信息数据*/
    private void getQQMsg() {
        UserInfo userInfo = new UserInfo(this, mTencent.getQQToken());
        userInfo.getUserInfo(new IUiListener() {
            @Override
            public void onComplete(Object o) {
                Logs.i(tag + "100 " + o.toString());
                tv.setText(o.toString());//打印获取个人信息数据
            }

            @Override
            public void onError(UiError uiError) {
                Logs.e(tag + "106 " + uiError.errorDetail);
            }

            @Override
            public void onCancel() {
                ToastUtil.showShort("取消了");
                Logs.d(tag + "112  取消了");
            }
        });
    }


3.登入成功后获取的数据

登录成功后调用public void onComplete(JSONObject arg0) 回传的JsonObject, 其中包含OpenId, AccessToken等重要数据。
{
"ret":0,
"pay_token":"xxxxxxxxxxxxxxxx",
"pf":"openmobile_android",
"expires_in":"7776000",
"openid":"xxxxxxxxxxxxxxxxxxx",
"pfkey":"xxxxxxxxxxxxxxxxxxx",
"msg":"sucess",
"access_token":"xxxxxxxxxxxxxxxxxxxxx"
}


4.个人信息

{
"is_yellow_year_vip": "0",
"ret": 0,
"figureurl_qq_1":
"http://q.qlogo.cn/qqapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/40",
"figureurl_qq_2":
"http://q.qlogo.cn/qqapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/100",
"nickname": "小罗",
"yellow_vip_level": "0",
"msg": "",
"figureurl_1":
"http://qzapp.qlogo.cn/qzapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/50",
"vip": "0",
"level": "0",
"figureurl_2":
"http://qzapp.qlogo.cn/qzapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/100",
"is_yellow_vip": "0",
"gender": "男",
"figureurl":
"http://qzapp.qlogo.cn/qzapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/30"
}



猜你喜欢

转载自blog.csdn.net/xxdw1992/article/details/80631587