AndroidShare是通过官方(微信,QQ,微博)提供的依赖进行二次封装的一个库,封装之后,登录和分享试用起来就十分的简单。
1、Demo里的微信,QQ,微博的Key或者Id,都是用的测试的,大家在用的时候,务必换成自己申请的。
2、Demo代码下载后,是不能实现登录分享的,因为需要签名,毕竟牵扯到保密,签名文件并没有提交,大家如果测试,可以更换成自己的签名文件即可,查看效果,扫描二维码下载即可。
3、大家下好apk,如果图片报错,需要大家自己把Demo中mipmap下的ic_launcher.png复制到手机sd卡根路径下即可,仅做测试用,实际大家还是按照需求而定。
repositories {
……
maven { url "https://gitee.com/AbnerAndroid/almighty/raw/master" }
}
implementation 'com.vip:share:1.0.0'
ShareUtils.get().initShare(
this,
BuildConfig.WX_ID,
BuildConfig.QQ_ID,
BuildConfig.WB_KEY
)
<activity
android:name=".wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
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" />
<data android:scheme="tencent1112002456" />
</intent-filter>
</activity>
ShareUtils.get()
.login(this, ShareUtils.QQ)
.setOnQqCallBackListener({
//登录成功
Toast.makeText(this, it.toString(), Toast.LENGTH_LONG).show()
}, {
//登录失败
})
ShareUtils.get().login(this, ShareUtils.WEIXIN)
ShareUtils.get().login(this, ShareUtils.WEIBO)
.setOnWbCallBackListener({
//成功
Toast.makeText(this, "微博登录成功", Toast.LENGTH_LONG).show()
}, {
//失败
})
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareUrl(
this,
ShareUtils.QQ,
"我是测试分享标题",
"我是测试分享链接",
"https://www.vipandroid.cn/ming/page/open.html",
imagePath,
).setOnQqCallBackListener({
Log.e(TAG, "分享成功")
}, {
Log.e(TAG, "分享失败")
})
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareImage(
this,
ShareUtils.QQ,
imagePath,
).setOnQqCallBackListener({
Log.e(TAG, "分享成功")
}, {
Log.e(TAG, "分享失败")
})
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareUrl(
this,
ShareUtils.QQ,
"我是测试分享标题",
"我是测试分享链接",
"https://www.vipandroid.cn/ming/page/open.html",
imagePath,
ShareUtils.SCENE
)
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareImage(
this,
ShareUtils.QQ,
imagePath,
ShareUtils.SCENE
).setOnQqCallBackListener({
Log.e(TAG, "分享成功")
}, {
Log.e(TAG, "分享失败")
})
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareImage(this, ShareUtils.WEIBO, imagePath)
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareUrl(
this, ShareUtils.WEIBO,
"我是测试标题", "我是测试描述", "https://www.vipandroid.cn", imagePath
)
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareUrl(
this,
ShareUtils.WEIXIN,
"我是测试标题",
"我是测试描述",
"https://www.vipandroid.cn",
imagePath
)
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareImage(this, ShareUtils.WEIXIN, imagePath)
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareUrl(
this,
ShareUtils.WEIXIN,
"我是测试标题",
"我是测试描述",
"https://www.vipandroid.cn",
imagePath, ShareUtils.SCENE
)
//注意,图片地址需要自己生成,这里我使用的是简单的测试
val absoluteFile = Environment.getExternalStorageDirectory().absoluteFile
val imagePath = "$absoluteFile/ic_launcher.png"
ShareUtils.get().qwShareImage(this, ShareUtils.WEIXIN, imagePath, ShareUtils.SCENE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (data != null) {
//QQ回调
ShareUtils.get().onQqActivityResult(requestCode, resultCode, data)
//微博回调
ShareUtils.get().onWbShareActivityResult(data, object : WbShareCallback {
override fun onComplete() {
}
override fun onError(p0: UiError?) {
}
override fun onCancel() {
}
})
}
}
class WXEntryActivity : Activity(), IWXAPIEventHandler {
private var mIWXAPI: IWXAPI? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mIWXAPI = WXAPIFactory.createWXAPI(this, BuildConfig.WX_ID)
mIWXAPI!!.handleIntent(intent, this)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
setIntent(intent)
mIWXAPI!!.handleIntent(intent, this)
}
override fun onReq(resp: BaseReq?) {
}
override fun onResp(resp: BaseResp?) {
when (resp!!.errCode) {
BaseResp.ErrCode.ERR_OK -> {
if (resp.type == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
//分享,做一些其他操作
finish()
} else if (resp.type == ConstantsAPI.COMMAND_SENDAUTH) { //登陆
val code = (resp as SendAuth.Resp).code
Toast.makeText(this, "微信登录成功", Toast.LENGTH_LONG).show()
//根据code,和服务端进行绑定,获取信息后执行后续操作
finish()
}
}
else -> {
finish()
}
}
}
}
微信搜索【Android干货铺】,或扫描下面二维码关注,查阅更多技术文章!
Copyright (C) AbnerMing, AndroidShare Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.