源码简介:因为涉及到客户的个人信息安全,所以需要大家根据这篇博客(http://blog.csdn.net/csdnfml/article/details/49446479)进行简单修改
源码效果:
源码片段:
- /** ------------------------QQ第三方登录-------------------- */
- public void loginQQ(){
- /** 判断是否登陆过 */
- if (!mTencent.isSessionValid()){
- mTencent.login(this, "all",loginListener);
- }/** 登陆过注销之后在登录 */
- else {
- mTencent.logout(this);
- mTencent.login(this, "all",loginListener);
- }
- }
- IUiListener loginListener = new BaseUiListener() {
- @Override
- protected void doComplete(JSONObject values) {
- initOpenidAndToken(values);
- updateUserInfo();
- }
- };
- /** QQ登录第二步:存储token和openid */
- public static void initOpenidAndToken(JSONObject jsonObject) {
- 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);
- }
- } catch(Exception e) {
- }
- }
- /** QQ登录第三步:获取用户信息 */
- private void updateUserInfo() {
- if (mTencent != null && mTencent.isSessionValid()) {
- IUiListener listener = new IUiListener() {
- @Override
- public void onError(UiError e) {
- Message msg = new Message();
- msg.obj = "把手机时间改成获取网络时间";
- msg.what = 1;
- mHandler.sendMessage(msg);
- }
- @Override
- public void onComplete(final Object response) {
- Message msg = new Message();
- msg.obj = response;
- msg.what = 0;
- mHandler.sendMessage(msg);
- }
- @Override
- public void onCancel() {
- Message msg = new Message();
- msg.obj = "获取用户信息失败";
- msg.what = 2;
- mHandler.sendMessage(msg);
- }
- };
- mInfo = new UserInfo(this, mTencent.getQQToken());
- mInfo.getUserInfo(listener);
- } else {
- }
- }
- Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- if (msg.what == 0) {
- JSONObject response = (JSONObject) msg.obj;
- if (response.has("nickname")) {
- try {
- log("获取用户信息成功,返回结果:"+response.toString());
- mThirdLoginResult.setText("登录成功\n"+"昵称:"+response.getString("nickname")+"\n头像地址:"+response.get("figureurl_qq_1"));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }else if(msg.what == 1){
- mThirdLoginResult.setText(msg+"");
- }else if(msg.what == 2){
- mThirdLoginResult.setText(msg+"");
- }
- }
- };
- /** QQ登录***步:获取token和openid */
- private class BaseUiListener implements IUiListener {
- @Override
- public void onComplete(Object response) {
- if (null == response) {
- mToast("登录失败");
- return;
- }
- JSONObject jsonResponse = (JSONObject) response;
- if (null != jsonResponse && jsonResponse.length() == 0) {
- mToast("登录失败");
- return;
- }
- log("QQ登录成功返回结果-" + response.toString());
- doComplete((JSONObject)response);
- }
- protected void doComplete(JSONObject response) {}
- @Override
- public void onError(UiError e) {
- Util.toastMessage(MainActivity.this, "onError: " + e.errorDetail);
- Util.dismissDialog();
- }
- @Override
- public void onCancel() {
- Util.toastMessage(MainActivity.this, "onCancel: ");
- Util.dismissDialog();
- if (isServerSideLogin) {
- isServerSideLogin = false;
- }
- }
- }
- /** -------------------------QQ第三方登录结束-------------------- */