Android分享-微信、Facebook、Google
2018-07-30 本文已影响165人
844b9a3a3a68
前言:因为项目涉及到国际化,故而国内需要集成微信分享,国外需要集成Facebook、Google,这里大致记录下集成过程,希望做笔记的同时也能给同行踩踩坑。
分享集成官网:
1.微信:https://open.weixin.qq.com/cgi-bin/appdetail
2.Facebook:https://developers.facebook.com/docs/android/getting-started
3.Google:https://developers.google.com/+/mobile/android/share/
1.引入分享库:
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:5.1.4'
implementation 'com.facebook.android:facebook-share:[4,5)',{
exclude group: 'com.google.zxing', module: 'core'
}
implementation 'com.google.android.gms:play-services-plus:15.0.1'
2.微信分享:
- 在项目包名下新建包,新建包名必须为:wxapi
- wxapi包名下新建类:WXEntryActivity
- 分享回调处理在WXEntryActivity中,具体内容如下:
/**
* Created by Chao 2018/7/20 on 11:05
* description 微信SDK回调路径
*/
class WXEntryActivity : AppCompatActivity(), IWXAPIEventHandler {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WXAPIFactory.createWXAPI(this, App.APP_ID, false).handleIntent(intent, this);
}
/**
* 微信发送请求到第三方应用时,会回调到该方法
*/
override fun onReq(p0: BaseReq?) {
}
/**
* 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
* app发送消息给微信,处理返回消息的回调
*/
override fun onResp(resp: BaseResp) {
when (resp.errCode) {
BaseResp.ErrCode.ERR_AUTH_DENIED, BaseResp.ErrCode.ERR_USER_CANCEL -> {
showToast("分享取消")
finish()
}
BaseResp.ErrCode.ERR_OK -> finish()
}
}
}
- 微信分享初始化:
/**
* Created by Chao 2018/5/8 on 13:31
* description
*/
class App : BaseApplication() {
override fun init() {
registerWeChat(this)
}
/**
* 注册微信分享
*/
private fun registerWeChat(context: Context) {
api = WXAPIFactory.createWXAPI(context, APP_ID, true);
api.registerApp(APP_ID);
}
companion object {
lateinit var api: IWXAPI
const val APP_ID = "wx88888888"
fun getWXAPI(): IWXAPI {
return api
}
}
}
- 微信分享调起:
/**
* Created by Chao 2018/7/25 on 16:22
* description
*/
object ShareUtil {
fun share(activity: AppCompatActivity) {
CommonShareDialog.show(activity, R.layout.common_dialog_share_chinese) {
val webpage = WXWebpageObject()
webpage.webpageUrl = "http://www.baidu.com"
val msg = WXMediaMessage(webpage)
msg.title = "标题"
msg.description = "描述"
//msg.thumbData = getWXThumb(bitmap).toByteArray()
val req = SendMessageToWX.Req()
//WXSceneTimeline朋友圈 WXSceneSession聊天界面
req.scene = if (it == 0) SendMessageToWX.Req.WXSceneSession else SendMessageToWX.Req.WXSceneTimeline//0微信 1朋友圈
req.message = msg
req.transaction = System.currentTimeMillis().toString()
App.getWXAPI().sendReq(req)
if (it == 0) {//微信
activity.showToast("微信")
} else if (it == 1) {//朋友圈
activity.showToast("朋友圈")
}
}
}
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
}
}
2.facebook、google+分享:
/**
* Created by Chao 2018/7/25 on 16:22
* description
*/
object ShareUtil {
private val GOOGLE_REQUEST_CODE: Int = 162
private var callbackManager: CallbackManager? = null
fun share(activity: AppCompatActivity) {
FacebookSdk.setApplicationId("217521058906811")
FacebookSdk.sdkInitialize(activity.applicationContext)
CommonShareDialog.show(activity, R.layout.common_dialog_share_international) {
if (it == 0) {//facebook
val content = ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build()
callbackManager = CallbackManager.Factory.create()
val shareDialog = ShareDialog(activity)
shareDialog.registerCallback(callbackManager, object : FacebookCallback<Sharer.Result> {
override fun onCancel() {
activity.showToast("分享取消")
}
override fun onSuccess(result: Sharer.Result?) {
activity.showToast("分享成功")
}
override fun onError(error: FacebookException?) {
activity.showToast("分享失败")
}
})
shareDialog.show(content, ShareDialog.Mode.AUTOMATIC)
//activity.showToast("facebook")
} else if (it == 1) {//google
val shareIntent = PlusShare.Builder(activity)
.setType("text/plain") // 不变
.setText("Welcome to the Google+ platform.") // 自定义内容
.setContentUrl(Uri.parse("https://developers.google.com/+/")) // 自定义链接地址
.getIntent()
try {
activity.startActivityForResult(shareIntent, GOOGLE_REQUEST_CODE)
} catch (e: ActivityNotFoundException) {
activity.showToast("You haven't installed google+ on your device")
}
//activity.showToast("google+")
}
}
}
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == GOOGLE_REQUEST_CODE) {
return
}
callbackManager?.onActivityResult(requestCode, resultCode, data)
}
}