sharesdk 的使用


 

社交分享组件有很多 介绍一下sharesdk 的使用

官网:http://sharesdk.cn/

 

1、先上效果图

 

2、主要代码:

 

[java] view plain copy
  1. public class TestShare extends Activity {  
  2.   
  3.     private RelativeLayout sina;  
  4.     private RelativeLayout sms;  
  5.     private RelativeLayout qq;  
  6.     private RelativeLayout chat;  
  7.     private TextView invite_code;  
  8.   
  9.     private Context context;  
  10.     private String[] items = new String[] { "分享给好友", "分享到朋友圈" };  
  11.     private String share_content;  
  12.     private String user_id;  
  13.   
  14.     private static final int SHARE_SUCCESS = 30;  
  15.     private static final int SHARE_FAIL = 31;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         // TODO Auto-generated method stub  
  20.         super.onCreate(savedInstanceState);  
  21.   
  22.         this.setContentView(R.layout.test_share);  
  23.         context = TestShare.this;  
  24.   
  25.         ShareSDK.initSDK(this);  
  26.   
  27.         sina = (RelativeLayout) this.findViewById(R.id.share_sina);  
  28.         qq = (RelativeLayout) this.findViewById(R.id.share_qq);  
  29.         chat = (RelativeLayout) this.findViewById(R.id.share_chat);  
  30.         sms = (RelativeLayout) this.findViewById(R.id.share_sms);  
  31.         share_content = this.getResources().getString(R.string.share_content);  
  32.         sina.setOnClickListener(new View.OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 // TODO Auto-generated method stub  
  37.                 // 分享到新浪微博  
  38.                 // showShare(true, SinaWeibo.NAME,TestShare.this);  
  39.                 share(share_content, null, SinaWeibo.NAME);  
  40.             }  
  41.         });  
  42.   
  43.         qq.setOnClickListener(new View.OnClickListener() {  
  44.   
  45.             @Override  
  46.             public void onClick(View v) {  
  47.                 // TODO Auto-generated method stub  
  48.                 // 分享到腾讯微博  
  49.                 // showShare(true, TencentWeibo.NAME, TestShare.this);  
  50.                 share(share_content, null, TencentWeibo.NAME);  
  51.             }  
  52.         });  
  53.   
  54.         chat.setOnClickListener(new View.OnClickListener() {  
  55.   
  56.             @Override  
  57.             public void onClick(View v) {  
  58.                 // TODO Auto-generated method stub  
  59.   
  60.                 showChatDialog();  
  61.   
  62.             }  
  63.         });  
  64.   
  65.         sms.setOnClickListener(new View.OnClickListener() {  
  66.   
  67.             @Override  
  68.             public void onClick(View v) {  
  69.                 // TODO Auto-generated method stub  
  70.                 sendSms(share_content, "");  
  71.   
  72.             }  
  73.         });  
  74.     }  
  75.   
  76.     // 新浪微博分享 腾讯微博等只需修改 NAME  
  77.     public void share(String text, String photopath, String sharename) {  
  78.   
  79.         Platform.ShareParams sp = new SinaWeibo.ShareParams();  
  80.         sp.text = text;  
  81.   
  82.         if (photopath != null) {  
  83.             // sp.imagePath = "/mnt/sdcard/测试分享的图片.jpg";  
  84.             sp.imagePath = photopath;  
  85.   
  86.         }  
  87.   
  88.         Platform weibo = ShareSDK.getPlatform(context, sharename);  
  89.   
  90.         // 设置分享事件回调  
  91.         weibo.setPlatformActionListener(new PlatformActionListener() {  
  92.   
  93.             public void onError(Platform platform, int action, Throwable t) {  
  94.                 // 操作失败的处理代码  
  95.                 Message m = handler.obtainMessage();  
  96.                 m.what = SHARE_FAIL;  
  97.                 TestShare.this.handler.sendMessage(m);  
  98.             }  
  99.   
  100.             public void onComplete(Platform platform, int action,  
  101.                     HashMap<String, Object> res) {  
  102.                 // 操作成功的处理代码  
  103.                 Message m = handler.obtainMessage();  
  104.                 m.what = SHARE_SUCCESS;  
  105.                 TestShare.this.handler.sendMessage(m);  
  106.             }  
  107.   
  108.             public void onCancel(Platform platform, int action) {  
  109.                 // 操作取消的处理代码  
  110.             }  
  111.   
  112.         });  
  113.   
  114.         // 执行图文分享  
  115.         weibo.share(sp);  
  116.     }  
  117.   
  118.     @Override  
  119.     protected void onDestroy() {  
  120.         // TODO Auto-generated method stub  
  121.         ShareSDK.stopSDK(this);  
  122.         super.onDestroy();  
  123.     }  
  124.   
  125.     // 发送短信  
  126.     public void sendSms(final String content, final String to) {  
  127.         Uri smsToUri = Uri.parse("smsto:".concat(to));  
  128.         Intent intent = new Intent(android.content.Intent.ACTION_SENDTO,  
  129.                 smsToUri);  
  130.         intent.putExtra("sms_body", content);  
  131.         startActivity(intent);  
  132.     }  
  133.   
  134.     /** 
  135.      * 显示选择对话框 
  136.      */  
  137.     private void showChatDialog() {  
  138.   
  139.         new AlertDialog.Builder(this).setTitle("分享到")  
  140.                 .setItems(items, new DialogInterface.OnClickListener() {  
  141.   
  142.                     @Override  
  143.                     public void onClick(DialogInterface dialog, int which) {  
  144.                         switch (which) {  
  145.                         case 0:  
  146.                             share(share_content, null, Wechat.NAME);  
  147.                             break;  
  148.                         case 1:  
  149.   
  150.                             share(share_content, null, WechatMoments.NAME);  
  151.                             break;  
  152.                         }  
  153.                     }  
  154.                 })  
  155.                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  156.   
  157.                     @Override  
  158.                     public void onClick(DialogInterface dialog, int which) {  
  159.                         dialog.dismiss();  
  160.                     }  
  161.                 }).show();  
  162.   
  163.     }  
  164.   
  165.     private Handler handler = new Handler() {  
  166.         /* 
  167.          * (non-Javadoc) 
  168.          *  
  169.          * @see android.os.Handler#handleMessage(android.os.Message) 
  170.          */  
  171.         @Override  
  172.         public void handleMessage(Message msg) {  
  173.             // TODO Auto-generated method stub  
  174.   
  175.             switch (msg.what) {  
  176.             case SHARE_SUCCESS:  
  177.   
  178.                 Toast.makeText(TestShare.this, "分享成功", Toast.LENGTH_SHORT)  
  179.                         .show();  
  180.                 break;  
  181.             case SHARE_FAIL:  
  182.   
  183.                 Toast.makeText(TestShare.this, "分享失败", Toast.LENGTH_SHORT)  
  184.                         .show();  
  185.                 break;  
  186.   
  187.             }  
  188.         }  
  189.   
  190.     };  
  191.   
  192. }  


 


3、  需要的jar包  ShareSDK-Core.jar 是sdk核心包 ,Wechat是微信核心包,Wechat分享到好友,Wechat-Moments 分享到朋友圈。

       另 android-support-v4.jar 需要使用官网下载的。

 

4、代码混淆

    混淆文件在demo中说明,直接更改就可以使用

   

 

posted @ 2016-03-03 23:41  Sharley  阅读(489)  评论(0编辑  收藏  举报