Android分享-微信、Facebook、Google
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: IWXAPIconst 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 = "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 = msgreq.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("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) {//googleval 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) 復制代碼} }
3.分享展示:
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
總結
以上是生活随笔為你收集整理的Android分享-微信、Facebook、Google的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 读书感悟 1 孙子兵法虚实篇(最重要的事
- 下一篇: CentOS7Jenkins安装