在Android中开发中,我们会经常遇到使用一键分享的功能,今天我以使用第三方平台ShareSDK来实现一键分享向大家分享下具体的流程。


下面,我就以步骤顺序依次展示在自己的Android项目中如何实现
1.添加相应的Jar包,此处我添加了所有APP的jar文件:



2.在官网将下载的onekeyshare包添加到自己的工程目录main下。




3.将ShareSDK.xml文件添加到assets目录下。


<?xml version="1.0" encoding="utf-8"?><DevInfor>    <!--  说明:   1、表格中的第一项  <ShareSDK  AppKey="api20" />  是必须的,其中的AppKey是您在ShareSDK上注册的开发者帐号的AppKey   2、所有集成到您项目的平台都应该为其在表格中填写相对应的开发者信息,以新浪微博为例:  <SinaWeibo  Id="1"  SortId="1"  AppKey="568898243"  AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"  RedirectUrl="http://www.sharesdk.cn"  Enable="true" />  其中的SortId是此平台在分享列表中的位置,由开发者自行定义,可以是任何整型数字,数值越大  越靠后AppKey、AppSecret和RedirectUrl是您在新浪微博上注册开发者信息和应用后得到的信息  Id是一个保留的识别符,整型,ShareSDK不使用此字段,供您在自己的项目中当作平台的识别符。  Enable字段表示此平台是否有效,布尔值,默认为true,如果Enable为false,即便平台的jar包  已经添加到应用中,平台实例依然不可获取。   各个平台注册应用信息的地址如下:  新浪微博 http://open.weibo.com  腾讯微博 http://dev.t.qq.com  QQ空间 http://connect.qq.com/intro/login/  微信好友 http://open.weixin.qq.com  Facebook https://developers.facebook.com  Twitter https://dev.twitter.com  人人网 http://dev.renren.com  开心网 http://open.kaixin001.com  搜狐微博 http://open.t.sohu.com  网易微博 http://open.t.163.com  豆瓣 http://developers.douban.com  有道云笔记 http://note.youdao.com/open/developguide.html#app  印象笔记 https://dev.evernote.com/  Linkedin https://www.linkedin.com/secure/developer?newapp=  FourSquare https://developer.foursquare.com/  搜狐随身看 https://open.sohu.com/  Flickr http://www.flickr.com/services/  Pinterest http://developers.pinterest.com/  Tumblr http://www.tumblr.com/developers  Dropbox https://www.dropbox.com/developers  Instagram http://instagram.com/developer#  VKontakte http://vk.com/dev  -->   <ShareSDK  AppKey = "androidv1101"/> <!-- 修改成你在sharesdk后台注册的应用的appkey"-->   <SinaWeibo  Id="1"  SortId="1"  AppKey="568898243"  AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"  RedirectUrl="http://www.sharesdk.cn"  Enable="true" />    <TencentWeibo  Id="2"  SortId="2"  AppKey="801307650"  AppSecret="ae36f4ee3946e1cbb98d6965b0b2ff5c"  RedirectUri="http://sharesdk.cn"  Enable="true" />    <QZone  Id="3"  SortId="3"  AppId="100371282"  AppKey="aed9b0303e3ed1e27bae87c33761161d"  Enable="true" />        <!-- Wechat微信和WechatMoments微信朋友圈的appid是一样的;   注意:开发者不能用我们这两个平台的appid,否则分享不了   微信测试的时候,微信测试需要先签名打包出apk,  sample测试微信,要先签名打包,keystore在sample项目中,密码123456 -->  <Wechat  Id="4"  SortId="4"  AppId="wx4868b35061f87885"  Enable="true" />        <WechatMoments  Id="5"  SortId="5"  AppId="wx4868b35061f87885"  Enable="true" />           <WechatFavorite  Id="6"  SortId="6"  AppId="wx4868b35061f87885"  Enable="true" />       <QQ  Id="7"  SortId="7"  AppId="100371282"  AppKey="aed9b0303e3ed1e27bae87c33761161d"  Enable="true" />        <Facebook  Id="8"  SortId="8"  ConsumerKey="107704292745179"  ConsumerSecret="38053202e1a5fe26c80c753071f0b573"  Enable="true" />    <Twitter  Id="9"  SortId="9"  ConsumerKey="mnTGqtXk0TYMXYTN7qUxg"  ConsumerSecret="ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc"  CallbackUrl="http://www.sharesdk.cn"  Enable="true" />    <Renren  Id="10"  SortId="10"  AppId="226427"  ApiKey="fc5b8aed373c4c27a05b712acba0f8c3"  SecretKey="f29df781abdd4f49beca5a2194676ca4"  Enable="true" />        <KaiXin  Id="11"  SortId="11"  AppKey="358443394194887cee81ff5890870c7c"  AppSecret="da32179d859c016169f66d90b6db2a23"  RedirectUri="http://www.sharesdk.cn"  Enable="true" />        <Email  Id="12"  SortId="12"  Enable="true" />        <ShortMessage  Id="13"  SortId="13"  Enable="true" />        <SohuMicroBlog  Id="14"  SortId="14"  ApiKey="SAfmTG1blxZY3HztESWx"  ConsumerKey="SAfmTG1blxZY3HztESWx"  ConsumerSecret="yfTZf)!rVwh*3dqQuVJVsUL37!F)!yS9S!Orcsij"  CallbackUrl="http://www.sharesdk.cn"  Enable="true" />       <NetEaseMicroBlog  Id="15"  SortId="15"  ConsumerKey="T5EI7BXe13vfyDuy"  ConsumerSecret="gZxwyNOvjFYpxwwlnuizHRRtBRZ2lV1j"  RedirectUri="http://www.shareSDK.cn"  Enable="true" />        <Douban  Id="16"  SortId="16"  ApiKey="02e2cbe5ca06de5908a863b15e149b0b"  Secret="9f1e7b4f71304f2f"  RedirectUri="http://www.sharesdk.cn"  Enable="true" />        <YouDao  Id="17"  SortId="17"  HostType="product"  ConsumerKey="dcde25dca105bcc36884ed4534dab940"  ConsumerSecret="d98217b4020e7f1874263795f44838fe"  RedirectUri="http://www.sharesdk.cn"  Enable="true" />           <SohuSuishenkan  Id="18"  SortId="18"  AppKey="e16680a815134504b746c86e08a19db0"  AppSecret="b8eec53707c3976efc91614dd16ef81c"  RedirectUri="http://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"  ConsumerKey="sharesdk-7807"  ConsumerSecret="d05bf86993836004"  ShareByAppClient="true"  Enable="true" />        <LinkedIn  Id="20"  SortId="20"  ApiKey="ejo5ibkye3vo"  SecretKey="cC7B2jpxITqPLZ5M"  RedirectUrl="http://sharesdk.cn"  Enable="true" />         <GooglePlus  Id="21"  SortId="21"  Enable="true" />          <FourSquare  Id="22"  SortId="22"  ClientID="G0ZI20FM30SJAJTX2RIBGD05QV1NE2KVIM2SPXML2XUJNXEU"  ClientSecret="3XHQNSMMHIFBYOLWEPONNV4DOTCDBQH0AEMVGCBG0MZ32XNU"  RedirectUrl="http://www.sharesdk.cn"  Enable="true" />           <Pinterest  Id="23"  SortId="23"  ClientId="1432928"  Enable="true" />          <Flickr  Id="24"  SortId="24"  ApiKey="33d833ee6b6fca49943363282dd313dd"  ApiSecret="3a2c5b42a8fbb8bb"  RedirectUri="http://www.sharesdk.cn"  Enable="true" />            <Tumblr  Id="25"  SortId="25"  OAuthConsumerKey="2QUXqO9fcgGdtGG1FcvML6ZunIQzAEL8xY6hIaxdJnDti2DYwM"  SecretKey="3Rt0sPFj7u2g39mEVB3IBpOzKnM3JnTtxX2bao2JKk4VV1gtNo"  CallbackUrl="http://sharesdk.cn"  Enable="true" />    <Dropbox  Id="26"  SortId="26"  AppKey="7janx53ilz11gbs"  AppSecret="c1hpx5fz6tzkm32"  Enable="true" />            <VKontakte  Id="27"  SortId="27"  ApplicationId="3921561"  Enable="true" />            <Instagram  Id="28"  SortId="28"  ClientId="ff68e3216b4f4f989121aa1c2962d058"  ClientSecret="1b2e82f110264869b3505c3fe34e31a1"  RedirectUri="http://sharesdk.cn"  Enable="true" />       <Yixin  Id="29"  SortId="29"  AppId="yx0d9a9f9088ea44d78680f3274da1765f"  Enable="true" />   <!-- AppId="yx28eafabad0934f8d8d34a2a7238f53a0" -->   <YixinMoments  Id="30"  SortId="30"  AppId="yx0d9a9f9088ea44d78680f3274da1765f"  Enable="true" /></DevInfor>



4.将如下两个string.xml文件添加到values目录下。






5.配置AndroidMainfest.xml文件:

(1)所需要的权限:
<uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/><uses-permission android:name="android.permission.GET_ACCOUNTS"/>





(2)配置一个Activity:
<activity  android:name="cn.sharesdk.framework.ShareSDKUIShell"  android:theme="@android:style/Theme.Translucent.NoTitleBar"  android:configChanges="keyboardHidden|orientation"  android:screenOrientation="portrait"  android:windowSoftInputMode="stateHidden|adjustResize" >    <!--  Adapter表示一个继承自cn.sharesdk.framework.authorize.AuthorizeAdapter的类,  这个类可以监听到页面的生命周期,也可以获取授权页面的各种UI控件。  开发者可以通过继承AuthorizeAdapter,重写其方法,并获取各种UI来自定义这个页面的行为。   If you want to change the page's title of auth, you should extends the AuthorizeAdapter  and set the below android:value  -->  <meta-data android:name="Adapter" android:value="cn.sharesdk.demo.MyAdapter" />    <!--  Dropbox的SSO功能需要在此处添加一个对ACTION_VIEW事件的过滤器,其中的scheme是  “db-”前缀再开发者应用的加上appKey。如果此过滤器不设置,则不能完成SSO功能授权   The platform of Dropbox uses SSO that must set the below inter-filter  -->  <intent-filter>        <data android:scheme="db-7janx53ilz11gbs" />        <action android:name="android.intent.action.VIEW" />        <category android:name="android.intent.category.BROWSABLE"/>        <category android:name="android.intent.category.DEFAULT" />    </intent-filter></activity>
6.在程序代码中启动ShareSDK:
   ShareSDK.initSDK(this);
7.创建OnekeyShare并设置参数:
   OnekeyShare oks = new OnekeyShare();   oks.setNotification(R.drawable.ic_launcher, "ShareSDK demo");   oks.setAddress("12345678901");   oks.setTitle("ShareSDK title");//分享内容的标题   oks.setTitleUrl("http://sharesdk.cn");//标题对应的网址,没有可以不设置   oks.setText("ShareSDK text");//设置分享的文本内容   oks.setImagePath(MainActivity.TEST_IMAGE);//设置分享照片的本地路径,如果没有可以不设置   //设置分享照片的url地址,如果没有可以不设置。   oks.setImageUrl("http://img.appgo.cn/imgs/sharesdk/content/2013/07/25/1374723172663.jpg");   oks.setUrl("http://sharesdk.cn");//微信和易信的网络连接,如果没有可以不设置   //人人平台特有的评论字段,如果没有可以不设置   oks.setComment("comment");   //程序的名称或者是站点的名称   oks.setSite("site");   //程序的名称或者是站点名称的链接   oks.setSiteUrl("http://sharesdk.cn");   //设置纬度   oks.setLatitude(23.122619f);   //设置精度   oks.setLongitude(113.372338f);
   //设置是否直接分享   oks.setSilent(false);   //显示   oks.show(MainActivity.this);
8.上面设置参数的方法只是前一种一种,其实还有第二种方式,就是获取相应的平台,然后设置监听器,当分享成功,失败,取消时系统会回掉相应的方法:
//给平台注册回掉事件 private void registerSharedSDKListener(){
    //获取腾讯微博平台    Platform SinaWeiboPlatform = ShareSDK.getPlatform(this, SinaWeibo.NAME);    Platform TencentWeiboPlatform = ShareSDK.getPlatform(this, TencentWeibo.NAME);    SinaWeiboPlatform.setPlatformActionListener(new PlatformActionListener() {        @Override        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {            //成功        }        @Override        public void onError(Platform platform, int i, Throwable throwable) {            //失败        }        @Override        public void onCancel(Platform platform, int i) {            //取消        }    });}
到此为止,经过如上几步就可以轻松的搞定一键分享功能啦!



更多相关文章

  1. android通过浏览器得到手机数据
  2. android 微信支付接口申请分享
  3. Android实现通用的ActivityGroup(效果类似Android微博客户端主界
  4. 一篇文章了解大前端
  5. android平台yuv缩放相关
  6. android 微信分享
  7. 从初学android到现在每天收入50块,收入还在慢慢增加 ,android开发
  8. iOS到Android到底有多远
  9. 持续提高 Android(安卓)应用的安全性与性能

随机推荐

  1. Android(安卓)中关于Environment类的总结
  2. android setTag (int key, Object tag)使
  3. ButterKnife 8.8.1版本在AndroidStudio中
  4. android 中如何在androidmanifest.xml设
  5. android下的异步任务
  6. 简单的网页源码查看器
  7. (血的经验!!)Android(安卓)Studio 如何导入别
  8. 修改apk调用蓝牙无明确提示
  9. Introducing home screen widgets and th
  10. android app 与电脑wifi通信(二)