Android uses Android studio to realize third-party QQ login

Two words don't say, effect picture!!

This is the demo page

This is the login page

This is the post login page

Now the third-party login is very common, such as QQ, Weibo, wechat. Today, we will realize how to access QQ to login into our project
We can also log in to a third party. However, today, we do not use our company or Tencent open platform, because I did not sign up for "embarrassed"
Select Android platform to get APPID and APPKEY. After creation, we need to SDKJar package download address
Download the SDKJar package, then you can realize QQ login, create a new project named QQLogin, extract the SDK we just downloaded, copy the jar package in the jar folder to our project libs

This is a good jar package to download

Start importing jar package

So far, the preparations are over
Here's the logic

Open our Androidmanifest Add permissions and registration to it Activity as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="" package="com.tangxiaoying.qq2">

    <!-- QQ Required permissions for login authorization -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

        <!-- register SDKActivity -->
            android:noHistory="true" >
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tencent1105602574" /> <!-- Acquired by open platform APPID -->
        <activity android:name="com.tencent.connect.common.AssistActivity"

Layout file activity_main Just one Button Button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:text="click QQ Sign in"
Here's our MainActivity Code in
//Well, because it's really a third-party code, I don't know much about it. In order to learn and use it in the future, please give me more advice
package com.example.qqlogin;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final String APP_ID = "1105602574";//Official APPID
    private Tencent mTencent;
    private BaseUiListener mIUiListener;
    private UserInfo mUserInfo;

    protected void onCreate(Bundle savedInstanceState) {
        //Incoming parameters APPID and global Context context
        mTencent = Tencent.createInstance(APP_ID, MainActivity.this.getApplicationContext());

    public void buttonLogin(View v) {
        /**Through this code, the SDK implements QQ login. This method has three parameters. The first parameter is context context, and the second parameter, SCOPO, is a String type String, indicating some permissions
         Instructions in the official document: which API permissions the application needs to obtain are separated by ",". For example: SCOPE = "get user info, add T"; use "all" for all permissions
         The third parameter is an event listener, an instance of the IUiListener interface, where the implementation class of the interface is used */
        mIUiListener = new BaseUiListener();
        //All means get all permissions
        mTencent.login(MainActivity.this, "all", mIUiListener);

     * Three methods need to be implemented after implementing IUiListener interface with custom listener
     * onComplete Complete onError error onCancel cancel
    private class BaseUiListener implements IUiListener {

        public void onComplete(Object response) {
            Toast.makeText(MainActivity.this, "Authorization successful", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "response:" + response);
            JSONObject obj = (JSONObject) response;
            try {
                String openID = obj.getString("openid");
                String accessToken = obj.getString("access_token");
                String expires = obj.getString("expires_in");
                mTencent.setAccessToken(accessToken, expires);
                QQToken qqToken = mTencent.getQQToken();
                mUserInfo = new UserInfo(getApplicationContext(), qqToken);
                mUserInfo.getUserInfo(new IUiListener() {
                    public void onComplete(Object response) {
                        Log.e(TAG, "Login successful" + response.toString());

                    public void onError(UiError uiError) {
                        Log.e(TAG, "Login failed" + uiError.toString());

                    public void onCancel() {
                        Log.e(TAG, "Sign in cancellation");

            } catch (JSONException e) {

        public void onError(UiError uiError) {
            Toast.makeText(MainActivity.this, "privilege grant failed", Toast.LENGTH_SHORT).show();


        public void onCancel() {
            Toast.makeText(MainActivity.this, "Cancellation of authorization", Toast.LENGTH_SHORT).show();



     * Override onActivityResult method in Activity or Fragment calling Login
     * @param requestCode
     * @param resultCode
     * @param data
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_LOGIN) {
            Tencent.onActivityResultData(requestCode, resultCode, data, mIUiListener);
        super.onActivityResult(requestCode, resultCode, data);

Tags: Android SDK xml encoding

Posted on Mon, 04 May 2020 01:11:29 -0400 by neal.pressley