Android--分享功能

实现分享功能的几个办法:

一、调用系统的分享功能
二、通过第三方SDK,如ShareSDK,友盟等
三、自行使用各自平台的SDK,比如QQ,微信,微博各自的SDK

DEMO地址:https://github.com/chaoyu168/shareDemo/tree/master

先看原生方法:

/**
     * Android原生分享功能
     * 默认选取手机所有可以分享的APP
     */
    public void allShare(){
        Intent share_intent = new Intent();
        share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
        share_intent.setType("text/plain");//设置分享内容的类型
        share_intent.putExtra(Intent.EXTRA_SUBJECT, "share");//添加分享内容标题
        share_intent.putExtra(Intent.EXTRA_TEXT, "share with you:"+"android");//添加分享内容
        //创建分享的Dialog
        share_intent = Intent.createChooser(share_intent, "share");
        startActivity(share_intent);
    }
    /**
     * Android原生分享功能
     * @param appName:要分享的应用程序名称
     */
    private void share(String appName) {
        Intent share_intent = new Intent();
        share_intent.setAction(Intent.ACTION_SEND);
        share_intent.setType("text/plain");
        share_intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        share_intent.putExtra(Intent.EXTRA_TEXT, "推荐您使用一款软件:" + appName);
        share_intent = Intent.createChooser(share_intent, "分享");
        startActivity(share_intent);
    }

二、以shareSDK为例

1,在http://www.mob.com官网上去创建一个后台应用(如果没有账号的同学要去先注册一下),主要后去的是你新创建的应用的appKey值

2,这里我们分享新浪微博为例,在新浪微博的sdk中创建一个新的应用,主要是获取这个应用的appKey和appSecret值

3.在官网配置

4、按方法配置APP build.gradle

5一键分享

/**
     * onekeyshare分享调用九宫格方法
     */
    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题,印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("分享标题");
        // titleUrl是标题的网络链接,仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本,所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信(包括好友和朋友圈)中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论,仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称,仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址,仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }
    /**
     * 指定平台分享
     */
    private void showShare1() {
        Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME); //获取分享平台

        //设置平台监听器
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                // TODO: 2016/11/3 此方法中增加分享功能
                switch (i) {
                    case Platform.ACTION_AUTHORIZING:
                        Toast.makeText(getApplicationContext(), "授权成功", Toast.LENGTH_SHORT).show();
                        //获取分享参数
                        SinaWeibo.ShareParams params = new SinaWeibo.ShareParams();
                        String sTxt = "http://www.baidu.com/互联";//转成url编码
                        try {
                            sTxt += URLEncoder.encode("互联", "utf-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        params.setText("测试指定平台分享 @1611 zsp 分享地址:" + sTxt);
                        platform.share(params); //开始分享
                        platform.showUser(null);
                        break;

                    case Platform.ACTION_SHARE:
                        Log.d("zsp", "分享成功");
                        Toast.makeText(getApplication(), "分享成功", Toast.LENGTH_SHORT).show();
                        break;
                    case Platform.ACTION_USER_INFOR:
                        //显示所有用户信息
                        for (Map.Entry<String, Object> e : hashMap.entrySet()) {
                            Log.d("zsp", e.getKey() + "---" + e.getValue());
                        }
                        break;
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        platform.authorize();//分享授权
    }

具体详见DEMO:https://github.com/chaoyu168/shareDemo/tree/master

三、微信分享,与2大同小异

https://blog.csdn.net/baidu_35559769/article/details/82497289

  • 6
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值