/**
* @ClassName: UMShareAgent
* @Description: 自定义友盟分享面板以及与之相关的操作
* @author you4580
* @date 2015年6月16日 上午11:25:43
*
*/
public class UMShareAgent extends PopupWindow implements {
public static final String DESCRIPTOR = "";
private static UMSocialService mController = null;
private Activity activity=null;
private static UMShareAgent mUMShareAgent=null;
private SinaShareContent sinaShareContent;
private UMImageButtonShareItem myImageButtonWeChat,myImageButtonCircle,
myImageButtonQQ,myImageButtonQZone,myImageButtonSina,myImageButtonOther;
private LinearLayout ll_share_btn;
private RelativeLayout rl_share;
private UMShareAgent(Activity a) {
=a;
initUMService();
}
/**
* @Title: initUMService
* @Description: 初始化
* @throws
*/
private void initUMService() {
if (mController == null) {
mController = (DESCRIPTOR);
//关闭提示
().closeToast();
}
configPlatforms();
}
/**
* @Title: getInstance
* @Description: 获取UMShareAgent对象
* @param @param a 当前的上下文
* @param @return
* @return UMShareAgent
* @throws
*/
public static UMShareAgent getInstance(Activity a) {
if (mUMShareAgent == null) {
mUMShareAgent = new UMShareAgent(a);
}
// 是否只有已登录用户才能打开分享选择页
//(a, false);
return mUMShareAgent;
}
private Boolean isShowBtn=false;
private WebView webView = null;
private String copyUrl = "";
private String title = "";
private String content = "";
private String imgUrl = "";
private String shareUrl = "";
/**
* @Title: oneKeyShare
* @Description: 一键分享传入的内容
* @param @param a 当前的上下文
* @param @param title 分享的标题
* @param @param url 分享的内容,实际显示的就是个URL
* @param @param imgUrl 分享的图片URL
* @param @param shareUrl 点击后跳转目标的URL
* @return void
* @throws
*/
public void oneKeyShare(Activity a,Boolean isShow,String title,String content,String imgUrl, String shareUrl) {
if(a!=null)
=a;
if (title != null)
= title;
if (content != null)
= content;
if (imgUrl != null)
= imgUrl;
if (shareUrl != null)
= shareUrl;
if (isShow != null)
= isShow;
initView(activity,isShowBtn);
setSharePlateform();
}
/**
* @Title: oneKeyOther
* @Description: 在友盟分享面板中添加的其他功能
* @param @param webView 重新加载内置浏览器的内容会用到
* @param @param copyUrl 需要复制的网址和在浏览器中打开的网址
* @return void
* @throws
*/
public void oneKeyOther(WebView webView,String copyUrl) {
if (webView != null)
= webView;
if (copyUrl != null)
= copyUrl;
}
/**
* @功能描述 : 添加平台分享(新浪、QQ和QQ空间、微信和微信朋友圈)
* @return
*/
private void configPlatforms() {
// 添加新浪SSO授权
().setSsoHandler(new SinaSsoHandler());
// 添加QQ、QZone平台
addQQQZonePlatform();
// 添加微信、微信朋友圈平台
addWXPlatform();
//addOtherPlatform();//在友盟分享的默认样式上添加自定义分享平台使用该方法
//默认分享列表中存在的平台如果需要删除,则调用下面的代码:
().removePlatform( SHARE_MEDIA.TENCENT);
//设置分享列表的平台排列顺序,则使用下面的代码:
().setPlatformOrder(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.SINA);
}
/**
* @功能描述 : 添加自定义平台分享
* @return
*/
public void addOtherPlatform() {
CustomPlatform mCustomPlatform = new CustomPlatform("OTHER_SHARE", "其他", .share_more );
= ;// 灰色图标id
= new () {
// 平台点击事件,必须实现,在这里填写你的实际代码
@Override
public void onClick(Context context, SocializeEntity entity,
listener) {
// 调用系统自带的分享平台
Intent intent = new Intent(Intent.ACTION_SEND);
("text/plain");
(Intent.EXTRA_SUBJECT, "分享");
(Intent.EXTRA_TEXT,"《"+ title+"》"+" "+content);
(Intent.FLAG_ACTIVITY_NEW_TASK);
((intent, "分享"));
}
};
//最后把平台添加到sdk中
().addCustomPlatform(mCustomPlatform);
}
/**
* @功能描述 : 添加QQ平台分享(包括QQ和QQ空间)
* @return
*/
private void addQQQZonePlatform() {
String APPID = "APPID "; //自己公司申请的APPID
String APPKEY = "APPKEY "; //自己公司申请的APPKEY
// 添加QQ支持, 并且设置QQ分享内容的target url
UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity ,APPID, APPKEY);
();
// 添加QZone平台
QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity ,APPID, APPKEY);
();
}
/**
* @功能描述 : 添加微信平台分享
* @return
*/
private void addWXPlatform() {
// 注意:在微信授权的时候,必须传递appSecret
// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
String appID = "appID "; /自己公司申请的appID
String appSecret = "appSecret "; //自己公司申请的appSecret
// 添加微信平台
UMWXHandler wxHandler = new UMWXHandler(activity,appID,appSecret);
//关闭微信提示:大于32k 压缩图片
(false);
();
// 支持微信朋友圈
UMWXHandler wxCircleHandler = new UMWXHandler(activity,appID,appSecret);
//关闭微信朋友圈提示:大于32k 压缩图片
(false);
(true);
();
}
/**
* @功能描述 : 设置分享平台
* @return
*/
private void setSharePlateform() {
// 设置新浪微博分享内容
sinaShareContent = new SinaShareContent();
setShareContent(sinaShareContent);
// 设置QQ空间分享内容
QZoneShareContent qzone = new QZoneShareContent();
setShareContent(qzone);
// 设置分享给QQ好友的内容
QQShareContent qqShareContent = new QQShareContent();
setShareContent(qqShareContent);
//设置微信好友分享内容
WeiXinShareContent weixinContent = new WeiXinShareContent();
setShareContent(weixinContent);
//设置微信朋友圈分享内容
CircleShareContent circleMedia = new CircleShareContent();
setShareContent(circleMedia);
}
/**
* @功能描述 : 设置对应平台分享的内容
* baseShareContent 需要传入分享的平台
* @return
*/
private void setShareContent(BaseShareContent baseShareContent) {
if((sinaShareContent)){
( title);
//设置分享文字
(title+" "+content+" @sina ");
//设置分享图片
(new UMImage(activity,imgUrl));
//设置点击消息的跳转URL
(shareUrl);
(baseShareContent);
}else{
(title);
//设置分享文字
(content);
//设置分享图片
(new UMImage(activity,imgUrl));
//设置点击消息的跳转URL
(shareUrl);
(baseShareContent);
}
}
/**
* @Title: initView
* @Description: 初始化友盟分享自定义面板上的控件,设置点击事件
* @param @param context
* @return void
* @throws
*/
@SuppressWarnings("deprecation")
private void initView(Context context,Boolean isShowBtn) {
View rootView = (context).inflate(.umeng_share_custom_board, null);
rl_share= (RelativeLayout) (.rl_share);
rl_share.setOnClickListener(this);
myImageButtonWeChat=(UMImageButtonShareItem) ();
(this);
myImageButtonCircle=(UMImageButtonShareItem) (.wechat_circle);
(this);
myImageButtonQQ=(UMImageButtonShareItem) ();
(this);
myImageButtonQZone=(UMImageButtonShareItem) ();
(this);
myImageButtonSina=(UMImageButtonShareItem) ();
(this);
myImageButtonOther=(UMImageButtonShareItem) ();
(this);
(.share_other_copyurl).setOnClickListener(this);
(.share_other_open).setOnClickListener(this);
(.share_other_reload).setOnClickListener(this);
ll_share_btn= (LinearLayout) (.ll_share_btn);
//使用内置浏览器时,需要显示刷新,复制网页链接,在浏览器中打开等按钮
if(isShowBtn){
ll_share_btn.setVisibility();
}
setContentView(rootView);
().setAlpha(100);
setWidth(.MATCH_PARENT);
setHeight(.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchable(true);
}
/** (非 Javadoc)
* Title: onClick
* Description:分享面板中对应条目的点击事件
* @param v
* @see #onClick()
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
int id = ();
switch (id) {
case :
setAnimationAndSharePlates(SHARE_MEDIA.WEIXIN,(),true);
break;
case .wechat_circle:
setAnimationAndSharePlates(SHARE_MEDIA.WEIXIN_CIRCLE,(),true);
break;
case :
setAnimationAndSharePlates(SHARE_MEDIA.QQ,(),true);
break;
case :
setAnimationAndSharePlates(SHARE_MEDIA.QZONE,(),true);
break;
case :
setAnimationAndSharePlates(SHARE_MEDIA.SINA,(),true);
break;
case :
setAnimationAndSharePlates(null,(),false);
break;
case .share_other_copyurl:
if(copyUrl!=null||copyUrl!=""){
if (() > 10) {
try {
ClipboardManager cmb = (ClipboardManager) (Context.CLIPBOARD_SERVICE);
(copyUrl);
(activity, "已复制到剪切板",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
();
}
}
}
();
break;
case .share_other_open:
if(copyUrl!=null||copyUrl!=""){
try {
Uri uri = (copyUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
(intent);
} catch (Exception e) {
(activity, "抱歉,链接地址错误", Toast.LENGTH_SHORT).show();
}
}
();
break;
case .share_other_reload:
if(webView!=null){
();
}
();
break;
case .rl_share:
if(()){
();
}
break;
default:
break;
}
}
/**
* @Title: setAnimationAndSharePlates
* @Description: TODO设置点击的动画和动画后跳转的分享平台
* @param @param platform 分享的平台
* @param @param imageView 产生动画效果的Image
* @param @param flag 是否是友盟分享(true是友盟分享,false是调用系统默认的分享功能)
* @return void
* @throws
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setAnimationAndSharePlates(final SHARE_MEDIA platform,ImageView imageView,final boolean flag) {
PropertyValuesHolder pvhX1 = ("alpha", 1f,0f, 1f);
PropertyValuesHolder pvhY1 = ("scaleX", 1f,0, 1f);
PropertyValuesHolder pvhZ1 = ("scaleY", 1f,0, 1f);
ObjectAnimator objectAnimator = (imageView, pvhX1, pvhY1,pvhZ1);
(200);
(new () {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if(flag==true){
//自定义友盟分享操作
performShare(platform);
}else{
//调用系统的分享面板
Intent intent = new Intent(Intent.ACTION_SEND);
("text/plain");
(Intent.EXTRA_SUBJECT, "分享");
(Intent.EXTRA_TEXT, title+" "+content);
(Intent.FLAG_ACTIVITY_NEW_TASK);
((intent, "分享"));
dismiss();
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
();
}
/**
*
* @Title: performShare
* @Description: 自定义友盟分享操作的接口回调方法
* @param @param platform
* @return void
* @throws
*/
private void performShare(SHARE_MEDIA platform) {
// 参数1为Context类型对象, 参数2为要分享到的目标平台, 参数3为分享操作的回调接口
(activity, platform, new () {
@Override
public void onStart() {
}
@Override
public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
String showText = ();
if (eCode == StatusCode.ST_CODE_SUCCESSED) {
showText += "平台分享成功";
} else {
showText += "平台分享失败";
}
(activity, showText, Toast.LENGTH_SHORT).show();
dismiss();
}
});
}