分享功能的实现//图片为本地图片//支付回调方法//隐藏不分享的图标


title,content,shareUrl有后台返回或者是通过其他途径获得,分享如下:

public void openShare() {
            OnekeyShare oks = new OnekeyShare();
            //关闭sso授权
            oks.disableSSOWhenAuthorize();
            oks.setTitle(title);
            oks.setImageUrl("地址url");
            oks.setTitleUrl(shareUrl);
            // text是分享文本,所有平台都需要这个字段
            oks.setText(content);
            oks.setUrl(shareUrl);
             //如果取本地的图片而不是在网上获取的话可以用下面的方法,可以设置在分享哪种的时候设置相应的图片
             //Wechat.NAMR.equals(platform.getName())
             oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
               @Override
               public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                   if(WechatMoments.NAME.equals(platform.getName()) || 
                             Wechat.NAME.equals(platform.getName())){
                       paramsToShare.setTitle(title);
                       paramsToShare.setText(content);
                        paramsToShare.setImageUrl("...");
                       paramsToShare.setUrl(shareUrl+",title:"+title);
                       paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                       Resources res = getResources();
                       Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon_tag);
                       paramsToShare.setImageData(bmp);
                   }else if (SinaWeibo.NAME.equals(platform.getName())) {
                       paramsToShare.setText(context + " " + shareUrl+",title:"+title);
                       paramsToShare.setUrl(null);
                       paramsToShare.setImageUrl("这里我新浪用的是url,上面的情况去的是本地的图片");
                   }
               }
           });

            oks.setComment("我是测试评论文本");
            // site是分享此内容的网站名称,仅在QQ空间使用
            oks.setSite("...");
            // siteUrl是分享此内容的网站地址,仅在QQ空间使用
            oks.setSiteUrl(shareUrl+",title:"+title);
            //以下是分享成功后的回调,成功或者是失败
            oks.setCallback(new PlatformActionListener() {
                @Override
                public void onError(Platform arg0, int arg1, Throwable t) {
                    Message msg = new Message();
                    msg.obj = t;
                    msg.arg1 = 2;
                    UIHandler.sendMessage(msg, new Handler.Callback() {
                        @Override
                        public boolean handleMessage(Message message) {
                            Toast.makeText(ShareActivity.this,"分享出错哦",Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    });
                }

                @Override
                public void onComplete(Platform arg0, int arg1,
                                       HashMap, Object> arg2) {
                    Message msg = new Message();
                    msg.obj = arg2;
                    msg.arg1 = 1;

                    UIHandler.sendMessage(msg, new Handler.Callback() {
                        @Override
                        public boolean handleMessage(Message message) {
                            Toast.makeText(ShareActivity.this,"分享成功",Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    });
                }

                @Override
                public void onCancel(Platform arg0, int arg1) {
                    Message msg = new Message();
                    msg.arg1 = 3;
                    UIHandler.sendMessage(msg, new Handler.Callback() {
                        @Override
                        public boolean handleMessage(Message message) {
                            Toast.makeText(ShareActivity.this,"分享失败哦",Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    });
                }
            });

            // 启动分享GUI
            oks.show(ShareActivity.this);
        }


如果只分享到朋友圈的话,隐藏其余的图标:
            oks.addHiddenPlatform(QQ.NAME);
            oks.addHiddenPlatform(QZone.NAME);
            oks.addHiddenPlatform(SinaWeibo.NAME);
            oks.addHiddenPlatform(Wechat.NAME);
            oks.addHiddenPlatform(WechatFavorite.NAME);

你可能感兴趣的:(android)