美文网首页三方
【androidstudio】快速集成 Mob的 ShareSD

【androidstudio】快速集成 Mob的 ShareSD

作者: Sraindy | 来源:发表于2019-08-26 14:01 被阅读0次

    一、准备

    MobSDK官网
    http://www.mob.com/mobService/sharesdk
    官网的集成文档说明
    http://wiki.mob.com/sdk-share-android-3-0-0/
    官网的Android项目demo
    https://github.com/MobClub/ShareSDK-for-Android

    二、快速集成的步骤

    1、先申请Mob的appkey与appsecret ( 申请指南 http://bbs.mob.com/forum.php?mod=viewthread&tid=8212&extra=page%3D1)
    2、打开项目根目录的build.gradle,在buildscrip-->dependencies 模块下面添加 classpath 'com.mob.sdk:MobSDK:+',如下所示;

    image.png

    3、 新增MobSDK.gradle 文件,存放到文件根目录,与android并列,不要配置到android里面,位置如下:


    image.png

    4、 MobSDK.gradle文件内容

    apply plugin: 'com.mob.sdk'
    
    MobSDK {
    
        appKey "moba0b0c0d0"
        appSecret "5713f0d88511f9f4cf100cade0610a34"
    
       /* MobLink {
            uriScheme "moba0b0c0d0://cn.sharesdk.demo"
            appLinkHost "ahmn.t4m.cn"
        }*/
    
        //https true
    
        ShareSDK {
    
            loopShare true
    
            //平台配置信息
            devInfo {
    
                SinaWeibo {
                    id 1
                    sortId 59
                    appKey "568898243"
                    appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
                    callbackUri "http://www.sharesdk.cn"
                    shareByAppClient true
                    enable true
                }
    
                TencentWeibo {
                    id 2
                    sortId 2
                    appKey "801307650"
                    appSecret "ae36f4ee3946e1cbb98d6965b0b2ff5c"
                    callbackUri "http://sharesdk.cn"
                    enable true
                }
    
                QZone {
                    id 3
                    sortId 3
                    appId "100371282"
                    appKey "aed9b0303e3ed1e27bae87c33761161d"
                    shareByAppClient true
                    bypassApproval false
                    enable true
                }
    
                //分享QQ小程序专用
                /*QZone {
                    id 3
                    sortId 3
                    appId "222222"
                    appKey "aed9b0303e3ed1e27bae87c33761161d"
                    shareByAppClient true
                    bypassApproval false
                    enable true
                }*/
    
                 /* Wechat微信和WechatMoments微信朋友圈的appid是一样的;
                注意:开发者不能用我们这两个平台的appid,否则分享不了
                微信测试的时候,微信测试需要先签名打包出apk,
                sample测试微信,要先签名打包,keystore在sample项目中,密码123456
                BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过
                审核的应用也可以执行分享,但是仅限于分享文字和图片,不能分享其他类型,
                默认值为false。此外,微信收藏不支持此字段。wx4868b35061f87885
    
                <!--要分享微信小应用程序时用userName,path-->*/
    
                Wechat {
                    id 4
                    sortId 4
                    appId "wx4868b35061f87885"
                    appSecret "64020361b8ec4c99936c0e3999a9f249"
                    userName "gh_afb25ac019c9"
                    path "pages/index/index.html?id=1"
                    withShareTicket true
                    miniprogramType 0
                    bypassApproval false
                    enable true
                }
    
                WechatMoments {
                    id 5
                    sortId 5
                    appId "wx4868b35061f87885"
                    appSecret "64020361b8ec4c99936c0e3999a9f249"
                    bypassApproval false
                    enable true
                }
    
                WechatFavorite {
                    id 6
                    sortId 6
                    appId "wx4868b35061f87885"
                    appSecret "64020361b8ec4c99936c0e3999a9f249"
                    bypassApproval false
                    enable true
                }
    
                QQ {
                    id 7
                    sortId 7
                    appId "100371282"
                    appKey "aed9b0303e3ed1e27bae87c33761161d"
                    shareByAppClient false
                    bypassApproval false
                    enable true
                }
    
                //分享小程序专用
                /*QQ {
                    id 7
                    sortId 7
                    appId "222222"
                    appKey "aed9b0303e3ed1e27bae87c33761161d"
                    shareByAppClient false
                    bypassApproval false
                    enable true
                }*/
    
                Facebook {
                    id 8
                    sortId 8
                    appKey "1412473428822331"
                    appSecret "a42f4f3f867dc947b9ed6020c2e93558"
                    callbackUri "https://mob.com"
                    shareByAppClient true
                    enable true
                }
    
                Twitter {
                    id 9
                    sortId 9
                    appKey "viOnkeLpHBKs6KXV7MPpeGyzE"
    appSecret "NJEglQUy2rqZ9Io9FcAU9p17omFqbORknUpRrCDOK46aAbIiey"
                    callbackUri "http://mob.com"
                    shareByAppClient true
                    enable true
    
                }
    
                Renren {
                    id 10
                    sortId 10
                    appId "226427"
                    appKey "fc5b8aed373c4c27a05b712acba0f8c3"
                    appSecret "f29df781abdd4f49beca5a2194676ca4"
                    enable true
                }
    
                KaiXin {
                    id 11
                    sortId 11
                    appKey "358443394194887cee81ff5890870c7c"
                    appSecret "da32179d859c016169f66d90b6db2a23"
                    callbackUri "http://www.sharesdk.cn"
                    enable true
                }
    
                Email {
                    id 12
                    sortId 12
                    enable true
                }
    
                ShortMessage {
                    id 13
                    sortId 13
                    enable true
                }
    
                Douban {
                    id 16
                    sortId 16
                    appKey "031a96a3aa8b28af094fc3eaffa17a0d"
                    appSecret "2e675e730571b75d"
                    callbackUri "http://mob.com"
                    enable true
                }
    
                YouDao {
                    id 17
                    sortId 17
                    hostType "product"
                    appKey "dcde25dca105bcc36884ed4534dab940"
                    appSecret "d98217b4020e7f1874263795f44838fe"
                    callbackUri "http://www.sharesdk.cn/"
                    enable true
                }
    
                /*在中国大陆,印象笔记有两个服务器,一个是沙箱(sandbox),一个是生产服务器(china)。
                一般你注册应用,它会先让你使用sandbox,当你完成测试以后,可以到
                http://dev.yinxiang.com/support/上激活你的ConsumerKey,激活成功后,修改HostType
                为china就好了。至于如果您申请的是国际版的印象笔记(Evernote),则其生产服务器类型为
                “product”。
                如果目标设备上已经安装了印象笔记客户端,ShareSDK允许应用调用本地API来完成分享,但
                是需要将应用信息中的“ShareByAppClient”设置为true,此字段默认值为false。*/
    
                Evernote {
                    id 19
                    sortId 19
                    hostType "sandbox"
                    appKey "sharesdk-7807"
                    appSecret "d05bf86993836004"
                    shareByAppClient true
                    enable true
                }
    
                LinkedIn {
                    id 20
                    sortId 20
                    appKey "ejo5ibkye3vo"
                    appSecret "cC7B2jpxITqPLZ5M"
                    callbackUri "http://sharesdk.cn"
                    shareByAppClient true
                    enable true
                }
    
                GooglePlus {
                    id 21
                    sortId 21
                    appId "236300675100-am5pm8km7md1memjevq8rl9pg5c4s4b8.apps.googleusercontent.com
                    callbackUri "http://localhost"
                    shareByAppClient true
                    enable true
                }
    
                FourSquare {
                    id 22
                    sortId 22
        appId "G0ZI20FM30SJAJTX2RIBGD05QV1NE2KVIM2SPXML2XUJNXEU"
    appSecret "3XHQNSMMHIFBYOLWEPONNV4DOTCDBQH0AEMVGCBG0MZ32XNU"
                    callbackUri "http://www.sharesdk.cn"
                    enable true
                }
    
                Pinterest {
                    id 23
                    sortId 23
                    appId "1432928"
                    enable true
                }
    
                Flickr {
                    id 24
                    sortId 24
                    appKey "33d833ee6b6fca49943363282dd313dd"
                    appSecret "3a2c5b42a8fbb8bb"
                    callbackUri "http://www.sharesdk.cn"
                    enable true
                }
    
                Tumblr {
                    id 25
                    sortId 25
        appKey "2QUXqO9fcgGdtGG1FcvML6ZunIQzAEL8xY6hIaxdJnDti2DYwM"
    appSecret "3Rt0sPFj7u2g39mEVB3IBpOzKnM3JnTtxX2bao2JKk4VV1gtNo"
                    callbackUri  "http://www.sharesdk.cn"
                    enable true
                }
    
                Dropbox {
                    id 26
                    sortId 26
                    appKey "i5vw2mex1zcgjcj"
                    appSecret "3i9xifsgb4omr0s"
                    callbackUri "https://www.sharesdk.cn"
                    enable true
                }
    
                VKontakte {
                    id 27
                    sortId 27
                    appId "3921561"
                    enable true
                }
    
                Instagram {
                    id 28
                    sortId 28
                    appId "ff68e3216b4f4f989121aa1c2962d058"
                    appSecret "1b2e82f110264869b3505c3fe34e31a1"
                    callbackUri "http://sharesdk.cn"
                    enable true
                }
    
                /*Yixin易信和YixinMoments易信朋友圈的appid是一样的;
                注意:开发者不能用我们这两个平台的appid,否则分享不了
                易信测试的时候需要先签名打包出apk,
                sample测试易信,要先签名打包,keystore在sample项目中,密码123456
                BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过
                审核的应用也可以执行分享,但是仅限于分享文字或图片,不能分享其他类型, 默认值为false。*/
                Yixin {
                    id 29
                    sortId 29
                    appId "yx0d9a9f9088ea44d78680f3274da1765f"
                    bypassApproval false
                    enable true
                }
    
                YixinMoments{
                    id 30
                    sortId 30
                    appId "yx0d9a9f9088ea44d78680f3274da1765f"
                    bypassApproval false
                    enable true
                }
    
                Mingdao {
                    id 31
                    sortId 31
                    appKey "EEEE9578D1D431D3215D8C21BF5357E3"
                    appSecret "5EDE59F37B3EFA8F65EEFB9976A4E933"
                    callbackUri "http://sharesdk.cn"
                    enable true
                }
    
                Line {
                    id 32
                    sortId 32
                    appId "1639219273"
                    appSecret "58faad55ffce8bf8f63b59fe6fb702ae"
                    callbackUri "https://www.mob.com/"
                    callbackscheme "lineauth"
                    enable true
                }
    
                KakaoTalk {
                    id 33
                    sortId 33
                    appKey "48d3f524e4a636b08d81b3ceb50f1003"
                    enable true
                }
    
                KakaoStory {
                    id 34
                    sortId 34
                    appKey "48d3f524e4a636b08d81b3ceb50f1003"
                    enable true
                }
    
                WhatsApp {
                    id 35
                    sortId 35
                    enable true
                }
    
                Pocket {
                    id 37
                    sortId 37
                    appKey "32741-389c565043c49947ba7edf05"
                    enable true
                }
    
                Instapaper {
                    id 38
                    sortId 38
    appKey "4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA"
    appSecret "GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe"
                    enable true
                }
    
                FacebookMessenger {
                    id 39
                    sortId 39
                    appId "107704292745179"
                    enable true
                }
    
                Telegram {
                    id 47
                    sortId 47
                    appKey "782826033"
                    callbackUri "http://www.mob.com"
                    enable true
                }
    
                Alipay {
                    id 50
                    sortId 50
                    appId "2015072400185895"
                    enable true
                }
    
                AlipayMoments {
                    id 51
                    sortId 51
                    appId "2015072400185895"
                    enable true
                }
    
                Dingding {
                    id 52
                    sortId 52
                    appId "dingoanxyrpiscaovl4qlw"
                    bypassApproval false
                    enable true
                }
    
                Youtube {
                    id 53
                    sortId 53
                    appId "370141748022-bicrnsjfiije93bvdt63dh3728m4shas.apps.googleusercontent.com"
                    appSecret "AIzaSyAO06g-0TDpHcsXXO918a7QE3Zdct2bB5E"
                    callbackUri "http://localhost"
                    shareByAppClient true
                    enable true
                }
    
                Meipai {
                    id 54
                    sortId 54
                    appId "1089867596"
                    appSecret "y9ym6rrjjijtkt23qtsc"
                    shareByAppClient true
                    enable true
                }
    
                Cmcc {
                    id 55
                    sortId 55
                    appId "300011860247"
                    appKey "2D464D8BFCE73A44B4F9DF95A2FDBE1C"
                }
    
                Reddit {
                    id 56
                    sortId 56
                    appKey "MExDxPuTCtFiRw"
                    callbackUri "http://www.sharesdk.cn"
                }
    
                Telecom {
                    id 57
                    sortId 57
                    appKey "8148612606"
                    appSecret "mCltrhUqwshFa86egDTs0491ibaAulKA"
                    callbackUri "http://www.sharesdk.cn"
                    enable true
                }
    
                /* app_secret的值为Account Kit 应用密匙 ,不是客户端口令 */
                Accountkit {
                    id 58
                    sortId 58
                    appKey "579465512480462"
                    appSecret "8a6383652dd9f23fb0994f03d350d0ca"
                    callbackUri "http://www.sharesdk.cn/"
                    enable true
                }
    
                Douyin {
                    id 59
                    sortId 1
                    appKey "aw9ivykfjvi4hpwo"
                    appSecret "42b4caa6bda60bd49f05f06d0a4956e1"
                    enable true
                }
    
                Wework {
                    id 60
                    sortId 60
                    appKey "wwa21eaecf93f0e3ba"
                    appSecret "dW7e27P7Hc8NiYdRxnbTeOLgfI1ugR72e-PM8uusq2s"
                    agentId "1000012"
                    schema "wwautha21eaecf93f0e3ba000012"
                }
            }
    
        }
    }
    

    5、 在module下面的build.gradle文件里面,引用MobSDK.gradle文件。添加如下代码:apply from: '../MobSDK.gradle',添加位置如下图:


    image.png

    6、 在java代码中,初始化MobSDK

       MobSDK.init(TGlobalData.context, SHARE_KEY, SHARE_KEY_SECRET);
    

    7、 添加配置后,即可调用授权、获取资料、分享等操作,如一键分享的代码:

    private void showShare() {
         OnekeyShare oks = new OnekeyShare();
         //关闭sso授权
         oks.disableSSOWhenAuthorize(); 
    
         // title标题,微信、QQ和QQ空间等平台使用
         oks.setTitle(getString(R.string.share));
         // titleUrl QQ和QQ空间跳转链接
         oks.setTitleUrl("http://sharesdk.cn");
         // text是分享文本,所有平台都需要这个字段
         oks.setText("我是分享文本");
         // imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
         oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
         // url在微信、微博,Facebook等平台中使用
         oks.setUrl("http://sharesdk.cn");
         // comment是我对这条分享的评论,仅在人人网使用
         oks.setComment("我是测试评论文本");
        // 启动分享GUI
        oks.show(this);
    }
    }
    

    MobSdk.gradle中的配置字段说明

    配置支持的平台全部属性字段有以下这些(注:区分大小写):

    id 数字,平台的id,可以设置为任何不重复的数字,(可选字段)
    sortId 数字,九宫格界面平台的排序,越小越靠前,可以设置为任何不重复的数字,(可选字段)
    appId 文本,对应ShareSDK.xml中的AppId、ClientID、ApplicationId、ChannelID
    appKey 文本,对应ShareSDK.xml中的AppKey、ConsumerKey、ApiKey、OAuthConsumerKey
    appSecret 文本,对应ShareSDK.xml中的AppSecret、ConsumerSecret、SecretKey、Secret、ClientSecret、ApiSecret、ChannelSecret
    callbackUri 文本,对应ShareSDK.xml中的RedirectUrl、RedirectUri、CallbackUrl
    shareByAppClient 布尔值,shareByAppClient标识是否使用客户端分享
    bypassApproval 布尔值,bypassApproval表示是否绕过审核
    enable 布尔值,enable字段表示此平台是否启用
    userName 文本,userName在微信小程序中使用
    path 文本,path在微信小程序中使用
    hostType 文本,表示服务器类型,在YouDao和Evernote中使用
    withShareTicket 布尔值,分享微信小程序时,分享出去的小程序被二次打开时可以获取到更多信息,true为能获取,反之不能;仅在微信中使用
    miniprogramType 数字,分享微信小程序时,表示小程序的开发状态,取值范围:0-正式,1-开发,2-体验,仅在微信中使用

    相关文章

      网友评论

        本文标题:【androidstudio】快速集成 Mob的 ShareSD

        本文链接:https://www.haomeiwen.com/subject/jsmrectx.html