Skip to content

分享

概述

借助宝船 SDK 平台接入 各个平台的分享,你可以在你的应用中快速轻松地实现分享功能。

应用接入

  1. 如果您的应用没有继承 Applicaton 的类,请创建该类,如若已有实现类,则在应用的 Applicaton 继承类中添加如下方法:
kotlin
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        val config = BrickConfig()
        //是否打印日志,可选
        config.logEnable = true
        //app的业务Id,不能为空,可以为包名或者其他
        config.bindUid ="cc.xxxx.demo"
        BrickSDK.initBrick(
            this,
            "cdshf8dfbshdf8sdfc",//宝船出海SDK应用创建后,平台生成的key
            "sdjncvs889347r983274cvhsd",config //宝船出海SDK应用创建后,平台生成的秘钥
        )
    }
}

涉及方法与具体说明

  • BrickSDK.initBrick 方法具体说明请点击 initBrick 查看。
  1. 打开 /app/manifest/AndroidManifest.xml 文件,在里面同步引用上一步创建的 MyApplication
xml
<application
    android:name="您的包名和路径.MyApplication"
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/logo"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/logo"
    android:supportsRtl="true">
    ......
    </application>

用户分享相关方法和说明

分享举例

在您需要分享的 activity 页面中,需要做分享操作的时候吊起分享,同时注册回调方法。以微信支付为例:

kotlin
......
import android.util.Log
import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap
import cc.k2games.android.brick.core.BrickError
import cc.k2games.android.brick.core.BrickSDK
import cc.k2games.android.brick.share.BrickSharePlatform
import cc.k2games.android.brick.share.ShareListener
import cc.k2games.android.brick.share.media.BrickMediaObject
import cc.k2games.android.brick.share.media.BrickShareImage
import cc.k2games.android.brick.share.media.BrickShareMedias
import cc.k2games.android.brick.share.media.BrickShareMiniProgram
import cc.k2games.android.brick.share.media.BrickShareMusic
import cc.k2games.android.brick.share.media.BrickShareText
import cc.k2games.android.brick.share.media.BrickShareVideo
import cc.k2games.android.brick.share.media.BrickShareWebLink
import cc.k2games.android.brick.share.media.MiniProgramType
......

class MainActivity : Activity() {

    ......
    
    private fun share(
    
    ) {
        val textMedia = BrickShareText("这是一个文本分享")
        textMedia.description = "我即将发送一个文本"
        val brickMediaObject: BrickMediaObject=textMedia,
        BrickSDK.share(this, BrickSharePlatform.WeiXinSession, brickMediaObject, object : ShareListener {
            override fun onShareError(error: BrickError) {
                Log.i(
                    "ShareActivity",
                    "分享失败  ${from.from.getPlatformDes()} onError code=${error.getCode()}  message=${error.getMessage()}"
                )
                Toast.makeText(
                    this@ShareListActivity,
                    "分享失败  ${from.from.getPlatformDes()}",
                    LENGTH_SHORT
                ).show()
            }

            override fun onShareCancel() {
                Log.i("ShareActivity", "分享取消")
                Toast.makeText(
                    this@ShareListActivity,
                    "分享取消  ${from.from.getPlatformDes()}",
                    LENGTH_SHORT
                ).show()
            }

            override fun onShareSuccess() {
                Log.i("ShareActivity", "分享成功 ${from.from.getPlatformDes()} onSuccess ")
                Toast.makeText(
                    this@ShareListActivity,
                    "分享成功  ${from.from.getPlatformDes()}",
                    LENGTH_SHORT
                ).show()
            }

        })
    }
}

涉及方法与具体说明

  • BrickSDK.share 方法具体说明请点击 share 查看。

分享类型

可进行各种类型的分享,只需要改变BrickSDK.share方法中的brickMediaObject字段传值即可。<\n> 实现brickMediaObject的对象有BrickShareText,BrickShareImage,BrickShareMiniProgram,BrickShareWebLink,BrickShareMedias,BrickShareMusic,BrickShareVideo

文本分享

kotlin
val textMedia = BrickShareText("这是一个文本分享")
textMedia.description = "我即将发送一个文本"

图片分享

kotlin
val imageMedia =BrickShareImage("您的bitmap对象或者远程图片url")
imageMedia.description = "我即将发送一个图片"

音频分享

kotlin
val videoMedia =BrickShareVideo("您的远程音频url")
videoMedia.description = "我即将发送一个音频文件"

多视频和音频分享

kotlin
val videoMedia =BrickShareMedias(listOf(BrickShareImage("您的bitmap对象或者远程图片ur")), listOf("您的远程音频url"))
videoMedia.description = "我即将发送一个多图片和音频文件"

网页分享

kotlin
val webMedia =BrickShareWebLink("远程网页url")
webMedia.title = "这是一个网页"
webMedia.description = "这是一个网页"
val drawable = resources.getDrawable(R.mipmap.logo, theme)
webMedia.thumb = BrickShareImage(drawable.toBitmap(100, 100))

小程序分享

kotlin
val miniMedia =BrickShareMiniProgram("您的小程序未打开的兼容网页url")
miniMedia.path = "您的小程序页面路径"
miniMedia.miniProgramId = "小程序原始id"
miniMedia.miniProgramType = MiniProgramType.TEST
miniMedia.title = "这是一个小程序"
miniMedia.description = "我即将发送一个小程序"
val drawable = resources.getDrawable(R.mipmap.logo, theme)
miniMedia.thumb = BrickShareImage(drawable.toBitmap(100, 100))

音乐分享

kotlin
val musicMedia =BrickShareMusic("远程音乐url地址")
musicMedia.targetUrl = "远程网页url地址"
musicMedia.singerName = "歌手名"
musicMedia.albumName = "音乐专辑名"
musicMedia.duration =10   // 歌曲时长
musicMedia.title = "这是一个音乐文件"
musicMedia.description = "我即将发送一个音乐文件"
val drawable = resources.getDrawable(R.mipmap.logo, theme)
musicMedia.thumb = BrickShareImage(drawable.toBitmap(100, 100))