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文件中的标签下添加
 2 
 3  <activity
 4    android:name="com.example.demo_share_01.Leading"
 5    android:label="@string/app_name" >
 6             
 7                 
 9                 
10                   
11             
12   
复制代码

 

 

 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
"1.0"  encoding= "utf-8" ?>
"http://schemas.android.com/apk/res/android"
     package = "com.qf.sdemo_02_01_volley_news"
     android:versionCode= "1"
     android:versionName= "1.0"  >
 
    
         android:minSdkVersion= "12"
         android:targetSdkVersion= "18"  />
     
  
     "android.permission.INTERNET"  />
     "android.permission.WRITE_EXTERNAL_STORAGE"  />
     "android.permission.GET_TASKS"  />
     "android.permission.INTERNET"  />
     "android.permission.ACCESS_WIFI_STATE"  />
     "android.permission.ACCESS_NETWORK_STATE"  />
     "android.permission.CHANGE_WIFI_STATE"  />
     "android.permission.WRITE_EXTERNAL_STORAGE"  />
     "android.permission.READ_PHONE_STATE"  />
     "android.permission.MANAGE_ACCOUNTS"  />
     "android.permission.GET_ACCOUNTS"  />
    
     "android.permission.BLUETOOTH"  />
     "android.permission.BLUETOOTH_ADMIN"  />
 
    
         android:anyDensity= "true"
         android:largeScreens= "true"
         android:normalScreens= "true"
         android:resizeable= "true"
         android:smallScreens= "true"  />
 
    
         android:allowBackup= "true"
         android:icon= "@drawable/ic_launcher"
         android:label= "@string/app_name"
         android:theme= "@style/AppTheme"  >
         
      
        
             android:name= "com.qf.sdemo_02_01_volley_news.MainActivity"
             android:label= "@string/app_name"  >
                 "android.intent.action.MAIN"  />
 
                 "android.intent.category.LAUNCHER"  />
            
        
         "com.qf.sdemo_02_01_volley_news.Detail"  >
        
         
        
        
             android:name= "cn.sharesdk.framework.ShareSDKUIShell"
             android:configChanges= "keyboardHidden|orientation|screenSize"
             android:theme= "@android:style/Theme.Translucent.NoTitleBar"
             android:windowSoftInputMode= "stateHidden|adjustResize"  >
 
            
            
                 "db-7janx53ilz11gbs"  />
 
                 "android.intent.action.VIEW"  />
 
                 "android.intent.category.BROWSABLE"  />
                 "android.intent.category.DEFAULT"  />
            
 
            
            
                 "tencent100371282"  />
 
                 "android.intent.action.VIEW"  />
 
                 "android.intent.category.DEFAULT"  />
                 "android.intent.category.BROWSABLE"  />
            
        
        
 
    
 

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

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

第七步: 写代码实现分享


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

你可能感兴趣的:(android)