标签:
public class SharePresenter {
//QQ分享帐号
private final static String QQ_AppID = "***";
private final static String QQ_AppKey = "***";
//微信分享帐号
private final static String WX_AppID = "***";
private final static String WX_AppKey = "***";
// 整个平台的Controller, 负责管理整个SDK的配置、操作等处理
public static UMSocialService mController = UMServiceFactory.getUMSocialService("test");
private static void init(Activity activity) {
initPlatforms(activity);
initShareContent(activity);
}
/**
* 配置平台
*/
private static void initPlatforms(Activity activity) {
// 添加新浪SSO授权
mController.getConfig().setSsoHandler(new SinaSsoHandler());
// 添加QQ支持, 并且设置QQ分享内容的target url
UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity,QQ_AppID,QQ_AppKey);
qqSsoHandler.addToSocialSDK();
// 添加QZone平台
QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity,QQ_AppID,QQ_AppKey);
qZoneSsoHandler.addToSocialSDK();
// 添加微信平台
UMWXHandler wxHandler = new UMWXHandler(activity, WX_AppID, WX_AppKey);
wxHandler.addToSocialSDK();
// 支持微信朋友圈
UMWXHandler wxCircleHandler = new UMWXHandler(activity, WX_AppID, WX_AppKey);
wxCircleHandler.setToCircle(true);
wxCircleHandler.addToSocialSDK();
}
/**
* 配置分享内容
*/
private static void initShareContent(Activity activity) {
UMImage urlImage = new UMImage(activity,"http://www.che.com/upload/system/d95aa9d7-7980-4f61-84e1-3c28d320d051.png");
// 设置QQ空间分享内容
QZoneShareContent qzone = new QZoneShareContent();
qzone.setTitle("车城网分享测试");
qzone.setShareContent("买好车,上车城!");
qzone.setTargetUrl("http://www.che.com");
mController.setShareMedia(qzone);
// 设置QQ好友分享内容
QQShareContent qq = new QQShareContent();
qq.setTitle("车城网分享测试");
qq.setShareContent("买好车,上车城!");
qq.setTargetUrl("http://www.che.com");
mController.setShareMedia(qq);
//设置新浪微博分享内容
SinaShareContent sina = new SinaShareContent();
sina.setShareContent("买好车,上车城!车城是结合线上网络展示、线下展厅销售、金融服务的综合服务平台。http://www.che.com");
sina.setShareImage(new UMImage(activity, R.drawable.ic_main));
mController.setShareMedia(sina);
//设置微信好友分享内容
WeiXinShareContent wx = new WeiXinShareContent();
wx.setShareContent("车城是结合线上网络展示、线下展厅销售、金融服务的综合服务平台。http://www.che.com");
wx.setTitle("买好车,上车城!");
wx.setTargetUrl("http://www.che.com");
wx.setShareMedia(urlImage);
mController.setShareMedia(wx);
// 设置朋友圈分享的内容
CircleShareContent circle = new CircleShareContent();
circle.setShareContent("车城是结合线上网络展示、线下展厅销售、金融服务的综合服务平台。http://www.che.com");
circle.setTitle("买好车,上车城!");
circle.setTargetUrl("http://www.che.com");
circle.setShareMedia(urlImage);
mController.setShareMedia(circle);
}
/**
* 弹出对话框,分享到微信、QQ、新浪等
*/
public static void share(Activity activity) {
init(activity);
mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA);
mController.openShare(activity, false);
}
/**
* 弹出对话框,分享到微信、QQ、新浪等
*/
public static void share(Activity activity,String shareCarName,String sharePrice,String shareImageUrl,String shareUrl) {
LogUtil.e("shareCarName"+shareCarName+"\tshareImageUrl="+shareImageUrl+"\tshareUrl="+shareUrl);
initPlatforms(activity);
initShareContent(activity, shareCarName, sharePrice, shareImageUrl, shareUrl);
mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA);
mController.openShare(activity, false);
}
/**
* 配置分享内容
*/
private static void initShareContent(Activity activity,String shareCarName,String sharePrice,String shareImageUrl,String shareUrl) {
shareImageUrl="http://www.che.com/upload/system/d95aa9d7-7980-4f61-84e1-3c28d320d051.png";
UMImage urlImage = new UMImage(activity,shareImageUrl);
//设置新浪微博分享内容
SinaShareContent sina = new SinaShareContent();
sina.setShareContent("买好车,上车城!车城是结合线上网络展示、线下展厅销售、金融服务的综合服务平台。http://www.che.com");
sina.setShareImage(new UMImage(activity, R.drawable.isloading));
mController.setShareMedia(sina);
//设置微信好友分享内容
WeiXinShareContent wx = new WeiXinShareContent();
wx.setShareContent("车城交易价: "+sharePrice+",来车城分期贷款:支持0首付,车险办理,代办过户、延保");
wx.setTitle(shareCarName);
wx.setTargetUrl(shareUrl);
wx.setShareMedia(urlImage);
mController.setShareMedia(wx);
// 设置朋友圈分享的内容
CircleShareContent circle = new CircleShareContent();
circle.setShareContent("车城交易价: "+sharePrice+",来车城分期贷款:支持0首付,车险办理,代办过户、延保");
circle.setTitle(shareCarName);
circle.setTargetUrl(shareUrl);
circle.setShareMedia(urlImage);
mController.setShareMedia(circle);
}
}
// 添加微信平台
UMWXHandler wxHandler = new UMWXHandler(activity, WX_AppID, WX_AppKey);
wxHandler.addToSocialSDK();
public UMWXHandler(Context context, String appid, String secret) {
this.THUMB_SIZE = 150;
this.THUMB_LIMIT = ‘耀‘;
this.TITLE_LIMIT = 512;
this.DESCRIPTION_LIMIT = 1024;
this.REFRESH_TOKEN_EXPIRES = 604800;
this.WX_SECRET = "";
this.WX_DEFAULT_ICON = 0;
this.WXCIRCLE_DEFAULT_ICON = 0;
this.isToCircle = false;
this.mRefreshTokenAvailable = true;
this.TAG = "UMWXHandler";
this.SHOW_COMPRESS_TOAST = 1;
this.SHOW_TITLE_TOAST = 2;
this.isShowCompressToast = true;
this.mReportDesc = "";
this.mWxMediaMessage = null;
this.isFromAuth = false;
this.isWXSceneFavorite = false;
this.handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == 1 && UMWXHandler.this.isShowCompressToast) {
Toast.makeText(UMWXHandler.this.mContext, "图片大小超过32KB,正在对图片进行压缩...", 0).show();
} else if(msg.what == 2) {
Toast.makeText(UMWXHandler.this.mContext, "标题长度超过512 Bytes...", 0).show();
}
}
};
this.mEventHandler = new IWXAPIEventHandler() {
public void onResp(BaseResp resp) {
if(UMWXHandler.this.isFromAuth) {
UMWXHandler.this.dealOAuth(resp);
} else {
int statusCode = 0;
SHARE_MEDIA platform = SocializeConfig.getSelectedPlatfrom();
switch(resp.errCode) {
case -4:
statusCode = -4;
Log.d("UMWXHandler", "### 微信发送被拒绝");
break;
case -3:
statusCode = -3;
break;
case -2:
statusCode = ‘鱀‘;
Log.d("UMWXHandler", "### 微信分享取消");
break;
case -1:
statusCode = -1;
break;
case 0:
statusCode = 200;
Log.d("UMWXHandler", "### 微信分享成功.");
break;
default:
Log.d("UMWXHandler", "### 微信发送 -- 未知错误.");
}
if(!UMWXHandler.this.isFromAuth) {
UMWXHandler.this.mSocializeConfig.fireAllListenersOnComplete(SnsPostListener.class, platform, statusCode, UMWXHandler.mEntity);
if(statusCode == 200 && UMWXHandler.mEntity != null) {
SocializeUtils.sendAnalytic(UMWXHandler.this.mContext, UMWXHandler.mEntity.mDescriptor, UMWXHandler.this.mShareContent, UMWXHandler.this.mShareMedia, UMWXHandler.this.isToCircle?"wxtimeline":"wxsession");
}
}
}
}
public void onReq(BaseReq req) {
}
};
this.mContext = context.getApplicationContext();
this.WX_APPID = appid;
if(TextUtils.isEmpty(this.WX_APPID)) {
throw new NullPointerException("the weixin appid is null");
} else {
this.mWXApi = WXAPIFactory.createWXAPI(this.mContext, this.WX_APPID);
this.mWXApi.registerApp(this.WX_APPID);
this.WX_SECRET = secret;
this.mExtraData.put("wx_appid", this.WX_APPID);
if(TextUtils.isEmpty(this.WX_SECRET)) {
this.WX_SECRET = "";
}
this.mExtraData.put("wx_secret", this.WX_SECRET);
if(this.WX_DEFAULT_ICON == 0 || this.WXCIRCLE_DEFAULT_ICON == 0) {
this.WX_DEFAULT_ICON = ResContainer.getResourceId(context, ResType.DRAWABLE, "umeng_socialize_wechat");
this.WXCIRCLE_DEFAULT_ICON = ResContainer.getResourceId(context, ResType.DRAWABLE, "umeng_socialize_wxcircle");
}
}
}
public void addToSocialSDK() {
this.mSocializeConfig.addCustomPlatform(this.build());
this.mSocializeConfig.setSsoHandler(this);
}
public void addCustomPlatform(CustomPlatform var1) {
a((SnsPlatform)var1);
}
public void setSsoHandler(UMSsoHandler var1) {
if(var1 == null) {
Log.w(b, "ssoHander is null");
} else {
int var2 = var1.getRequstCode();
Log.d("", "#### set sso handler, code = " + var2);
f.put(var2, var1);
CustomPlatform var3 = var1.build();
if(var3 != null) {
this.addCustomPlatform(var3);
}
}
}
private static void a(SnsPlatform var0) {
if(var0 != null && !TextUtils.isEmpty(var0.mKeyword)) {
String var1 = var0.mKeyword;
if(t.containsKey(var1)) {
t.remove(var1);
}
t.put(var1, var0);
}
}
private static Map<String, SnsPlatform> t;
public final CustomPlatform build() {
return this.mCustomPlatform != null?this.mCustomPlatform:this.createNewPlatform();
}
protected abstract CustomPlatform createNewPlatform();
public final CustomPlatform createNewPlatform() {
int icon = this.isToCircle?this.WXCIRCLE_DEFAULT_ICON:this.WX_DEFAULT_ICON;
String keyWord = this.isToCircle?"weixin_circle":"weixin";
String showWord = this.isToCircle?"朋友圈":"微信";
this.mCustomPlatform = new CustomPlatform(keyWord, showWord, icon);
boolean grayIcon = false;
int grayIcon1;
if(this.isToCircle) {
grayIcon1 = ResContainer.getResourceId(this.mContext, ResType.DRAWABLE, "umeng_socialize_wxcircle_gray");
} else {
grayIcon1 = ResContainer.getResourceId(this.mContext, ResType.DRAWABLE, "umeng_socialize_wechat_gray");
}
this.mCustomPlatform.mGrayIcon = grayIcon1;
this.mCustomPlatform.mClickListener = new OnSnsPlatformClickListener() {
public void onClick(Context context, SocializeEntity entity, SnsPostListener listener) {
UMWXHandler.this.handleOnClick(context, UMWXHandler.this.mCustomPlatform, entity, listener);
}
};
return this.mCustomPlatform;
}
//设置微信好友分享内容
WeiXinShareContent wx = new WeiXinShareContent();
wx.setShareContent("车城是结合线上网络展示、线下展厅销售、金融服务的综合服务平台。http://www.che.com");
wx.setTitle("买好车,上车城!");
wx.setTargetUrl("http://www.che.com");
wx.setShareMedia(urlImage);
mController.setShareMedia(wx);
// 整个平台的Controller, 负责管理整个SDK的配置、操作等处理
public static UMSocialService mController = UMServiceFactory.getUMSocialService("test");
public static UMSocialService getUMSocialService(String var0) {
return getUMSocialService(var0, (RequestType)null);
}
@Deprecated
public static UMSocialService getUMSocialService(String var0, RequestType var1) {
SocializeEntity var2 = null;
if(var1 == null) {
var1 = RequestType.SOCIAL;
}
String var3 = var0 + var1;
if(c.g.containsKey(var3)) {
var2 = (SocializeEntity)c.g.get(var3);
return new c(var2);
} else {
var3 = var0 + RequestType.SOCIAL;
if(var1 == RequestType.ANALYTICS && c.g.containsKey(var3)) {
SocializeEntity var4 = (SocializeEntity)c.g.get(var3);
var2 = SocializeEntity.cloneNew(var4, var1);
} else {
var2 = new SocializeEntity(var0, var1);
}
return new c(var2);
}
}
public static volatile Map<String, SocializeEntity> g = new HashMap();
public c(SocializeEntity var1) {
super(var1);
}
public InitializeController(SocializeEntity var1) {
super(var1);
}
public BaseController(SocializeEntity var1) {
this.a = var1;
this.c = (CommentService)b.a(this.a, a.b, new Object[0]);
this.d = (LikeService)b.a(this.a, a.d, new Object[0]);
this.e = (AuthService)b.a(this.a, a.a, new Object[0]);
this.b = (ShareService)b.a(this.a, a.c, new Object[0]);
this.f = (UserCenterService)b.a(this.a, a.e, new Object[]{this.e});
}
boolean setShareMedia(UMediaObject var1);
public boolean setShareMedia(UMediaObject var1) {
if(var1 == null) {
this.a.setMedia((UMediaObject)null);
return true;
} else if(var1.isMultiMedia()) {
this.a.setMedia(var1);
return true;
} else {
Log.w(h, "unable set share media.type is no support.");
return false;
}
}
public void setMedia(UMediaObject var1) {
SHARE_MEDIA var2 = SHARE_MEDIA.GENERIC;
if(var1 != null) {
var2 = var1.getTargetPlatform();
}
if(this.h.containsKey(var2)) {
this.h.remove(var2);
}
this.h.put(var2, var1);
}
private Map<SHARE_MEDIA, UMediaObject> h = new HashMap();
WEIXIN("weixin") {
public int getReqCode() {
return 10086;
}
public boolean isSupportAuthorization() {
return true;
}
},
mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA);
mController.openShare(activity, false);
public void setPlatforms(SHARE_MEDIA... var1) {
this.v.clear();
if(var1 != null && var1.length > 0) {
SHARE_MEDIA[] var2 = var1;
int var3 = var1.length;
for(int var4 = 0; var4 < var3; ++var4) {
SHARE_MEDIA var5 = var2[var4];
if(SocializeUtils.isValidPlatform(var5)) {
this.v.add(var5);
}
}
}
}
private List<SHARE_MEDIA> v;
void openShare(Activity var1, boolean var2);
public void openShare(Activity var1, boolean var2) {
this.b.openShare(var1, var2);
}
public void openShare(Activity var1, boolean var2) {
this.g = var2;
this.openShare(var1, (SnsPostListener)null);
}
public void openShare(Activity var1, SnsPostListener var2) {
this.a();
if(this.e(var1)) {
this.a(var1);
this.a.addStatisticsData(var1, SHARE_MEDIA.GENERIC, 1);
this.f = null;
a var3 = new a(var1);
var3.setLayoutParams(new LayoutParams(-1, -1));
this.f = new com.umeng.socialize.view.b(var1, var3, UMServiceFactory.getUMSocialService(this.a.mDescriptor));
this.f.setFocusable(true);
this.f.setBackgroundDrawable(new BitmapDrawable());
this.f.a(this.i);
var3.a(new OnClickListener() {
public void onClick(View var1) {
b.this.f.dismiss();
}
});
if(var2 != null) {
this.c.registerListener(var2);
}
if(this.g) {
this.d(var1);
} else {
this.f.showAtLocation(var1.getWindow().getDecorView(), 80, 0, 0);
}
}
}
public void showAtLocation(View var1, int var2, int var3, int var4) {
try {
super.showAtLocation(var1, var2, var3, var4);
if(this.e != null) {
this.e.onShow();
}
} catch (Exception var6) {
var6.printStackTrace();
}
}
var4.setOnClickListener(new OnClickListener() {
public void onClick(View var1x) {
b.this.dismiss();
SHARE_MEDIA var2 = SHARE_MEDIA.convertToEmun(var3.mKeyword);
if(!DeviceConfig.isNetworkAvailable(b.this.a) && var2 != SHARE_MEDIA.SMS) {
Toast.makeText(b.this.a, "您的网络不可用,请检查网络连接...", 0).show();
} else {
a(var3, var2);
}
}
});
private void a(SnsPlatform var1x, SHARE_MEDIA var2) {
if(var2 != null) {
b.this.c.getEntity().addStatisticsData(b.this.a, var2, 14);
}
if(var1x != null) {
SocializeConfig.setSelectedPlatfrom(var2);
SnsPostListener var3 = ListenerUtils.createSnsPostListener();
var1x.performClick(b.this.a, b.this.c.getEntity(), var3);
}
}
public void performClick(Context var1, SocializeEntity var2, SnsPostListener var3) {
if(this.mClickListener != null) {
this.mClickListener.onClick(var1, var2, var3);
}
}
public interface OnSnsPlatformClickListener {
void onClick(Context var1, SocializeEntity var2, SocializeListeners.SnsPostListener var3);
}
标签:
原文地址:http://www.cnblogs.com/yutianran/p/5076769.html