Android 分享功能的实现

I : 利用隐式Intent实现分享

   1、应用实现分享的功能

      

 1 /* 实现分享功能 */
 2         
 3         Intent intent = new Intent();
 4         intent.setAction(Intent.ACTION_SEND);
 5 
 6         // 分享文本
 7         intent.setType("text/plain"); // text/html ...
 8         intent.putExtra(Intent.EXTRA_SUBJECT, "我要分享");
 9         intent.putExtra(Intent.EXTRA_TEXT, "分享的内容");
10         
11         // 分享本地图片
12         intent.setType("image/*");
13         File file = new File(Environment.getExternalStorageDirectory()+"/imgCache/a0.jpg");
14         Uri uri = Uri.fromFile(file);
15         intent.putExtra(Intent.EXTRA_STREAM,uri);
16 
17         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
18         startActivity(Intent.createChooser(intent, "分享列表"));

 

 2、应用加入系统的分享列表

 

 1  在AndroidManifest.xml文件中的<activity>标签下添加<Intent-filter>
 2 
 3  <activity
 4    android:name="com.example.demo_share_01.Leading"
 5    android:label="@string/app_name" >
 6             <intent-filter>
 7                 <action android:name="android.intent.action.SEND" />
 9                 <category android:name="android.intent.category.DEFAULT" />
10                 <data android:mimeType="image/*"/>  
11             </intent-filter>
12   </activity>

 

 

 II :  利用ShareSDK第三方库实现分享

    以一键分享(新浪微博和腾讯微博)为例

   第一步: 在src文件夹下添加叫cn.sharesdk.onekeyshare包

   第二步: 在libs文件夹下添加库文件 

                        

  第三步:在assets文件夹下添加ShareSDK.xml文件

        

  ShareSDK.xml文件代码如下:

<?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.mob.com" 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://developer.linkedin.com 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 易信好友 http://open.yixin.im/ 明道 http://open.mingdao.com/ Line http://media.line.me/zh-hant/ Pocket http://getpocket.com/developer/apps/new --> <ShareSDK AppKey = "426e34bef3d7"/> <!-- 修改成你在sharesdk后台注册的应用的appkey"--> <!-- ShareByAppClient标识是否使用微博客户端分享,默认是false --> <SinaWeibo Id="1" SortId="1" AppKey="2073300154" AppSecret="a5f7b084ba7630688831f691e16d146b" RedirectUrl="https://api.weibo.com/oauth2/default.html" ShareByAppClient="false" Enable="true" /> <TencentWeibo Id="2" SortId="2" AppKey="801550161" AppSecret="d2c82a847a577af120857feee42548ea" RedirectUri="http://www.baidu.com" Enable="true" /> </DevInfor>

 第四步:在AndroidManifest.xml文件中添加访问权限等配置信息

  

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qf.sdemo_02_01_volley_news"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="18" />
    
  <!-- 添加需要的各种权限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <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.WRITE_EXTERNAL_STORAGE" />
    <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" />
    <!-- 蓝牙分享所需的权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
      <!-- 主Activity -->
        <activity
            android:name="com.qf.sdemo_02_01_volley_news.MainActivity"
            android:label="@string/app_name" >
<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.qf.sdemo_02_01_volley_news.Detail" >
        </activity>
        
        <!-- 进行授权界面的注册 -->
        <activity
            android:name="cn.sharesdk.framework.ShareSDKUIShell"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:windowSoftInputMode="stateHidden|adjustResize" >

            <!--
            	Dropbox的SSO功能需要在此处添加一个对ACTION_VIEW事件的过滤器,其中的scheme是
            	“db-”前缀再开发者应用的加上appKey。如果此过滤器不设置,则不能完成SSO功能授权
            -->
            <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>

            <!--
            	如果集成QQ分享,或者使用QQ客户端来进行QQ空间的分享,须要在此处添加一个对ACTION_VIEW
            	事件的过滤器,其中的scheme是“tencent”前缀再开发者应用的加上appId。如果此过滤器不设置,
            	则分享结束以后不能得到正确的回调
            -->
            <intent-filter>
                <data android:scheme="tencent100371282" />

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>
       

    </application>

</manifest>

第五步:将官方Demo中的Sample工程下的res文件下的资源文件拷贝到自己的工程目录下

第六步: 在mob.com 、新浪微博开发平台 和腾讯微博开发平台创建应用获得 appkey 和 appsecrect ,并添加到ShareSDK.xml文件中

第七步: 写代码实现分享

   

  1 package com.qf.demo_sharesdk_01;
  2 
  3 import java.util.HashMap;
  4 
  5 import android.app.Activity;
  6 import android.os.Bundle;
  7 import android.util.Log;
  8 import android.view.View;
  9 import cn.sharesdk.framework.Platform;
 10 import cn.sharesdk.framework.PlatformActionListener;
 11 import cn.sharesdk.framework.ShareSDK;
 12 import cn.sharesdk.onekeyshare.OnekeyShare;
 13 import cn.sharesdk.sina.weibo.SinaWeibo;
 14 import cn.sharesdk.tencent.weibo.TencentWeibo;
 15 
 16 public class MainActivity extends Activity {
 17 
 18     @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         setContentView(R.layout.activity_main);
 22         
 23         //注意:要初始化ShareSDK,不然指定分享会出错
 24         ShareSDK.initSDK(this);
 25     }
 26     
 27     //一键分享到新浪和腾讯微博
 28     public void  onekeyShare(View v){
 29         OnekeyShare onekeyShare = new OnekeyShare();
 30         onekeyShare.setTitle("分享");
 31         onekeyShare.setText("分享的内容");
 32         //新浪暂不支持图片分享
 33 //        onekeyShare.setImageUrl("http://img4.duitang.com/uploads/blog/201306/23/20130623134809_2QhYF.jpeg");
 34         onekeyShare.show(this);
 35     }
 36     
 37     //指定分享到新浪微博
 38     public void  share2Sina(View v){
 39         //第一步:创建平台对象
 40         Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
 41         //第二步:给平台对象绑定监听器
 42         platform.setPlatformActionListener(new PlatformActionListener() {
 43             
 44             @Override
 45             public void onError(Platform platform, int action, Throwable t) {
 46                 Log.v("TAG", "分享失败");
 47             }
 48             
 49             @Override
 50             public void onComplete(Platform platform, int action, HashMap<String, Object> datas) {
 51                 if(action == Platform.ACTION_AUTHORIZING){
 52                     SinaWeibo.ShareParams params = new SinaWeibo.ShareParams();
 53                     params.setTitle("分享");
 54                     params.setText("分享的内容");
 55                     params.setAddress("http://www.baidu.com");
 56                     params.setAuthor("It's don !");
 57                     params.setComment("nice");
 58                     params.setContentType(TRIM_MEMORY_BACKGROUND);
 59                     params.text = "share content";    
 60                     
 61                     platform.share(params);
 62                 }else if(action == Platform.ACTION_SHARE){
 63                     Log.v("TAG", "分享成功");
 64                 }
 65             }
 66             
 67             @Override
 68             public void onCancel(Platform platform, int action) {
 69                 Log.v("TAG", "分享取消");
 70             }
 71         });
 72         
 73         //第三步:给平台对象授权
 74         platform.authorize();
 75     }
 76     
 77     //指定分享到腾讯微博
 78     public void share2Tencent(View v){
 79         //第一步:
 80         Platform platform = ShareSDK.getPlatform(TencentWeibo.NAME);
 81         //第二步:
 82         platform.setPlatformActionListener(new PlatformActionListener() {
 83             
 84             @Override
 85             public void onError(Platform platform, int action, Throwable t) {
 86                 Log.v("TAG","分享失败");
 87             }
 88             
 89             @Override
 90             public void onComplete(Platform platform, int action, HashMap<String, Object> datas) {
 91                 if(action == Platform.ACTION_AUTHORIZING){
 92                     TencentWeibo.ShareParams params = new TencentWeibo.ShareParams();
 93                     params.text = "T-MAC";
 94                     params.setTitle("我要分享");
 95                     params.setText("Tracy Mcgrady");
 96                     params.setImageUrl("http://image.hoopchina.com/nba/desktop/%5Bwww.hoopchina.com%5D20100311-tmac-1.jpg");
 97                 
 98                     platform.share(params);
 99                 }else if(action == Platform.ACTION_SHARE){
100                     Log.v("TAG", "分享成功");
101                 }
102             }
103             
104             @Override
105             public void onCancel(Platform platform, int action) {
106                 Log.v("TAG", "取消分享");
107             }
108         });
109         
110         //第三步:
111         platform.authorize();
112     }
113 
114 
115 }
View Code

 

posted on 2014-11-09 15:43  畅遨  阅读(2662)  评论(0编辑  收藏  举报

导航