admin管理员组文章数量:1122888
Android QQ登录,分享,分享图片
近年来三方导流,方便用户使用App,以及方便App数据分享越来越多的App支持了三方登录以及三方分享网页和图片.在此,整理了一系列三方库的集成方法以及集成中遇到的问题 .
一:引用
1:Maven引用
implementation 'com.tencent.tauth:qqopensdk:3.52.0'
2:jar 包下载
二:QQ三方配置
1:AndroidManifest.xml配置
<!--QQ需要的权限--><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><application><!--qq--><activityandroid: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" /><!--tencent你的AppId--><data android:scheme="xxxid" /></intent-filter></activity><activityandroid:name="com.tencent.connectmon.AssistActivity"android:configChanges="orientation|keyboardHidden"android:screenOrientation="behind"android:theme="@android:style/Theme.Translucent.NoTitleBar" /><activity android:name=".QQThirdActivity"/></application>
2:Activity调用页面配置(且QQ的监听只能在Activity中监听)
import android.content.Intentimport android.os.Bundleimport android.widget.Buttonimport androidx.appcompat.app.AppCompatActivityimport androidx.databinding.DataBindingUtilimport com.tencent.tauth.IUiListenerimport com.tencent.tauth.Tencentimport com.tencent.tauth.UiErrorimport com.wu.third.databinding.ActivityQqBindingclass QQThirdActivity : AppCompatActivity(), IUiListener {var mTencent: Tencent? = null//qq的权限val ACOPE_ALL = "all"val url = "/"var img = "=http%3A%2F%2Fimg.redocn%2Fsheying%2F20140704%2Fyuntaishanfengjing_2695407.jpg&refer=http%3A%2F%2Fimg.redocn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639293976&t=cc27d9362325a51c8ee2c2c16a6dbe25"override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)var binding = DataBindingUtil.setContentView<ActivityQqBinding>(this, R.layout.activity_qq)mTencent = Tencent.createInstance("xxxx", this.getApplicationContext())binding.btLogin.setOnClickListener {//QQ 登录if (mTencent!!.isQQInstalled(this))mTencent!!.login(this, ACOPE_ALL, this)}binding.btShare.setOnClickListener {//QQ分享网页if (mTencent!!.isQQInstalled(this)) ThirdUtil.shareQQ(this, mTencent!!, "标题", "这是简介", img, url)}binding.btShareImg.setOnClickListener {//QQ分享图片if (mTencent!!.isQQInstalled(this))ThirdUtil.shareQQImg(this, mTencent!!, "path");}}override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult(requestCode, resultCode, data)//设置回调Tencent.onActivityResultData(requestCode, resultCode, data, this)}//成功override fun onComplete(p0: Any?) {ThirdUtil.loginQQ(this, mTencent!!, p0!!)}//失败override fun onError(p0: UiError?) {}//取消override fun onCancel() {}override fun onWarning(p0: Int) {}}
三:功能实现
1:QQ登录
1.调用登录方法
//QQ登录
if (mTencent!!.isQQInstalled(this)) mTencent!!.login(this, ACOPE_ALL, this)
2.登陆回调执行:执行IUiListener回调,获取Object数据
3:获取数据
//QQ 登录fun loginQQ(context: Context, mTencent: Tencent, any: Any) {var jsObj = any as JSONObjectif (jsObj != null) {var ret = jsObj.getInt(SocialConstants.PARAM_OPEN_ID)//授权成功if (ret == 0) {//此处登录授权成功val openId: String = jsObj.getString(SocialConstants.PARAM_OPEN_ID)val access_token: String = jsObj.getString(Constants.PARAM_ACCESS_TOKEN)val expires: String = jsObj.getString(Constants.PARAM_EXPIRES_IN)mTencent.openId = openIdmTencent.setAccessToken(access_token, expires)//获取用户唯一IDgetUnionId(context,mTencent)} else {// todo 授权失败}}}
4.获取UnionID
//获取用户唯一IDprivate fun getUnionId(context: Context, mTencent: Tencent) {var listener = object : IUiListener {override fun onComplete(response: Any?) {if (response != null) {val jsonObject = response as JSONObjecttry {val unionid = jsonObject.getString("unionid")getUserInfo(context,mTencent,unionid)} catch (e: Exception) {// todo 数据异常}} else {// todo 数据异常}}override fun onError(p0: UiError?) {// todo 获取唯一ID失败}override fun onCancel() {}override fun onWarning(p0: Int) {}}val unionInfo = UnionInfo(context, mTencent.qqToken)unionInfo.getUnionId(listener)}
5.获取用户信息
//获取用户信息private fun getUserInfo(context: Context, mTencent: Tencent, unionid: String) {val info = UserInfo(context, mTencent.qqToken)info.getUserInfo(object : IUiListener {override fun onComplete(value: Any?) {val jsonObject = value as JSONObjectif (jsonObject == null) return//名字val name = jsonObject.getString("nickname")//头像val urlface = jsonObject.getString("figureurl_qq")//性别val gender = jsonObject.getString("gender")//省会val province = jsonObject.getString("province")//城市val city = jsonObject.getString("city")Log.e("QQ数据", name + ":" + urlface + ":" + gender + ":" + province + ":" + city)}override fun onError(p0: UiError?) {// todo 用户信息失败}override fun onCancel() {}override fun onWarning(p0: Int) {}})}
2:QQ分享网页
//QQ分享网页fun shareQQ(context: QQThirdActivity,mTencent: Tencent,title: String,desc: String,imgUrl: String,url: String) {val params = Bundle()params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT) //分享类型 分享网页params.putString(QQShare.SHARE_TO_QQ_TITLE,title) // 标题 限制(长度 128 )QQ_SHARE_TITLE_MAX_LENGTHparams.putString(QQShare.SHARE_TO_QQ_SUMMARY,desc) // 摘要限制(512) QQ_SHARE_SUMMARY_MAX_LENGTH = 512params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url) // 内容地址 最好控制下长度 长连接报过错params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imgUrl) // 网络图片地址(本地地址)params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "xxx") // 应用名称// 分享操作要在主线程中完成mTencent.shareToQQ(context, params, object : IUiListener {override fun onComplete(p0: Any?) {// todo 分享成功}override fun onError(p0: UiError?) {// todo 分享失败}override fun onCancel() {}override fun onWarning(p0: Int) {}})}
3.分享图片
// 分享图片fun shareQQImg(qqThirdActivity: QQThirdActivity, mTencent: Tencent, path: String) {val params = Bundle()params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, path) //分享图片地址 最好做个限制大小params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "xxxApp")// 应用名称params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE)//分享类型 分享图片// 分享操作要在主线程中完成mTencent.shareToQQ(qqThirdActivity, params, object : IUiListener {override fun onComplete(p0: Any?) {// todo 分享成功}override fun onError(p0: UiError?) {// todo 分享失败}override fun onCancel() {}override fun onWarning(p0: Int) {}})}
总结:
QQ三方库简单的集成以及主要功能实现,以此记录,欢迎问题探讨,来都来了点个赞再走吧!!!
1:集成文档QQ
2:QQ资源文件
3:源码地址
本文标签: Android QQ登录分享分享图片
版权声明:本文标题:Android QQ登录,分享,分享图片 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1701866251a494733.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论