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="http://schemas.android.com/apk/res/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" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


        <!-- register SDKActivity -->
        <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" />
                <data android:scheme="tencent1105602574" /> <!-- Acquired by open platform APPID -->
            </intent-filter>
        </activity>
        <activity android:name="com.tencent.connect.common.AssistActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:screenOrientation="portrait"/>
    </application>

</manifest>
Layout file activity_main Just one Button Button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click QQ Sign in"
        android:onClick="buttonLogin"
        android:layout_centerInParent="true"
        android:textSize="16sp"
        android:textColor="#f4736e"/>
</RelativeLayout>
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.support.v7.app.AppCompatActivity;
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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //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 {

        @Override
        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.setOpenId(openID);
                mTencent.setAccessToken(accessToken, expires);
                QQToken qqToken = mTencent.getQQToken();
                mUserInfo = new UserInfo(getApplicationContext(), qqToken);
                mUserInfo.getUserInfo(new IUiListener() {
                    @Override
                    public void onComplete(Object response) {
                        Log.e(TAG, "Login successful" + response.toString());
                    }

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

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

                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

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

        }

        @Override
        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
     */
    @Override
    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