ShareSdkUtil.java
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.cnlive.goldenline.util;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.ui.widget.SharePopupWindow;
import com.mob.tools.utils.UIHandler;
import java.util.HashMap;
import java.util.List;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import static com.cnlive.goldenline.util.LogUtils.LOGD;
import static com.cnlive.goldenline.util.LogUtils.LOGE;
public class ShareSdkUtil {
public static final int SHARE_VIDEO = 1;
public static final int SHARE_IMAGE = 2;
public static final int SHARE_CONTENT = 3;
private static final int MSG_ACTION_CCALLBACK = 4;
@Nullable
private static SharePopupWindow popupWindow;
private static Context mContext;
@NonNull
private static Callback callback = new Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
switch (msg.arg1) {
case 1:
// 成功 用tostutil微信回调不起作用,所以改用tost
Toast.makeText(mContext, mContext.getString(R.string.toast_msg_share_success), Toast.LENGTH_LONG).show();
break;
case 2:
// 失败
Toast.makeText(mContext, mContext.getString(R.string.toast_msg_share_fail), Toast.LENGTH_LONG).show();
break;
case 3:
// 取消
Toast.makeText(mContext, mContext.getString(R.string.toast_msg_share_cancel), Toast.LENGTH_LONG).show();
break;
}
return false;
}
};
private static Platform sPlat;
//获取第三方平台用户资料
public static void loginThirdPlatform(Context context, String platformName, PlatformActionListener paListener) {
initShareSdk(context);
Platform plat = ShareSDK.getPlatform(context, platformName);
if (plat == null) {
return;
}
if (plat.isValid()) {//先清除缓存
plat.removeAccount();
}
plat.setPlatformActionListener(paListener);
//使用SSO授权,通过客户单授权
plat.SSOSetting(false);
plat.showUser(null);//获取登陆用户资料
}
public static void selectSharePlatform(@Nullable Activity a, int showAtLocationId, int type, String title, String content, String img, String targetUrl) {
if (a == null)
return;
if (popupWindow != null)
popupWindow.dismiss();
popupWindow = new SharePopupWindow(a.findViewById(showAtLocationId), a, type, title, content, img, targetUrl);
}
public static void sharePlatform(@NonNull Context context, @NonNull String platformName, int type, String title, String content, String img, String targetUrl) {
mContext = context;
if (popupWindow != null)
popupWindow.dismiss();
initShareSdk(context);
if (type == SHARE_VIDEO) {
LOGE(" share 视频 ");
shareMediaVideo(context, platformName, title, content, img, targetUrl);
} else if (type == SHARE_IMAGE) {
LOGE(" share 图片 ");
shareMediaImage(context, platformName, title, content, img, targetUrl);
} else if (type == SHARE_CONTENT) {
LOGE(" share 视频1 ");
shareMediaContent(context, platformName, title, content, img, targetUrl);
}
}
private static void shareMediaContent(@NonNull final Context context, @NonNull String platformName, String title, String content, String img, String targetUrl) {
Platform.ShareParams sp = new Platform.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE);
sp.setTitleUrl(targetUrl);
LOGE("share img ==" + img);
sp.setImagePath(img);
if (platformName.equals(SinaWeibo.NAME)) {
sp.setTitle(title);
sp.setText(content + targetUrl);
} else if (platformName.equals(Wechat.NAME)) {
sp.setUrl(targetUrl);
sp.setTitle(title);
sp.setText(content);
} else if (platformName.equals(WechatMoments.NAME)) {
sp.setUrl(targetUrl);
sp.setTitle(content);
sp.setText(content);
} else if (platformName.equals(QZone.NAME)) {
sp.setTitle(title);
sp.setText(content);
sp.setSite("正在上演");
sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
}
directShare(context, platformName, sp);
}
private static void shareMediaImage(@NonNull final Context context, @NonNull String platformName, String title, String content, String img, String targetUrl) {
Platform.ShareParams sp = new Platform.ShareParams();
LOGD("platformName= " + platformName);
sp.setShareType(Platform.SHARE_IMAGE);
// sp.setTitle(title);
sp.setImageUrl(img);
if (platformName.equals(SinaWeibo.NAME)) {
sp.setText(title);
} else if (platformName.equals(Wechat.NAME)) {
sp.setSite("正在上演");
sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
LOGD("targetUrl= " + targetUrl);
} else if (platformName.equals(WechatMoments.NAME)) {
sp.setSite("正在上演");
sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
} else if (platformName.equals(QZone.NAME)) {
// sp.setTitleUrl(targetUrl);
sp.setText(content);
sp.setSite("正在上演");
sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
}
directShare(context, platformName, sp);
}
private static void shareMediaVideo(@NonNull Context context, @NonNull String platformName, String title, String content, String img, String targetUrl) {
Platform.ShareParams sp = new Platform.ShareParams();
sp.setShareType(Platform.SHARE_VIDEO);
sp.setTitle("【正在上演】"+title);
LogUtils.LOGD("share img=" + img);
sp.setImageUrl(img);
// sp.setImagePath(img);
if (platformName.equals(SinaWeibo.NAME)) {
sp.setText("【正在上演】"+"《" + title + "》" + content + targetUrl);
} else if (platformName.equals(Wechat.NAME)) {
sp.setUrl(targetUrl);
sp.setText(content);
Log.e("111111111111111111",targetUrl+" "+content);
} else if (platformName.equals(WechatMoments.NAME)) {
sp.setUrl(targetUrl);
sp.setText(content);
Log.e("22222222222",targetUrl+" "+content);
} else if (platformName.equals(QZone.NAME)) {
sp.setTitleUrl(targetUrl);
sp.setText(content);
sp.setSite("正在上演");
sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
}
// Platform.ShareParams wechat = new Platform.ShareParams();
// wechat.setTitle("分享标题");
// wechat.setText("分享文本 http://www.baidu.com");
// wechat.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
// wechat.setUrl("http://www.baidu.com");
// wechat.setShareType(Platform. SHARE_WEBPAGE);
directShare(context, platformName, sp);
}
private static void shareComplete(Activity a, String platformName) {
// if (platformName.equals(SinaWeibo.NAME)) {
// HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_sina);
// } else if (platformName.equals(Wechat.NAME)) {
// HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_weixin);
// } else if (platformName.equals(WechatMoments.NAME)) {
// HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_weixin_circle);
// } else if (platformName.equals(QZone.NAME)) {
// HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_qzone);
// }
}
private static void directShare(final Context context, @NonNull final String platformName, Platform.ShareParams sp) {
Log.e("TAGTAGTAG=====",platformName);
Platform platform = ShareSDK.getPlatform(platformName);
Log.e("222222222222222=====",platform.toString());
platform.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int action, HashMap<String, Object> stringObjectHashMap) {
// 成功
LOGD("shareonComplete" + action + " " + platformName.toString());
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 1;
msg.arg2 = action;
msg.obj = platform;
UIHandler.sendMessage(msg, callback);
}
@Override
public void onError(Platform platform, int action, @NonNull Throwable throwable) {
// 失敗
//打印错误信息,print the error msg
throwable.printStackTrace();
//错误监听,handle the error msg
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 2;
msg.arg2 = action;
msg.obj = throwable;
LogUtils.LOGD("share " + msg.toString());
// UIHandler.sendMessage(msg, callback);
}
@Override
public void onCancel(Platform platform, int action) {
// 取消
Message msg = new Message();
msg.what = MSG_ACTION_CCALLBACK;
msg.arg1 = 3;
msg.arg2 = action;
msg.obj = platform;
// UIHandler.sendMessage(msg, callback);
}
}); // 设置分享事件回调
// 执行图文分享
platform.share(sp);
}
public static void initShareSdk(Context context) {
//初始化shareSdk 参数appkey
ShareSDK.initSDK(context);
}
public static boolean isAppAvilible(@NonNull Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
for (int i = 0; i < pinfo.size(); i++) {
if (pinfo.get(i).packageName.equalsIgnoreCase(packageName)) {
return true;
}
}
return false;
}
}