parent
dd20085bc4
commit
bab5324815
Binary file not shown.
@ -0,0 +1,67 @@ |
||||
package com.android.sdk.social.qq; |
||||
|
||||
import android.app.Activity; |
||||
import android.content.Context; |
||||
import android.support.annotation.Nullable; |
||||
|
||||
import com.android.sdk.social.common.Utils; |
||||
import com.tencent.tauth.IUiListener; |
||||
import com.tencent.tauth.Tencent; |
||||
import com.tencent.tauth.UiError; |
||||
|
||||
import timber.log.Timber; |
||||
|
||||
/** |
||||
* @author Ztiany |
||||
* Email: ztiany3@gmail.com |
||||
* Date : 2019-08-27 18:16 |
||||
*/ |
||||
public class QQManager { |
||||
|
||||
private final Tencent mTencent; |
||||
|
||||
private static String sAppId; |
||||
|
||||
public static void initQQSDK(String appId) { |
||||
sAppId = appId; |
||||
} |
||||
|
||||
private static String getAppId() { |
||||
Utils.requestNotNull(sAppId, "weChat app id"); |
||||
return sAppId; |
||||
} |
||||
|
||||
public QQManager(Context context) { |
||||
mTencent = Tencent.createInstance(getAppId(), context); |
||||
} |
||||
|
||||
public void shareToQQ(Activity activity, QQShareInfo shareInfo, @Nullable ShareResultCallback shareResultCallback) { |
||||
mTencent.shareToQQ(activity, shareInfo.getBundle(), newDefaultListener(shareResultCallback)); |
||||
} |
||||
|
||||
private static IUiListener newDefaultListener(ShareResultCallback shareResultCallback) { |
||||
if (shareResultCallback == null) { |
||||
return null; |
||||
} |
||||
return new IUiListener() { |
||||
@Override |
||||
public void onComplete(Object o) { |
||||
Timber.d("shareToQQ onComplete: " + o); |
||||
shareResultCallback.onSuccess(); |
||||
} |
||||
|
||||
@Override |
||||
public void onError(UiError uiError) { |
||||
Timber.d("shareToQQ onError: " + uiError); |
||||
shareResultCallback.onError(); |
||||
} |
||||
|
||||
@Override |
||||
public void onCancel() { |
||||
Timber.d("shareToQQ onCancel"); |
||||
shareResultCallback.onCancel(); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,54 @@ |
||||
package com.android.sdk.social.qq; |
||||
|
||||
import android.os.Bundle; |
||||
|
||||
import com.tencent.connect.share.QQShare; |
||||
|
||||
/** |
||||
* @author Ztiany |
||||
* Email: ztiany3@gmail.com |
||||
* Date : 2019-08-27 19:00 |
||||
*/ |
||||
public class QQShareInfo { |
||||
|
||||
private final Bundle mBundle = new Bundle(); |
||||
|
||||
public QQShareInfo() { |
||||
shareToFriend(); |
||||
} |
||||
|
||||
public QQShareInfo setTitle(String title) { |
||||
mBundle.putString(QQShare.SHARE_TO_QQ_TITLE, title); |
||||
return this; |
||||
} |
||||
|
||||
public QQShareInfo setTargetUrl(String targetUrl) { |
||||
mBundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, targetUrl); |
||||
return this; |
||||
} |
||||
|
||||
public QQShareInfo setSummary(String summary) { |
||||
mBundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary); |
||||
return this; |
||||
} |
||||
|
||||
public QQShareInfo setImage(String imageUrl) { |
||||
mBundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageUrl); |
||||
return this; |
||||
} |
||||
|
||||
public QQShareInfo setLocalImage(String imageUrl) { |
||||
mBundle.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, imageUrl); |
||||
return this; |
||||
} |
||||
|
||||
public QQShareInfo shareToFriend() { |
||||
mBundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT); |
||||
return this; |
||||
} |
||||
|
||||
Bundle getBundle() { |
||||
return mBundle; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,16 @@ |
||||
package com.android.sdk.social.qq; |
||||
|
||||
/** |
||||
* @author Ztiany |
||||
* Email: ztiany3@gmail.com |
||||
* Date : 2019-08-27 19:35 |
||||
*/ |
||||
public interface ShareResultCallback { |
||||
|
||||
void onSuccess(); |
||||
|
||||
void onError(); |
||||
|
||||
void onCancel(); |
||||
|
||||
} |
@ -0,0 +1,115 @@ |
||||
package com.android.sdk.social.wechat; |
||||
|
||||
import android.support.annotation.Nullable; |
||||
|
||||
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX; |
||||
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage; |
||||
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject; |
||||
|
||||
/** |
||||
* @author Ztiany |
||||
* Email: ztiany3@gmail.com |
||||
* Date : 2019-08-27 17:04 |
||||
*/ |
||||
public class WeChatShareInfo { |
||||
|
||||
public static final int SCENE_FRIEND = 1; |
||||
public static final int SCENE_MOMENT = 2; |
||||
public static final int SCENE_FAVORITE = 3; |
||||
|
||||
abstract static class ShareContent { |
||||
} |
||||
|
||||
public static class Url extends ShareContent { |
||||
|
||||
private int scene; |
||||
private String webpageUrl; |
||||
private String title; |
||||
private String description; |
||||
@Nullable |
||||
private byte[] thumbBmp; |
||||
|
||||
public void setScene(int scene) { |
||||
this.scene = scene; |
||||
} |
||||
|
||||
String getWebpageUrl() { |
||||
return webpageUrl; |
||||
} |
||||
|
||||
public void setWebpageUrl(String webpageUrl) { |
||||
this.webpageUrl = webpageUrl; |
||||
} |
||||
|
||||
String getTitle() { |
||||
return title; |
||||
} |
||||
|
||||
public void setTitle(String title) { |
||||
this.title = title; |
||||
} |
||||
|
||||
String getDescription() { |
||||
return description; |
||||
} |
||||
|
||||
public void setDescription(String description) { |
||||
this.description = description; |
||||
} |
||||
|
||||
@Nullable |
||||
byte[] getThumbBmp() { |
||||
return thumbBmp; |
||||
} |
||||
|
||||
public void setThumbBmp(@Nullable byte[] thumbBmp) { |
||||
this.thumbBmp = thumbBmp; |
||||
} |
||||
|
||||
private int getScene() { |
||||
return scene; |
||||
} |
||||
} |
||||
|
||||
static SendMessageToWX.Req buildReq(ShareContent shareContent) { |
||||
if (shareContent instanceof Url) { |
||||
return buildUrlReq((Url) shareContent); |
||||
} |
||||
throw new UnsupportedOperationException("不支持的分享内容"); |
||||
} |
||||
|
||||
private static SendMessageToWX.Req buildUrlReq(Url shareContent) { |
||||
//初始化一个WXWebpageObject,填写url
|
||||
WXWebpageObject webpage = new WXWebpageObject(); |
||||
webpage.webpageUrl = shareContent.getWebpageUrl(); |
||||
|
||||
//用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
|
||||
WXMediaMessage msg = new WXMediaMessage(webpage); |
||||
msg.title = shareContent.getTitle(); |
||||
msg.description = shareContent.getDescription(); |
||||
if (shareContent.getThumbBmp() != null) { |
||||
msg.thumbData = shareContent.getThumbBmp(); |
||||
} |
||||
|
||||
//构造一个Req
|
||||
SendMessageToWX.Req req = new SendMessageToWX.Req(); |
||||
req.message = msg; |
||||
req.scene = mapScene(shareContent.getScene()); |
||||
req.userOpenId = WeChatManager.getAppId(); |
||||
return req; |
||||
} |
||||
|
||||
private static int mapScene(int scene) { |
||||
if (scene == SCENE_FAVORITE) { |
||||
return SendMessageToWX.Req.WXSceneFavorite; |
||||
} |
||||
if (scene == SCENE_FRIEND) { |
||||
return SendMessageToWX.Req.WXSceneSession; |
||||
} |
||||
if (scene == SCENE_MOMENT) { |
||||
return SendMessageToWX.Req.WXSceneTimeline; |
||||
} |
||||
throw new UnsupportedOperationException("不支持的场景"); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue