1. Log in to your account, create an app, and generate APPID and APPKEY
2. Download related SDK
SDK download address: https://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD
3. Copy the relevant files in the SDK to your own project
- You need to put the open_sdk_r6137_lite.jar package into your own project and reference
- Relevant codes need to be configured in Androidminfist.xml
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="behind"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<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" />
<!--注意,这里的222222要换成app自己的appid-->
<data android:scheme="tencent222222" />
<!-- 100380359 100381104 222222 -->
</intent-filter>
</activity>
- Code implementation function
Tencent is the function entry of the SDK, and all interface calls must be called through Tencent. Therefore, to call the SDK, you first need to create a Tencent instance, and its code is as follows
lateinit var mTencent: Tencent
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 其中APP_ID是分配给第三方应用的appid,类型为String
mTencent = Tencent.createInstance(APP_ID, this.getApplicationContext())
// 1.4版本:此处需新增参数,传入应用程序的全局context,可通过activity的getApplicationContext方法获取<br>
}
//以下代码是唤起第三方应用QQ,并进行登录
mTencent.logout(this)
if (!mTencent.isSessionValid) {
//进行登录操作
mTencent.login(this, "all", loginListener)
//进行扫码登录操作
mTencent.login(this, "all", loginListener, true)
}
Your AppId should be replaced with the AppId of the specific application . For example, if your AppId is "222222", the label should be like this:
- After successful login, perform callback operation
1.实现UIlistener()类
2.登录成功,会得到JSON数据
3.务必,将数据通过initOpenidAndToken()方法进行配置
4.调用getUserInfo()得到用户信息
loginListener = object : BaseUiListener() {
override fun doComplete(values: JSONObject) {
initOpenidAndToken(values)
getUserInfo()
}
}
/**
* 根据QQ授权登录之后,得到的token。进行配置
* 1.必须进行配置,不然取不到QQ用户的一些基本信息
*/
fun initOpenidAndToken(jsonObject: JSONObject) {
try {
val token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN)
val expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN)
val openId = jsonObject.getString(Constants.PARAM_OPEN_ID)
if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires)
&& !TextUtils.isEmpty(openId)) {
mTencent.setAccessToken(token, expires)
mTencent.openId = openId
}
} catch (e: Exception) {
}
}
/**
* 取用户信息
*/
fun getUserInfo() {
val userInfo = UserInfo(this, mTencent.qqToken)
userInfo.getUserInfo(object : IUiListener {
override fun onComplete(response: Any?) {
val type = object : TypeToken<QQUserInfoModel>() {}.type
val qqUserInfoModel = GsonUtils.fromJson<QQUserInfoModel>(response.toString(), type)
}
override fun onCancel() {
}
override fun onError(error: UiError?) {
}
})
}
private open inner class BaseUiListener : IUiListener {
override fun onComplete(response: Any?) {
doComplete((response as JSONObject?)!!)
}
protected open fun doComplete(values: JSONObject) {
}
override fun onError(e: UiError) {
}
override fun onCancel() {
}
}
Special attention
When the application calls the Andriod_SDK interface, if it wants to successfully receive the callback, it needs to add the following code in the onActivityResult method of the Activity that calls the interface:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == Constants.REQUEST_LOGIN || requestCode == Constants.REQUEST_APPBAR) {
//当QQ登录完成后,进行回调
Tencent.onActivityResultData(requestCode, resultCode, data, loginListener)
}
super.onActivityResult(requestCode, resultCode, data)
}
- Jump to the details of some common problems during the development process
: Android SDK FAQ - Commonly used API interface
details jump to Android_SDK function list