QQ第三方授权登录(带详细源码)

建议下载demo,照着开发文档看

1.首先要配置SDK,官网下载的,两个jar。

2.配置AndroidManifest.xml 的权限和两个Activity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iwtle.www.englishstudy">
<!--用户需要的权限-->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- SDK2.1新增获取用户位置信息 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-permission android:name="android.permission.GET_TASKS" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/studylogo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".ui.LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!--需要配置的两个Activity-->
        <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="Tencent101368807" />
                <!--这个APPid需要替换成自己的-->
            </intent-filter>
        </activity>

        <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="behind"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    </application>

</manifest>

 

3.编写登录的代码。

3.1首先需要在onCreate初始化sdk 

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTencent = Tencent.createInstance("101368807", this.getApplicationContext());
    }

3.2创建回调的函数。

IUiListener loginListener = new BaseUiListener() {
        @Override
        protected void doComplete(JSONObject values) {
            Log.d("SDKQQAgentPref", "AuthorSwitch_SDK:" + SystemClock.elapsedRealtime());
            System.out.println("123456" + values.toString());

        }
    };

    private class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object response) {
            if (null == response) {
                return;
            }
            JSONObject jsonResponse = (JSONObject) response;
            if (null != jsonResponse && jsonResponse.length() == 0) {
                return;
            }

            doComplete((JSONObject) response);
            System.out.println("123456789" + response.toString());

        }

        protected void doComplete(JSONObject values) {

        }

        @Override
        public void onError(UiError e) {
            System.out.println("12345678910" + e.errorDetail);

        }

        @Override
        public void onCancel() {
            System.out.println("12345678910" + "onCancel");

        }
    }

3.3还需要一个回调的方法(如果要成功接收到回调)

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("12346", "-->onActivityResult " + requestCode + " resultCode=" + resultCode);
        if (requestCode == Constants.REQUEST_LOGIN ||
                requestCode == Constants.REQUEST_APPBAR) {
            Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

3.4最后登录的代码

    public void Login(View view) {
        System.out.println("我要登录了!!");
        if (!mTencent.isSessionValid()) {
            mTencent.login(this, "all", loginListener);
        }
    }

4.登录页面的整体源码

public class MainActivity extends AppCompatActivity {

    private Tencent mTencent;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTencent = Tencent.createInstance("101368807", this.getApplicationContext());

    }


    public void Login(View view) {
        System.out.println("我要登录了!!");
        if (!mTencent.isSessionValid()) {
            mTencent.login(this, "all", loginListener);
        }
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("12346", "-->onActivityResult " + requestCode + " resultCode=" + resultCode);
        if (requestCode == Constants.REQUEST_LOGIN ||
                requestCode == Constants.REQUEST_APPBAR) {
            Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    IUiListener loginListener = new BaseUiListener() {
        @Override
        protected void doComplete(JSONObject values) { //获取数据
            Log.d("SDKQQAgentPref", "AuthorSwitch_SDK:" + SystemClock.elapsedRealtime());
            System.out.println("123456" + values.toString());
        }
    };

    private class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object response) { //登录成功获取数据
            if (null == response) {
                return;
            }
            JSONObject jsonResponse = (JSONObject) response;
            if (null != jsonResponse && jsonResponse.length() == 0) {
                return;
            }
            doComplete((JSONObject) response);
            System.out.println("登录成功" + response.toString());
            Toast.makeText(MainActivity.this, "登录成功" + response.toString(), Toast.LENGTH_LONG).show();
        }

        protected void doComplete(JSONObject values) {

        }

        @Override
        public void onError(UiError e) {//登录出错
            System.out.println("12345678910" + e.errorDetail);

        }

        @Override
        public void onCancel() { //取消登录
            System.out.println("12345678910" + "onCancel");
        }
    }

}

 

5.demo的网址。

链接:http://pan.baidu.com/s/1gfr6eDt 密码:2trk

 

我发现再好的教程,如果你只是搜到,而不去敲一遍。永远都不会去好好写的。

 

转载于:https://my.oschina.net/TAOH/blog/809448

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值