增加QQ,微信独立分享,界面优化

This commit is contained in:
khy
2016-09-14 17:46:18 +08:00
parent b9a2374f8f
commit 385a206f6b
30 changed files with 268 additions and 111 deletions

View File

@ -1,8 +1,11 @@
package com.gh.common.util;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
@ -14,9 +17,24 @@ import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.gh.gamecenter.R;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.tencent.mm.sdk.platformtools.Util;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import java.util.ArrayList;
import java.util.HashMap;
import cn.sharesdk.framework.Platform;
@ -24,10 +42,6 @@ import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.system.text.ShortMessage;
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 khy on 2016/9/4.
@ -42,7 +56,9 @@ public class ShareUtils {
private boolean isPlugin = false;
private int maxHeight;
private static IWXAPI api;
private static Tencent mTencent;
private int[] arrLogo = {R.drawable.share_wechat_logo, R.drawable.share_wechatmoments_logo, R.drawable.share_qq_logo
, R.drawable.share_qzone_logo, R.drawable.share_sinaweibo_logo, R.drawable.share_shortmessage_logo
, R.drawable.share_copyfont_logo, R.drawable.share_cancel_logo};
@ -55,6 +71,8 @@ public class ShareUtils {
public static ShareUtils getInstance(Context context) {
if (instance == null) {
instance = new ShareUtils();
mTencent = Tencent.createInstance("1104659243", context); //初始化QQ分享
api = WXAPIFactory.createWXAPI(context, "wx3ffd0785fad18396"); //初始化微信分享
}
instance.context = context;
return instance;
@ -67,15 +85,13 @@ public class ShareUtils {
this.shareNewsTitle = newsTitle;
this.isPlugin = isPlugin;
maxHeight = context.getResources().getDisplayMetrics().heightPixels;
RelativeLayout contentView = new RelativeLayout(context);
contentView.setBackgroundColor(0x4c000000);
contentView.setFocusable(true);
contentView.setFocusableInTouchMode(true);
RecyclerView shareRecyclerView = new RecyclerView(context);
shareRecyclerView.setPadding(DisplayUtils.dip2px(context, 4), DisplayUtils.dip2px(context, 4), DisplayUtils.dip2px(context, 4), 0);
shareRecyclerView.setPadding(DisplayUtils.dip2px(context, 10), DisplayUtils.dip2px(context, 4), DisplayUtils.dip2px(context, 10), 0);
shareRecyclerView.setBackgroundColor(Color.WHITE);
//RecyclerView禁止滑动
@ -207,90 +223,148 @@ public class ShareUtils {
//QQ分享
private void qqSahre(){
QQ.ShareParams qqParams = new QQ.ShareParams();
Utils.toast(context,"分享跳转中...");
Bundle params = new Bundle();
if (shareNewsTitle != null){
qqParams.setTitle(shareGameName);
qqParams.setText(shareNewsTitle);
params.putString(QQShare.SHARE_TO_QQ_TITLE, shareGameName);
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareNewsTitle);
}else {
qqParams.setTitle("向你推荐:");
params.putString(QQShare.SHARE_TO_QQ_TITLE, "向你推荐:");
if (isPlugin){
qqParams.setText(shareGameName + "(光环加速版)");
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareGameName + "(光环加速版)");
}else {
qqParams.setText(shareGameName);
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareGameName);
}
}
qqParams.setImageUrl(shareIcon);
qqParams.setTitleUrl(shareUrl);
qqParams.setSite("光环助手");
sharePlatform(qqParams,QQ.NAME);
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareIcon);
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "光环助手");
mTencent.shareToQQ(
(Activity) context, params, new IUiListener() {
@Override
public void onComplete(Object o) {
Toast.makeText(context, "QQ分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
Toast.makeText(context, "QQ分享失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(context, "QQ分享取消", Toast.LENGTH_SHORT).show();
}
});
popupWindow.dismiss();
}
//微信好友分享
private void wechatSahre(){
Wechat.ShareParams wechatParams = new Wechat.ShareParams();
Utils.toast(context,"分享跳转中...");
WXWebpageObject webpage = new WXWebpageObject();
WXMediaMessage msg = new WXMediaMessage(webpage);
webpage.webpageUrl = shareUrl;
if (shareNewsTitle != null){
wechatParams.setText(shareNewsTitle);
msg.title = shareGameName;
msg.description = shareNewsTitle;
}else {
if (isPlugin){
wechatParams.setTitle("向你推荐");
wechatParams.setText(shareGameName + "(光环加速版)");
msg.title = "向你推荐";
msg.description = shareGameName + "(光环加速版)";
}else {
wechatParams.setTitle("向你推荐");
wechatParams.setText(shareGameName);
msg.title = "向你推荐";
msg.description = shareGameName;
}
}
wechatParams.setShareType(Platform.SHARE_WEBPAGE);
wechatParams.setImageUrl(shareIcon);
wechatParams.setUrl(shareUrl);
sharePlatform(wechatParams,Wechat.NAME);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
loadBitMap(shareIcon, msg, req);
popupWindow.dismiss();
}
//QQ空间分享
private void qZoneSahre(){
QZone.ShareParams qZoneParams = new QZone.ShareParams();
Utils.toast(context,"分享跳转中...");
Bundle params = new Bundle();
if (shareNewsTitle != null){
qZoneParams.setTitle(shareGameName);
qZoneParams.setText(shareNewsTitle);
params.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareGameName);
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareNewsTitle);
}else {
qZoneParams.setTitle("向你推荐:");
params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "向你推荐:");
if (isPlugin){
qZoneParams.setText("向你推荐:" + shareGameName + "(光环加速版)");
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareGameName + "(光环加速版)");
}else {
qZoneParams.setText("向你推荐:" + shareGameName);
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareGameName);
}
}
qZoneParams.setImageUrl(shareIcon);
qZoneParams.setTitleUrl(shareUrl);
qZoneParams.setSite("光环助手");
sharePlatform(qZoneParams,QZone.NAME);
ArrayList<String> imageUrls = new ArrayList<String>();
imageUrls.add(shareIcon);
params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_NO_TYPE);
params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
params.putString(QzoneShare.SHARE_TO_QQ_APP_NAME, "光环助手");
mTencent.shareToQzone(
(Activity) context, params, new IUiListener() {
@Override
public void onComplete(Object o) {
Toast.makeText(context, "QQ空间分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
Toast.makeText(context, "QQ空间分享失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(context, "QQ空间分享取消", Toast.LENGTH_SHORT).show();
}
});
popupWindow.dismiss();
}
//微信朋友圈分享
private void wechatMomentsSahre(){
WechatMoments.ShareParams wechatMomentsParams = new WechatMoments.ShareParams();
Utils.toast(context,"分享跳转中...");
WXWebpageObject webpage = new WXWebpageObject();
WXMediaMessage msg = new WXMediaMessage(webpage);
webpage.webpageUrl = shareUrl;
if (shareNewsTitle != null){
wechatMomentsParams.setTitle(shareNewsTitle);
msg.title = shareNewsTitle;
}else {
if (isPlugin){
wechatMomentsParams.setTitle("向你推荐:" + shareGameName + "(光环加速版)");
msg.title = "向你推荐" + shareGameName + "(光环加速版)";
}else {
wechatMomentsParams.setTitle("向你推荐:" + shareGameName);
msg.title = "向你推荐" + shareGameName;
}
}
wechatMomentsParams.setShareType(Platform.SHARE_WEBPAGE);
wechatMomentsParams.setImageUrl(shareIcon);
wechatMomentsParams.setUrl(shareUrl);
sharePlatform(wechatMomentsParams, WechatMoments.NAME);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneTimeline;
loadBitMap(shareIcon, msg, req);
popupWindow.dismiss();
}
//新浪微博分享
private void sinaWeiboSahre(){
SinaWeibo.ShareParams sinaWeiboParams = new SinaWeibo.ShareParams();
@ -299,12 +373,12 @@ public class ShareUtils {
sinaWeiboParams.setText(shareNewsTitle);
}else {
if (isPlugin){
sinaWeiboParams.setText("向你推荐:" + shareGameName + "(光环加速版)");
sinaWeiboParams.setText("向你推荐:" + shareGameName + "(光环加速版)" + shareUrl);
}else {
sinaWeiboParams.setText("向你推荐:" + shareGameName);
sinaWeiboParams.setText("向你推荐:" + shareGameName + shareUrl);
}
}
sinaWeiboParams.setImageUrl(shareIcon);
sharePlatform(sinaWeiboParams, SinaWeibo.NAME);
}
@ -330,8 +404,10 @@ public class ShareUtils {
//分享平台回调
private void sharePlatform(Platform.ShareParams params, String name) {
Utils.toast(context,"分享跳转中...");
Platform platform = ShareSDK.getPlatform(name);
if (platform.getName().equals(SinaWeibo.NAME)){
platform.SSOSetting(true);
}
platform.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
@ -352,6 +428,10 @@ public class ShareUtils {
popupWindow.dismiss();
}
private String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
//复制文字链接
private void copyLink(String copyContent) {
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
@ -360,4 +440,30 @@ public class ShareUtils {
Utils.toast(context,"复制成功");
}
private void loadBitMap(final String iconUrl, final WXMediaMessage msg, final SendMessageToWX.Req req){
ImageUtils.getInstance(context).loadBitmap(iconUrl, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
}
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
msg.thumbData = Util.bmpToByteArray(bitmap, true);
api.sendReq(req);
}
@Override
public void onLoadingCancelled(String s, View view) {
}
});
}
}