Android-使用Android Studio实现第三方QQ登录
按照一步步来就可以完成
现在的第三方登录很普遍如QQ,微博,微信,今天我们就来实现如何接入QQ登录到我们的项目中
要想使用QQ登录我们需要到腾讯开放平台注册账号获取开发者资格地址:http://open.qq.com/ 注册完成后点击我们右上角的管理中心
进入管理中心后点击下面的创建应用
然后选择Android平台就会获取APPID和APPKEY创建完成后我们需要到 http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD 下载SDKJar包 接下来就可以实现QQ登录了新建一个项目工程名为QQLoginDemo 然后把我们刚才下载的SDK解压将jar文件夹中的jar包拷贝到我们的项目libs中
项目结构如下
打开我们的Androidmanifest 在里面加入权限和注册Activity 如下
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.shiran.qqlogindemo"> <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: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> <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" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:screenOrientation="portrait"/> </application></manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
布局文件activity_main 就一个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" tools:context="com.shiran.qqlogindemo.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击QQ登录" android:onClick="buttonLogin" android:layout_centerInParent="true" android:textSize="16sp" android:textColor="#f4736e"/></RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
下面就是我们的MainActivity中的代码了
package com.shiran.qqlogindemo;import android.content.Intent;import android.support.v7.app.AppCompatActivity;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"; private Tencent mTencent; private BaseUiListener mIUiListener; private UserInfo mUserInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTencent = Tencent.createInstance(APP_ID,MainActivity.this.getApplicationContext()); } public void buttonLogin(View v){ /**通过这句代码,SDK实现了QQ的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限 官方文档中的说明:应用需要获得哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有权限用“all” 第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */ mIUiListener = new BaseUiListener(); mTencent.login(MainActivity.this,"all", mIUiListener); } /** * 自定义监听器实现IUiListener接口后,需要实现的3个方法 * onComplete完成 onError错误 onCancel取消 */ private class BaseUiListener implements IUiListener{ @Override public void onComplete(Object response) { Toast.makeText(MainActivity.this, "授权成功", 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,"登录成功"+response.toString()); } @Override public void onError(UiError uiError) { Log.e(TAG,"登录失败"+uiError.toString()); } @Override public void onCancel() { Log.e(TAG,"登录取消"); } }); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(UiError uiError) { Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show(); } } /** * 在调用Login的Activity或者Fragment中重写onActivityResult方法 * @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); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
来看看效果图吧