首页 > 其他分享 >社会化分享

社会化分享

时间:2022-12-14 20:02:38浏览次数:57  
标签:oks 微信 社会化 ShareSDK android 分享 com


实现微信(好友、朋友圈)、QQ(好友、空间)、新浪微博等社会化分享方式。

前言

现在的app几乎里面都包括社会化分享和第三方登录这两个功能,前段时间给大家分享了关于几种(​​第三方登录​​)的实现,今天再给大家分享关于社会化分享的几种方式。 
在关于第三方登录的那篇文章中就已经提到了​​​mob​​这个第三方平台,我们直接调用其SDK即可。市面上做社会化分享平台的跟人感觉不错的:mob和友盟,根据个人或者公司的需求来选择吧。

正文

再做社会化分享的时候,mob里面给我们提供了一篇​​文章​​详细的讲解,如何实现的。 
既然我们使用的是第三方平台,所以相对于我们自己实现要简单的很多。

到各大平台注册并创建应用

1.到mob平台去注册并创建应用

注册和创建应用的步骤我想就不用我多说了。 

SDK地址(​​http://www.mob.com/#/downloadDetail/ShareSDK/android​​),下载下来发现无法解压(你们可以试一下),联系他们客服,他们说:上传错误,就给我发了一个。我把它和我的Dmeo放在一起,可供下载。 

解压压缩包之后,找到ShareSDK for Android下Libs里面的两个文件夹,在后面我们会用到 

社会化分享_xml

2.到微信、新浪微博和QQ平台去注册并创建应用获取AppKey

这是开放平台地址:

QQ开放平台地址:​​http://open.qq.com/​​​ 
新浪微博开放平台地址:​​​http://open.weibo.com/​​​ 
微信开放平台地址:​​​https://open.weixin.qq.com/​

各大开放平台的网址及申请流程汇总:​​http://bbs.mob.com/forum.php?mod=viewthread&tid=275​

3.配置

1).导入SDK到工程文件:

  • 新建library形式的module,删掉这个module中src/main目录里面的所有文件(包括删掉java、res、AndroidManifest.xml)
  • 复制第三方库的AndroidMainfest.xml、res、src文件(夹)到库目录的src/main目录中
  • 将src/main目录下的src目录重命名为java
  • 社会化分享_xml

我们将上图中的两个工程文件新建为两个Library module:ShareSDK和OneKeyShare,记得要将下面的这个文件夹中的东西也要拷到MainLibs这个module的libs文件夹下:


社会化分享_xml_03

然后,需要注意的是,OneKeyShare这个module是依赖于ShareSDK这个module;而app这个module是依赖于OneKeyShare这个module。


社会化分享_android_04

2).添加应用信息:

先在app这个module(即我们这个项目的module)下新建一个assets文件夹(即第三方资产目录),操作如下:


社会化分享_微信_05

然后,我们将上图中的ShareSDK.xml文件复制到assets目录下。

紧接着开始修改ShareSDK.xml文件中的代码。这里以新浪微博为例:


社会化分享_微信_06


社会化分享_微信_07

Enable字段表示是否想分享到该平台 
RedirectUrl字段表示是否使用客户端分享

注:如果选择客户端分享,而手机没安装客户端则会使用网页版进行授权(除微信外)

新浪微博的回调地址RedirectUrl这个字段的值就是我们后台项目中的应用信息——>高级信息——>OAuth2.0 授权设置中的授权回调页这个字段,需要我们填写一个有效的地址就可以,例如下图:


社会化分享_android_08


注:如果RedirectUrl这个字段不填或填写不对会报以下错误: 

社会化分享_xml_09

3).配置AndroidManifest.xml:

a.


<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">activity</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"com.mob.tools.MobUIShell"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:theme</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@android:style/Theme.Translucent.NoTitleBar"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:configChanges</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"keyboardHidden|orientation|screenSize"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:screenOrientation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"portrait"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:windowSoftInputMode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"stateHidden|adjustResize"</span> ></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">intent-filter</span>></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">data</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:scheme</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"tencent1104646053"</span> /></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">action</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.intent.action.VIEW"</span> /></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">category</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.intent.category.BROWSABLE"</span> /></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">category</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.intent.category.DEFAULT"</span> /></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">intent-filter</span>></span> <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">activity</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>


注意: MobUIShell的路径是固定的,一定要在“cn.sharesdk.framework”下,因为它在Share-Core中。

之后,需要对上面的代码进行以下修改: 

社会化分享_android_10

 

上图中,将红框部分的数字改成在腾讯开放平台申请到的AppId即可。

b.微信相关配置:

在你的build.gradle的applicationId相应目录下新建一个wxapi目录,在该wxapi目录下新增一个WXEntryActivity类,该类继承自Activity(例如应用程序build.gradle的applicationId为com.aohuan.jinghai_lifee,则新添加的类如下图所示) : 

社会化分享_android_11


WXEntryActivity.java的代码如下:

<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/* * 官网地站:http://www.mob.com * 技术支持QQ: 4006852216 * 官方微信:ShareSDK (如果发布新版本的话,我们将会第一时间通过微信将版本更新内容推送给您。如果使用过程中有任何问题,也可以通过微信与我们取得联系,我们将会在24小时内给予回复) * * Copyright (c) 2013年 mob.com. All rights reserved. */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.example.wxapi; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.content.Intent; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.widget.Toast; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> cn.sharesdk.wechat.utils.WXAppExtendObject; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> cn.sharesdk.wechat.utils.WXMediaMessage; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> cn.sharesdk.wechat.utils.WechatHandlerActivity; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** 微信客户端回调activity示例 */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">WXEntryActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">WechatHandlerActivity</span> {</span> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 处理微信发出的向第三方应用请求app message * <p> * 在微信客户端中的聊天页面有“添加工具”,可以将本应用的图标添加到其中 * 此后点击图标,下面的代码会被执行。Demo仅仅只是打开自己而已,但你可 * 做点其他的事情,包括根本不打开任何页面 */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onGetMessageFromWXReq</span>(WXMediaMessage msg) { Intent iLaunchMyself = getPackageManager().getLaunchIntentForPackage(getPackageName()); startActivity(iLaunchMyself); } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 处理微信向第三方应用发起的消息 * <p> * 此处用来接收从微信发送过来的消息,比方说本demo在wechatpage里面分享 * 应用时可以不分享应用文件,而分享一段应用的自定义信息。接受方的微信 * 客户端会通过这个方法,将这个信息发送回接收方手机上的本demo中,当作 * 回调。 * <p> * 本Demo只是将信息展示出来,但你可做点其他的事情,而不仅仅只是Toast */</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onShowMessageFromWXReq</span>(WXMediaMessage msg) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (msg != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span> && msg.mediaObject != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span> && (msg.mediaObject <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">instanceof</span> WXAppExtendObject)) { WXAppExtendObject obj = (WXAppExtendObject) msg.mediaObject; Toast.makeText(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, obj.extInfo, Toast.LENGTH_SHORT).show(); } } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li></ul>


同时,在清单文件中进行声明:


<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"><!--微信分享回调--></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">activity</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">".wxapi.WXEntryActivity"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:configChanges</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"keyboardHidden|orientation|screenSize"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:exported</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"true"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:screenOrientation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"portrait"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:theme</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@android:style/Theme.Translucent.NoTitleBar"</span>/></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>


4).添加代码

集成ShareSDK至少需要在两个位置添加代码,包括:

a.在项目的入口Activity,在其onCreate方法中插入下面的代码进行初始化:(这个方法越早调用越好)


<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ShareSDK.initSDK(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>


如果不在所有的ShareSDK的操作之前调用这行代码,就会抛出空指针异常。

b.在项目出口Activity的onDestroy方法中第一行插入下面的代码:


<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ShareSDK.stopSDK(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>


上方这行代码会结束ShareSDK的统计功能并释放资源。如果这行代码没有被调用,那么“应用启动次数”将会不准确,因为应用可能从来没有被关闭过(注:这一行代码我还是没用到,不知道会造成什么实质性的后果)。

注意:initSDK是可以重复调用的,其实ShareSDK建议在不确定的时候调用这个方法来保证ShareSDK被正确初始化。而stopSDK一旦被调用了,就必须重新调用initSDK才能使用ShareSDK的功能,否则会出现空指针异常。

在代码中调用此方法,即可打开一键分享功能进行分享:


<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">showShare</span>() { ShareSDK.initSDK(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); OnekeyShare oks = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> OnekeyShare(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//关闭sso授权</span> oks.disableSSOWhenAuthorize(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 分享时Notification的图标和文字 2.5.9以后的版本不调用此方法</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// title标题,印象笔记、邮箱、信息、微信、人人网和QQ空间使用</span> oks.setTitle(getString(R.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span>.share)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// titleUrl是标题的网络链接,仅在人人网和QQ空间使用</span> oks.setTitleUrl(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://sharesdk.cn"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// text是分享文本,所有平台都需要这个字段</span> oks.setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"我是分享文本,啦啦啦~"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// imagePath是图片的本地路径,Linked-In以外的平台都支持此参数</span> oks.setImagePath(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/sdcard/test.jpg"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//确保SDcard下面存在此张图片</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// url仅在微信(包括好友和朋友圈)中使用</span> oks.setUrl(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://sharesdk.cn"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// comment是我对这条分享的评论,仅在人人网和QQ空间使用</span> oks.setComment(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"我是测试评论文本"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// site是分享此内容的网站名称,仅在QQ空间使用</span> oks.setSite(getString(R.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">string</span>.app_name)); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// siteUrl是分享此内容的网站地址,仅在QQ空间使用</span> oks.setSiteUrl(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://sharesdk.cn"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 启动分享GUI</span> oks.show(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>


5).主代码

MainActivity.java:


<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.example.sharedemo;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.os.Bundle;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.view.View;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.widget.Button; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> com.example.baseactivity.BaseActivity; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> cn.sharesdk.framework.ShareSDK; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> cn.sharesdk.onekeyshare.OnekeyShare; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">BaseActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">View</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">OnClickListener</span> {</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Button button1; <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//点击按钮,开始进行分享</span> <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onClick</span>(View v) { OnekeyShare oks = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> OnekeyShare(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//关闭sso授权</span> oks.disableSSOWhenAuthorize(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// title标题:微信、QQ(新浪微博不需要标题)</span> oks.setTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"我是分享标题"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//最多30个字符</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// text是分享文本:所有平台都需要这个字段</span> oks.setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"我是分享文本,啦啦啦~http://uestcbmi.com/"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//最多40个字符</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// imagePath是图片的本地路径:除Linked-In以外的平台都支持此参数</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//oks.setImagePath(Environment.getExternalStorageDirectory() + "/meinv.jpg");//确保SDcard下面存在此张图片</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//网络图片的url:所有平台</span> oks.setImageUrl(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://7sby7r.com1.z0.glb.clouddn.com/CYSJ_02.jpg"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//网络图片rul</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// url:仅在微信(包括好友和朋友圈)中使用</span> oks.setUrl(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://sharesdk.cn"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//网友点进链接后,可以看到分享的详情</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Url:仅在QQ空间使用</span> oks.setTitleUrl(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://www.baidu.com"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//网友点进链接后,可以看到分享的详情</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 启动分享GUI</span> oks.show(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); } <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onDestroy</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onDestroy(); ShareSDK.stopSDK(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>); } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li></ul>


提示:如果imagePath和imageUrl同时存在,imageUrl将被忽略。

6).运行效果 

社会化分享_android_12

 

社会化分享_android_13

 

社会化分享_xml_14

 

社会化分享_android_15

其它分享方式我就不列出来了 
如果你还需要做一些细微的调整,那就需要多看官方的东西了,这里贴出两个链接:

注:由于我的源代码里面涉及到客户的信息,所以AppKey、AppSecret等信息,被我删掉了,如果想看我的Demo效果,需要修改小部分我的代码: 

1.把我的com.example.wxapi换成你的applicationId(对应的包名).wxapi 

社会化分享_android_16


2.修改manifest.xml里面对应的信息: 

社会化分享_android_17

 

3.修改ShareSDK.xml里面对应的信息: 

社会化分享_xml_18

记得一定要打包进行测试,这样就可以了!!

​​参考文章​​

​​demo下载地址​​

标签:oks,微信,社会化,ShareSDK,android,分享,com
From: https://blog.51cto.com/u_13657808/5938298

相关文章