Android 5分钟实现ShareSDK分享各平台

效果图
这里写图片描述
一 首先,到官网下载shareSDK的jar包,我demo里的是3.1.2版本
http://www.mob.com/downloadDetail/ShareSDK/android
然后将你需要分享的平台的jar包导入lib
这里写图片描述
二 在你的根目录下(就是包名目录),new一个package叫wxapi,然后把你下载好的sdk里的WXEntryActivity扔进去
这里写图片描述
三 在你的 AndroidManifest.xml中application下,添加如下代码
权限:

<!-- 添加必要的权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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.RECEIVE_SMS" />
        <!--ShareSDK相关start-->
        <activity
            android:name="com.mob.tools.MobUIShell"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="stateHidden|adjustResize" >

            <!-- 调用新浪原生SDK,需要注册的回调activity -->
            <intent-filter>
                <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <!--集成line客户端登录授权,需要添如下格式的过滤器-->
            <intent-filter>
                <data android:scheme="line.1477692153" />
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
        <!-- 填写您从Mob开发者后台中得到的Appkey和AppSecret -->
        <meta-data android:name="Mob-AppKey" android:value="你的Appkey"/>
        <meta-data android:name="Mob-AppSecret" android:value="你的AppSecret"/>
        <activity
            android:name="cn.sharesdk.tencent.qq.ReceiveActivity"
            android:launchMode="singleTask"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tencent100371282" />
                <!--tencent+你的在腾讯申请的AppId,不改的话分享可以成功但是回调一直是onCancel哦-->
            </intent-filter>
        </activity>

        <activity
            android:name="com.kid.share.wxapi.WXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait" />
        <!--ShareSDK相关end-->

注意了!!!

记得把tencent100371282后面的数字改成你自己注册的腾讯平台的AppId
还有Mob-AppKey和Mob-AppSecret的value改为你在ShareSDK注册应用的的AppID和Appkey。
四 在app/src/main下的assets(没有这个包就创建),把SDK里的ShareSDK.xml复制进去,并修改对应平台的AppKey,AppSecret,AppId参数
这里写图片描述
至此,我们的sdk环境已经配置好了。接下来就是调用了。
先写一个方便调用的工具类

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by KID on 2017/12/29.
 * 一切AppID和Key请在ShareSDK.xml中配置
 * ShareSDK分享工具类
 */

public class ShareUtils {
    //QQ好友分享
    public static void shareQQ(String title, String shareurl, String text, String imageUrl, PlatformActionListener listener){
        Platform qq = ShareSDK.getPlatform(QQ.NAME);
        QQ.ShareParams sp = new QQ.ShareParams();
        sp.setTitle(title);
        sp.setTitleUrl(shareurl); // 标题的超链接
        sp.setText(text);
        sp.setImageUrl(imageUrl);
        qq.setPlatformActionListener(listener);
        qq.share(sp);
    }
    //QQ空间
    public static  void shareQQzone(String title,String shareurl,String text,String imageUrl,PlatformActionListener listener){
        Platform qqZone =ShareSDK.getPlatform(QZone.NAME);
        QZone.ShareParams sp = new QZone.ShareParams();
        sp.setShareType(Platform.SHARE_IMAGE);
        sp.setTitle(title);
        sp.setTitleUrl(shareurl); // 标题的超链接
        sp.setSiteUrl(shareurl);
        sp.setSite("QQ空间标题");
        sp.setText(text);
        sp.setImageUrl(imageUrl);
        qqZone.setPlatformActionListener(listener);
        qqZone.share(sp);
    }
    //新浪微博
    public static void shareWeibo(String title,String shareurl,String text,String imageUrl,PlatformActionListener listener){
        Platform sina =ShareSDK.getPlatform(SinaWeibo.NAME);
        SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
        sp.setTitle(title);
        sp.setTitleUrl(shareurl); // 标题的超链接
        sp.setSiteUrl(shareurl);
        sp.setText(text);
        sp.setImageUrl(imageUrl);
        sina.setPlatformActionListener(listener);
        sina.share(sp);
    }
    //微信
    public static void shareWechat(String title,String shareurl,String text,String imageUrl,PlatformActionListener listener){
        Platform weixin =ShareSDK.getPlatform(Wechat.NAME);
        Wechat.ShareParams sp = new Wechat.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(title);
        sp.setImageUrl(imageUrl);
        sp.setUrl(shareurl);
        sp.setText(text);
        sp.setSite("QQ空间标题");
        // siteUrl是分享此内容的网站地址,仅在QQ空间使用
        weixin.setPlatformActionListener(listener);
        weixin.share(sp);
    }
    //微信朋友圈
    public static void sharepyq(String title,String shareurl,String text,String imageUrl,PlatformActionListener listener){
        Platform weixin =ShareSDK.getPlatform(WechatMoments.NAME);
        WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(title);
        sp.setImageUrl(imageUrl);
        sp.setUrl(shareurl);
        sp.setText(text);
        sp.setSite("QQ空间标题");
        weixin.setPlatformActionListener(listener);
        weixin.share(sp);
    }
}

shareSDK对每个平台分享的代码已经封装的差不多了,基本使用无非是1 获取分享平台 2 设置分享参数 3 给分享平台设置分享参数 4设置分享回调
注意点:
1,不同平台的分享参数多多少少会有点不一样,我这里只写了5个平台供参考。而且参数也只是提取我想要的。比如你如果分享微信的图片不想用网络图片了,你还可以setImagePath设置本地图片也是可以的。或者setImageData直接设置bitmap(这个setImageData在其他平台用不了)
2,有些平台没有的参数,你设置了也不会报错。比如setSite仅在QQ空间有用,你给其他平台设置了,也不会报错。shareSDK里应该有if不为nul的判断
接下来只需要在点击事件里调用工具类里相关代码就可以了。
Demo代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

public class MainActivity extends AppCompatActivity {
    ShareDiaog shareDiaog;
    //分享标题
    private String share_title="百度一下";
    //分享链接
    private String share_url="http://blog.csdn.net/qq_31390699";
    //分享封面图片
    private String share_img="http://img.zcool.cn/community/0183b855420c990000019ae98b9ce8.jpg@900w_1l_2o_100sh.jpg";
    //分享描述
    private String share_desc="不懂你就百度啊";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.share_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareDiaog = new ShareDiaog(MainActivity.this);
                shareDiaog.builder().show();
                shareDiaog.setShareClickListener(shareClickListener);
            }
        });
    }
    /**
     * 各平台分享按钮点击事件
     */
    private ShareDiaog.ShareClickListener shareClickListener=new ShareDiaog.ShareClickListener() {
        @Override
        public void shareWechat() {
            ShareUtils.shareWechat(share_title,share_url,share_desc,share_img,platformActionListener);
        }
        @Override
        public void sharePyq() {
            ShareUtils.sharepyq(share_title,share_url,share_desc,share_img,platformActionListener);
        }
        @Override
        public void shareQQ() {
            ShareUtils.shareQQ(share_title,share_url,share_desc,share_img,platformActionListener);
        }
        @Override
        public void shareQzone() {
            ShareUtils.shareQQzone(share_title,share_url,share_desc,share_img,platformActionListener);
        }
    };
    /**
     * 分享回调
     */
    PlatformActionListener platformActionListener=new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            Log.e("kid","分享成功");
        }
        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            Log.e("kid","分享失败");
        }

        @Override
        public void onCancel(Platform platform, int i) {
            Log.e("kid","分享取消");
        }
    };

}

dialog代码

import android.app.AlertDialog;
import android.content.Context;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * Created by KID on 2017/12/29.
 */

public class ShareDiaog implements View.OnClickListener {
    private Context context;
    private AlertDialog alertDialog;

    private LinearLayout ll_share_wechat;
    private LinearLayout ll_share_pyq;
    private LinearLayout ll_share_qq;
    private LinearLayout ll_share_qzone;
    private RelativeLayout rl_menu_cancle;

    public ShareDiaog(Context context) {
        this.context = context;
    }
    public ShareDiaog builder() {
        alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.mycustom_dialog)).create();
        alertDialog.show();
        Window win = alertDialog.getWindow();
        win.setWindowAnimations(R.style.mystyle);
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        win.setAttributes(lp);
        win.setContentView(R.layout.dialog_share);

        rl_menu_cancle=win.findViewById(R.id.rl_menu_cancle);
        ll_share_wechat=win.findViewById(R.id.ll_share_wechat);
        ll_share_pyq=win.findViewById(R.id.ll_share_pyq);
        ll_share_qq=win.findViewById(R.id.ll_share_qq);
        ll_share_qzone=win.findViewById(R.id.ll_share_qzone);

        rl_menu_cancle.setOnClickListener(this);
        ll_share_wechat.setOnClickListener(this);
        ll_share_pyq.setOnClickListener(this);
        ll_share_qq.setOnClickListener(this);
        ll_share_qzone.setOnClickListener(this);

        return this;
    }
    public void show(){
        alertDialog.show();
    }
    public void cancle(){
        alertDialog.cancel();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_menu_cancle:
                cancle();
                break;
            case R.id.ll_share_wechat:
                cancle();
                if(shareClickListener!=null)shareClickListener.shareWechat();
                break;
            case R.id.ll_share_pyq:
                cancle();
                if(shareClickListener!=null)shareClickListener.sharePyq();
                break;
            case R.id.ll_share_qq:
                cancle();
                if(shareClickListener!=null)shareClickListener.shareQQ();
                break;
            case R.id.ll_share_qzone:
                cancle();
                if(shareClickListener!=null)shareClickListener.shareQzone();
                break;
        }
    }
    public ShareClickListener shareClickListener;
    public interface ShareClickListener{
        void shareWechat();
        void sharePyq();
        void shareQQ();
        void shareQzone();
    }
    public void setShareClickListener(ShareClickListener shareClickListener){
        this.shareClickListener=shareClickListener;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/share_show"
        android:text="弹出分享框"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>

dialog_share.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ll_dialog_main_bottom"
    android:orientation="vertical"
    android:background="@color/white">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/ll_share_wechat"
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:gravity="center_horizontal"
            android:layout_height="match_parent">

            <ImageView
                android:background="@color/colorAccent"
                android:layout_marginTop="16dp"
                android:layout_width="50dp"
                android:layout_height="50dp" />
            <TextView
                android:layout_marginTop="11dp"
                android:text="微信"
                android:textSize="12sp"
                android:textColor="@color/text_d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_share_pyq"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent">

            <ImageView
                android:background="@color/colorAccent"
                android:layout_marginTop="16dp"
                android:layout_width="50dp"
                android:layout_height="50dp" />
            <TextView
                android:layout_marginTop="11dp"
                android:text="朋友圈"
                android:textSize="12sp"
                android:textColor="@color/text_d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_share_qq"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent">

            <ImageView
                android:background="@color/colorAccent"
                android:layout_marginTop="16dp"
                android:layout_width="50dp"
                android:layout_height="50dp" />
            <TextView
                android:layout_marginTop="11dp"
                android:text="QQ"
                android:textSize="12sp"
                android:textColor="@color/text_d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_share_qzone"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent">

            <ImageView
                android:background="@color/colorAccent"
                android:layout_marginTop="16dp"
                android:layout_width="50dp"
                android:layout_height="50dp" />
            <TextView
                android:layout_marginTop="11dp"
                android:text="空间"
                android:textSize="12sp"
                android:textColor="@color/text_d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_marginTop="18dp"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        />
    <RelativeLayout
        android:id="@+id/rl_menu_cancle"
        android:layout_width="match_parent"
        android:layout_height="48dp">
        <TextView
            android:textColor="#000000"
            android:textSize="18sp"
            android:text="取消"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

</LinearLayout>

是不是不到5分钟!!!好吧,这时候肯定有些人要吐槽已经过去好久了–!你回头再想想,纯copy-paste4步集成shareSDK配置环境,然后copy一个ShareUtils。是不是都还不到2分钟。点击事件里调用ShareUtils的share方法,也就一行代码吧。至于activity怎么写,框怎么弹,那已经也项目逻辑的事了
代码链接http://download.csdn.net/download/qq_31390699/10183498

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页