安卓QQ第三方登陆

1、下载sdk:https://wiki.open.qq.com/wiki/mobile/SDK下载
2、将open-sdk.jar放到libs中 添加依赖implementation fileTree(dir: 'libs', include: '*.jar')
3.配置AndroidManifest


    
           
           
           
           //把AppId替换成你的AppId
    
 

4、权限

    
    
    

5、初始化

//把AppId替换成你的AppId
Tencent  mTencent = Tencent.createInstance("AppId",  getApplicationContext(),"");

6、登陆

  mTencent.login(this, "all", new LoginUiListener());

7、授权登录监听

private class LoginUiListener implements IUiListener {
    
    @Override
    public void onComplete(Object response) {
        //获取openid
        try {
            openid= ((JSONObject) response).getString("openid");
            access_token = ((JSONObject) response).getString("access_token");
            expires_in = ((JSONObject) response).getString("expires_in");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(UiError uiError) {

    }

    @Override
    public void onCancel() {

    }
}
   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Tencent.onActivityResultData(requestCode, resultCode, data, new LoginUiListener ());
    }

8、 调用QQ注销接口

mTencent.logout(this);

9、获取QQ用户名称、性别等信息

     QQToken qqToken = mTencent.getQQToken();
            mTencent.setOpenId(openid);
            mTencent.setAccessToken(access_token, expires_in);
            UserInfo info = new UserInfo(getApplicationContext(), qqToken);
            info.getUserInfo(new IUiListener() {
                @Override
                public void onComplete(Object o) {
                    String nickname = ((JSONObject) o).optString("nickname");
                    String gender= ((JSONObject) o).optString("gender");
                }

                @Override
                public void onError(UiError uiError) {

                }

                @Override
                public void onCancel() {

                }
            });

Application节点下添加


10、QQ登陆官方文档

https://wiki.open.qq.com/index.php?title=Android_SDK环境搭建

你可能感兴趣的:(Android)