1、首先到腾讯开放平台创建应用,并上线——使用有效APP ID才可以进行qq登录
腾讯开放平台:https://open.tencent.com/
上线后APP ID
2、下载腾讯sdk:https://download.csdn.net/download/meixi_android/10842092
3、activity代码详情:
(1)实例化
public static Tencent mTencent;
mTencent = Tencent.createInstance(ThirdKey.QQ_APP_ID, mActivity);
//public static final String QQ_APP_ID = "1104728100";
(2)登录监听
/*** 登陆监听*/IUiListener loginListener = new BaseUiListener() {@Overrideprotected void doComplete(JSONObject values) {try {Log.i("lgq","sssssss==="+values);String strAccessToken = values.getString("access_token");String strExpiresIn = values.getString("expires_in");String strOpenid = values.getString("openid");mTencent.getQQToken().setOpenId(strOpenid);mTencent.getQQToken().setAccessToken(strAccessToken, strExpiresIn);UserInfo info = new UserInfo(mActivity, mTencent.getQQToken());info.getUserInfo(new IUiListener() {@Overridepublic void onComplete(Object o) {try {Log.i("lgq","...... "+o.toString());JSONObject jsonObject = new JSONObject(o.toString());String nickname = jsonObject.getString("nickname");String avatar = jsonObject.getString("figureurl_2");
// setCompleteCallBack(new User(mTencent.getOpenId(), nickname, avatar));} catch (JSONException e) {
// setErrorCallBack(e.getMessage());}}@Overridepublic void onError(UiError uiError) {
// setErrorCallBack(uiError.errorMessage);}@Overridepublic void onCancel() {
// setCancelCallBack();}});// if (thirdLoginListener != null)
// thirdLoginListener.onComplete(ThirdLoginActivity.THIRD_QQ, strOpenid, strAccessToken, strExpiresIn);} catch (Exception e) {}loginOut();}};
回调成功返回数据打印:
12-11 09:47:05.197 9796-9796/com.zhenhua.online I/lgq: sssssss==={"ret":0,"openid":"D205861A27D46BC68DB3541E460A3089","access_token":"7D48C984236BDB6828DE74DFB8553CA4","pay_token":"32901E5EE08F51F959A16FA1F065EB53","expires_in":7776000,"pf":"desktop_m_qq-10000144-android-2002-","pfkey":"2801ea0dfca53cfa89c13a43b9d06208","msg":"","login_cost":447,"query_authority_cost":131,"authority_cost":0}
12-11 09:47:05.583 9796-9796/com.zhenhua.online I/lgq: ...... {"ret":0,"msg":"","is_lost":0,"nickname":"310225955","gender":"男","province":"广东","city":"珠海","year":"2002","constellation":"","figureurl":"http:\/\/qzapp.qlogo.cn\/qzapp\/1104728100\/D205861A27D46BC68DB3541E460A3089\/30","figureurl_1":"http:\/\/qzapp.qlogo.cn\/qzapp\/1104728100\/D205861A27D46BC68DB3541E460A3089\/50","figureurl_2":"http:\/\/qzapp.qlogo.cn\/qzapp\/1104728100\/D205861A27D46BC68DB3541E460A3089\/100","figureurl_qq_1":"http:\/\/thirdqq.qlogo.cn\/qqapp\/1104728100\/D205861A27D46BC68DB3541E460A3089\/40","figureurl_qq_2":"http:\/\/thirdqq.qlogo.cn\/qqapp\/1104728100\/D205861A27D46BC68DB3541E460A3089\/100","is_yellow_vip":"0","vip":"0","yellow_vip_level":"0","level":"0","is_yellow_year_vip":"0"}
使用这些qq信息即可链接项目后台进行登录
(4)登录方法
<string name="error_third_no_qq">对不起!请先安装QQ客户端~</string>
public void login() {if (mTencent.isSupportSSOLogin(mActivity)) {if (mTencent.isSessionValid())mTencent.logout(mActivity);mTencent.login(mActivity, "get_user_info", loginListener);
// loadingDialog.show();} elseToastUtil.showToastShort(R.string.error_third_no_qq);}
(5)启动qq成功
在线回复bug解决:qq1085220040