在做自己的项目时,难免会使用分享的功能,自己写的话即浪费精力又浪费 时间,UMShare做了一个很好的SDK。做项目时,由于需求变来变去,从一开始的默认分享面板到在默认的面板添加自己的需求的平台(比如调用手机安装哪些程序),然后再到自己定义的界面。。。。为了让伙伴们使用方便,今天特地封装好了一个类供大家使用,如有更好的实现方法,请留言
使用该方法的前提是你集成号UM的SDK了,里边的图片和一些布局用到的参数需要自己填一下,微信分享必须是签名的才可以。
===========================================================================================
版权所有,如需转载请标明出处:http://blog.csdn.net/you4580
===========================================================================================================UMShareAgent.class
/**
* @ClassName: UMShareAgent
* @Description: 自定义友盟分享面板以及与之相关的操作
* @author you4580
* @date 2015年6月16日 上午11:25:43
*
*/
public class UMShareAgent extends PopupWindow implements View.OnClickListener {
public static final String DESCRIPTOR = "com.umeng.share";
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) {
this.activity=a;
initUMService();
}
/**
* @Title: initUMService
* @Description: 初始化
* @throws
*/
private void initUMService() {
if (mController == null) {
mController = UMServiceFactory.getUMSocialService(DESCRIPTOR);
//关闭提示
mController.getConfig().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);
}
// 是否只有已登录用户才能打开分享选择页
//mController.openShare(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)
this.activity=a;
if (title != null)
this.title = title;
if (content != null)
this.content = content;
if (imgUrl != null)
this.imgUrl = imgUrl;
if (shareUrl != null)
this.shareUrl = shareUrl;
if (isShow != null)
this.isShowBtn = 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)
this.webView = webView;
if (copyUrl != null)
this.copyUrl = copyUrl;
}
/**
* @功能描述 : 添加平台分享(新浪、QQ和QQ空间、微信和微信朋友圈)
* @return
*/
private void configPlatforms() {
// 添加新浪SSO授权
mController.getConfig().setSsoHandler(new SinaSsoHandler());
// 添加QQ、QZone平台
addQQQZonePlatform();
// 添加微信、微信朋友圈平台
addWXPlatform();
//addOtherPlatform();//在友盟分享的默认样式上添加自定义分享平台使用该方法
//默认分享列表中存在的平台如果需要删除,则调用下面的代码:
mController.getConfig().removePlatform( SHARE_MEDIA.TENCENT);
//设置分享列表的平台排列顺序,则使用下面的代码:
mController.getConfig().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", "其他", R.drawable.share_more );
mCustomPlatform.mGrayIcon = R.drawable.icon ;// 灰色图标id
mCustomPlatform.mClickListener = new SocializeListeners.OnSnsPlatformClickListener() {
// 平台点击事件,必须实现,在这里填写你的实际代码
@Override
public void onClick(Context context, SocializeEntity entity,
SocializeListeners.SnsPostListener listener) {
// 调用系统自带的分享平台
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT,"《"+ title+"》"+" "+content);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(Intent.createChooser(intent, "分享"));
}
};
//最后把平台添加到sdk中
mController.getConfig().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);
qqSsoHandler.addToSocialSDK();
// 添加QZone平台
QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity ,APPID, APPKEY);
qZoneSsoHandler.addToSocialSDK();
}
/**
* @功能描述 : 添加微信平台分享
* @return
*/
private void addWXPlatform() {
// 注意:在微信授权的时候,必须传递appSecret
// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
String appID = "appID "; /自己公司申请的appID
String appSecret = "appSecret "; //自己公司申请的appSecret
// 添加微信平台
UMWXHandler wxHandler = new UMWXHandler(activity,appID,appSecret);
//关闭微信提示:大于32k 压缩图片
wxHandler.showCompressToast(false);
wxHandler.addToSocialSDK();
// 支持微信朋友圈
UMWXHandler wxCircleHandler = new UMWXHandler(activity,appID,appSecret);
//关闭微信朋友圈提示:大于32k 压缩图片
wxCircleHandler.showCompressToast(false);
wxCircleHandler.setToCircle(true);
wxCircleHandler.addToSocialSDK();
}
/**
* @功能描述 : 设置分享平台
* @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(baseShareContent.equals(sinaShareContent)){
baseShareContent.setTitle( title);
//设置分享文字
baseShareContent.setShareContent(title+" "+content+" @sina ");
//设置分享图片
baseShareContent.setShareImage(new UMImage(activity,imgUrl));
//设置点击消息的跳转URL
baseShareContent.setTargetUrl(shareUrl);
mController.setShareMedia(baseShareContent);
}else{
baseShareContent.setTitle(title);
//设置分享文字
baseShareContent.setShareContent(content);
//设置分享图片
baseShareContent.setShareImage(new UMImage(activity,imgUrl));
//设置点击消息的跳转URL
baseShareContent.setTargetUrl(shareUrl);
mController.setShareMedia(baseShareContent);
}
}
/**
* @Title: initView
* @Description: 初始化友盟分享自定义面板上的控件,设置点击事件
* @param @param context
* @return void
* @throws
*/
@SuppressWarnings("deprecation")
private void initView(Context context,Boolean isShowBtn) {
View rootView = LayoutInflater.from(context).inflate(R.layout.umeng_share_custom_board, null);
rl_share= (RelativeLayout) rootView.findViewById(R.id.rl_share);
rl_share.setOnClickListener(this);
myImageButtonWeChat=(UMImageButtonShareItem) rootView.findViewById(R.id.wechat);
myImageButtonWeChat.setOnClickListener(this);
myImageButtonCircle=(UMImageButtonShareItem) rootView.findViewById(R.id.wechat_circle);
myImageButtonCircle.setOnClickListener(this);
myImageButtonQQ=(UMImageButtonShareItem) rootView.findViewById(R.id.qq);
myImageButtonQQ.setOnClickListener(this);
myImageButtonQZone=(UMImageButtonShareItem) rootView.findViewById(R.id.qzone);
myImageButtonQZone.setOnClickListener(this);
myImageButtonSina=(UMImageButtonShareItem) rootView.findViewById(R.id.sina);
myImageButtonSina.setOnClickListener(this);
myImageButtonOther=(UMImageButtonShareItem) rootView.findViewById(R.id.other);
myImageButtonOther.setOnClickListener(this);
rootView.findViewById(R.id.share_other_copyurl).setOnClickListener(this);
rootView.findViewById(R.id.share_other_open).setOnClickListener(this);
rootView.findViewById(R.id.share_other_reload).setOnClickListener(this);
ll_share_btn= (LinearLayout) rootView.findViewById(R.id.ll_share_btn);
//使用内置浏览器时,需要显示刷新,复制网页链接,在浏览器中打开等按钮
if(isShowBtn){
ll_share_btn.setVisibility(View.VISIBLE);
}
setContentView(rootView);
rootView.getBackground().setAlpha(100);
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchable(true);
}
/** (非 Javadoc)
* Title: onClick
* Description:分享面板中对应条目的点击事件
* @param v
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.wechat:
setAnimationAndSharePlates(SHARE_MEDIA.WEIXIN,myImageButtonWeChat.getImageViewbutton(),true);
break;
case R.id.wechat_circle:
setAnimationAndSharePlates(SHARE_MEDIA.WEIXIN_CIRCLE,myImageButtonCircle.getImageViewbutton(),true);
break;
case R.id.qq:
setAnimationAndSharePlates(SHARE_MEDIA.QQ,myImageButtonQQ.getImageViewbutton(),true);
break;
case R.id.qzone:
setAnimationAndSharePlates(SHARE_MEDIA.QZONE,myImageButtonQZone.getImageViewbutton(),true);
break;
case R.id.sina:
setAnimationAndSharePlates(SHARE_MEDIA.SINA,myImageButtonSina.getImageViewbutton(),true);
break;
case R.id.other:
setAnimationAndSharePlates(null,myImageButtonOther.getImageViewbutton(),false);
break;
case R.id.share_other_copyurl:
if(copyUrl!=null||copyUrl!=""){
if (Integer.valueOf(android.os.Build.VERSION.SDK) > 10) {
try {
ClipboardManager cmb = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
cmb.setText(copyUrl);
Toast.makeText(activity, "已复制到剪切板",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
this.dismiss();
break;
case R.id.share_other_open:
if(copyUrl!=null||copyUrl!=""){
try {
Uri uri = Uri.parse(copyUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(intent);
} catch (Exception e) {
Toast.makeText(activity, "抱歉,链接地址错误", Toast.LENGTH_SHORT).show();
}
}
this.dismiss();
break;
case R.id.share_other_reload:
if(webView!=null){
webView.reload();
}
this.dismiss();
break;
case R.id.rl_share:
if(this.isShowing()){
this.dismiss();
}
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 = PropertyValuesHolder.ofFloat("alpha", 1f,0f, 1f);
PropertyValuesHolder pvhY1 = PropertyValuesHolder.ofFloat("scaleX", 1f,0, 1f);
PropertyValuesHolder pvhZ1 = PropertyValuesHolder.ofFloat("scaleY", 1f,0, 1f);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(imageView, pvhX1, pvhY1,pvhZ1);
objectAnimator.setDuration(200);
objectAnimator.addListener(new Animator.AnimatorListener() {
@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);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, title+" "+content);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(Intent.createChooser(intent, "分享"));
dismiss();
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
objectAnimator.start();
}
/**
*
* @Title: performShare
* @Description: 自定义友盟分享操作的接口回调方法
* @param @param platform
* @return void
* @throws
*/
private void performShare(SHARE_MEDIA platform) {
// 参数1为Context类型对象, 参数2为要分享到的目标平台, 参数3为分享操作的回调接口
mController.postShare(activity, platform, new SocializeListeners.SnsPostListener() {
@Override
public void onStart() {
}
@Override
public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
String showText = platform.toString();
if (eCode == StatusCode.ST_CODE_SUCCESSED) {
showText += "平台分享成功";
} else {
showText += "平台分享失败";
}
Toast.makeText(activity, showText, Toast.LENGTH_SHORT).show();
dismiss();
}
});
}
=========================================================================================
umeng_share_custom_board.xml
android:layout_height="match_parent"
android:background="@color/um_share_blank_bg"
android:id="@+id/rl_share">
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@color/um_share_content_bg">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/umeng_socialize_my_wechat"
android:text="@string/um_share_my_wechat" >
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/umeng_socialize_my_wxcircle"
android:text="@string/um_share_my_wxcircle" >
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/umeng_socialize_my_qq"
android:text="@string/um_share_my_qq" >
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/umeng_socialize_my_qzone"
android:text="@string/um_share_my_qzone" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_share_top"
android:layout_marginBottom="20dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:orientation="horizontal">
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/umeng_socialize_my_sina"
android:text="@string/um_share_my_sina" >
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/umeng_socialize_my_other"
android:text="@string/um_share_my_other" >
android:layout_height="wrap_content"
android:layout_weight="1" >
android:layout_height="wrap_content"
android:layout_weight="1" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ll_share_bottom"
android:orientation="vertical"
android:visibility="gone">
android:layout_height="0.5dp"
android:background="@color/um_share_btn_line"/>
=====================================================================================
自定义的ImageButton
public class UMImageButtonShareItem extends LinearLayout {
private ImageView imageViewbutton;
private TextView textView;
public UMImageButtonShareItem(Context context,AttributeSet attrs) {
super(context,attrs);
imageViewbutton = new ImageView(context, attrs);
imageViewbutton.setPadding(DensityUtil.dip2px(16), 0, DensityUtil.dip2px(16), 0);
textView =new TextView(context, attrs);
//水平居中
textView.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
textView.setPadding(0, 0, 0, 0);
textView.setTextSize(13);
textView.setTextColor(getResources().getColor(R.color.um_share_textcolor));
setClickable(true);
setFocusable(true);
setOrientation(LinearLayout.VERTICAL);
addView(imageViewbutton);
addView(textView);
}
public ImageView getImageViewbutton() {
return imageViewbutton;
}
public void setImageViewbutton(ImageView imageViewbutton) {
this.imageViewbutton = imageViewbutton;
}
}
==========================================================================
在MainActivity中这样调用就可以了:
UMShareAgent umShareAgent = UMShareAgent.getInstance(MainActivity.this);
umShareAgent.oneKeyShare(MainActivity.this,false,title,content,imgUrl,targetUrl);
umShareAgent.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
==========================================================================