sharesdk第三方分享功能(微博微信朋友圈QQ邮箱)

//需要引入sharesdk和在微博微信QQ提供的第三方平台上申请账号,请自行查阅sharesdk官方文档

//用于分享的数据,自行根据需求进行设置
public class ShareModel
{
    private String address;
    private String img;
    private String title;
    private String txt;

    public String getTitle() {
        return title;
    }

    public ShareModel(String address, String img, String title, String txt)
    {
        this.address = address;
        this.img = img;
        this.txt = txt;
        this.title = title;

    }

    public String getAddress() {
        return address;
    }

    public String getImg() {
        return img;
    }

    public String getTxt() {
        return txt;
    }
}
public class ShareUtil
{
    private static Context mContext;
    private static String shareToastTxt;

    public void doShare(Context context, final ShareModel ShareModel)
    {
        this.mContext = context;
        final ShareCallBack shareCallBack = new ShareCallBack();
        final ShareDialog ShareDialog = new ShareDialog(context);

        ShareDialog.setCancelButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ShareDialog.dismiss();
            }
        });
        ShareDialog.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int arg2, long arg3)
            {
                HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);

                //设置分享内容
                Platform.ShareParams sp = new Platform.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE);

                sp.setText(ShareModel.getTxt());

                //微信邮箱和QQ空间使用
                sp.setTitle(ShareModel.getTitle()); //分享文本
                sp.setTitleUrl(ShareModel.getAddress());

                //网络图片rul
                sp.setImageUrl(ShareModel.getImg());

                //仅在QQ空间使用
                sp.setTitleUrl(ShareModel.getAddress());

                //仅在微信使用
                sp.setUrl(ShareModel.getAddress());

                //仅在QQ空间使用
                sp.setSite(ShareModel.getTitle());
                sp.setSiteUrl(ShareModel.getAddress());

                if (item.get("ItemText").equals("微博"))
                {
                    Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
                    shareToastTxt = "微博";
                    sinaWeibo.setPlatformActionListener(shareCallBack); // 设置分享事件回调
                    // 执行分享
                    sinaWeibo.share(sp);
                }
                else if (item.get("ItemText").equals("微信好友"))
                {
                    Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                    shareToastTxt = "微信";
                    wechat.setPlatformActionListener(shareCallBack); // 设置分享事件回调
                    // 执行分享
                    wechat.share(sp);
                }
                else if (item.get("ItemText").equals("朋友圈"))
                {
                    Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
                    shareToastTxt = "朋友圈";
                    wechatMoments.setPlatformActionListener(shareCallBack); // 设置分享事件回调
                    // 执行分享
                    wechatMoments.share(sp);
                }
                else if (item.get("ItemText").equals("QQ好友"))
                {
                    Platform qq = ShareSDK.getPlatform(QQ.NAME);
                    shareToastTxt = "QQ";
                    qq.setPlatformActionListener(shareCallBack); // 设置分享事件回调
                    // 执行分享
                    qq.share(sp);
                }
                else if (item.get("ItemText").equals("QQ空间"))
                {
                    Platform qzone = ShareSDK.getPlatform(QZone.NAME);
                    shareToastTxt = "空间";
                    qzone.setPlatformActionListener(shareCallBack); // 设置分享事件回调
                    // 执行分享
                    qzone.share(sp);
                }
                else if (item.get("ItemText").equals("邮箱"))
                {
                    //设置分享内容
                    Platform.ShareParams sp1 = new Platform.ShareParams();
                    sp1.setShareType(Platform.SHARE_TEXT);
                    sp1.setTitle(ShareModel.getTitle());
                    sp1.setText(ShareModel.getTxt()+" "+ ShareModel.getAddress());

                    Platform email = ShareSDK.getPlatform(Email.NAME);
                    shareToastTxt = "邮箱";
                    email.setPlatformActionListener(shareCallBack); // 设置分享事件回调
                    // 执行分享
                    email.share(sp1);
                }
                else if(item.get("ItemText").equals("复制链接"))
                {
                    ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                    // 将文本内容放到系统剪贴板里。
                    cm.setText(ShareModel.getAddress());
                    Toast.makeText(mContext, "复制成功", Toast.LENGTH_LONG).show();
                }
                ShareDialog.dismiss();
            }
        });
    }

    //分享回调
    private static class ShareCallBack implements PlatformActionListener
    {

        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap)
        {
            Toast.makeText(mContext,shareToastTxt+"分享成功",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable)
        {
            Toast.makeText(mContext,shareToastTxt+"分享失败",Toast.LENGTH_SHORT).show();
            Log.e("share_error",throwable.toString());
        }

        @Override
        public void onCancel(Platform platform, int i)
        {
            Toast.makeText(mContext,shareToastTxt+"分享取消",Toast.LENGTH_SHORT).show();
        }
    }
}
public class ShareDialog
{
    private Dialog dialog;
    private GridView gridView;
    private RelativeLayout cancelButton;
    private SimpleAdapter saImageItems;

    //此处是分享dialog用到的七个图片,存放顺序与下方name相对应的顺序需要保持一致,请自行根据美工提供的图片进行替换
    private int[] image = {R.drawable.share_sina, R.drawable.share_wx, R.drawable.share_friend, R.drawable.share_qq,R.drawable.share_qzne,R.drawable.share_email,R.drawable.share_copyline};

    private String[] name = {"微博", "微信好友", "朋友圈", "QQ好友","QQ空间","邮箱","复制链接"};

    public ChShareDialog(Context context)
    {
        dialog = new Dialog(context,R.style.MyAnimDialog);
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);

        Window window = dialog.getWindow();
        window.setContentView(R.layout.ch_share_dialog);
        if (window != null)
        {
            window.getDecorView().setPadding(0, WidgetUtil.dip2px(context,5), 0,0);
            WindowManager.LayoutParams attr = window.getAttributes();
            if (attr != null)
            {
                attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                attr.width = ViewGroup.LayoutParams.MATCH_PARENT;
                //设置dialog 在布局中的位置
                attr.gravity = Gravity.BOTTOM;

                window.setAttributes(attr);
            }
        }

        gridView = (GridView) window.findViewById(R.id.share_gridView);
        cancelButton = (RelativeLayout) window.findViewById(R.id.share_cancel);
        List<HashMap<String, Object>> shareList = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < image.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", image[i]);//添加图像资源的ID
            map.put("ItemText", name[i]);//按序号做ItemText
            shareList.add(map);
        }

        saImageItems = new SimpleAdapter(context, shareList, R.layout.ch_share_item, new String[]{"ItemImage", "ItemText"}, new int[]{R.id.imageView1, R.id.textView1});
        gridView.setAdapter(saImageItems);
    }

    public void setCancelButtonOnClickListener(OnClickListener Listener)
    {
        cancelButton.setOnClickListener(Listener);
    }

    public void setOnItemClickListener(OnItemClickListener listener)
    {
        gridView.setOnItemClickListener(listener);
    }


    /**
     * 关闭对话框
     */
    public void dismiss()
    {
        dialog.dismiss();
    }
}
//用于分辨率转化,dp与px的相互转化
public class WidgetUtil
{
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}

//分享dialog用到的动画效果,请复制粘贴至styles文件内
<style name="MyAnimDialog" parent="android:Theme.Dialog">
    <!-- 背景颜色及透明程度 -->
    <item name="android:windowBackground">@android:color/transparent</item>
    <!-- 是否半透明 -->
    <item name="android:windowIsTranslucent">false</item>
    <!-- 是否没有标题 -->
    <item name="android:windowNoTitle">true</item>
    <!-- 是否浮现在activity之上 -->
    <item name="android:windowIsFloating">true</item>
    <!-- 是否背景模糊 -->
    <item name="android:backgroundDimEnabled">true</item>
    <!-- 设置背景模糊的透明度-->
    <item name="android:backgroundDimAmount">0.5</item>
    <!-- 动画 -->
    <item name="android:windowAnimationStyle">@style/dialog_animation</item>
</style>
<!-- 对话框显示和退出动画 -->
<style name="dialog_animation">
    <item name="android:windowEnterAnimation">@anim/dialog_enter</item>
    <item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>

// dialog_exit.xml   放入anim文件夹下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fillAfter="true"
        android:fromYDelta="0%"
        android:toYDelta="100%p"/>
</set>

// dialog_enter.xml 放入anim文件夹下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fillAfter="true"
        android:fromYDelta="100%p"
        android:toYDelta="0%"/>
</set>
// share_dialog.xml  shareDialog的布局文件,请自行放入layout文件夹
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ffffff"
    android:layout_gravity="bottom"
    android:orientation="vertical">

    <GridView
        android:paddingTop="20dp"
        android:id="@+id/share_gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@color/white"
        android:gravity="center"
        android:horizontalSpacing="1dp"
        android:listSelector="@android:color/transparent"
        android:numColumns="4"
        android:verticalSpacing="10dp"></GridView>

    <RelativeLayout
        android:id="@+id/share_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/share_gridView"
        android:gravity="center"
        android:padding="5dp"
        android:background="@color/white"
        android:visibility="visible">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:text="取消"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </RelativeLayout>
</RelativeLayout>

使用方法:

ShareModel shareModel = new ShareModel();

shareModel.set...;//根据需要设置四个参数

ShareUtil.doShare(context,shareModel); //到此就OK啦,放到你按键响应事件下即可

猜你喜欢

转载自blog.csdn.net/qq_30555429/article/details/81981792