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文件

        

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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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文件中

第七步: 写代码实现分享


转载于http://www.cnblogs.com/d-on/p/4085167.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值