添加qq sdk

androidx
Zhanty 5 years ago
parent dd20085bc4
commit bab5324815
  1. 28
      lib_social/README.md
  2. 6
      lib_social/build.gradle
  3. BIN
      lib_social/libs/open_sdk_r6137_lite.jar
  4. 67
      lib_social/src/main/java/com/android/sdk/social/qq/QQManager.java
  5. 54
      lib_social/src/main/java/com/android/sdk/social/qq/QQShareInfo.java
  6. 16
      lib_social/src/main/java/com/android/sdk/social/qq/ShareResultCallback.java
  7. 46
      lib_social/src/main/java/com/android/sdk/social/wechat/WeChatManager.java
  8. 115
      lib_social/src/main/java/com/android/sdk/social/wechat/WeChatShareInfo.java

@ -55,4 +55,30 @@ manifest 配置参考
## 支付宝支付 ## 支付宝支付
- sdk 版本:alipaySdk-15.6.2-20190416165100-noUtdid - sdk 版本:alipaySdk-15.6.2-20190416165100-noUtdid
## QQ
manifest 配置参考
```xml
<activity
android:name="com.tencent.tauth.AuthActivity"
android:launchMode="singleTask"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<!--1101491530 为你得 AppId-->
<data android:scheme="tencent1101491530"/>
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
```

@ -19,12 +19,6 @@ android {
} }
} }
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
lintOptions { lintOptions {
abortOnError false abortOnError false
} }

@ -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();
}

@ -14,6 +14,7 @@ import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseResp; import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram; import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.SendAuth; import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelpay.PayReq; import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI; import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
@ -31,6 +32,11 @@ public class WeChatManager {
private static String sAppId; private static String sAppId;
private static String sAppSecret; private static String sAppSecret;
/**
* @param context 上下文
* @param appId app id
* @param appSecret 密钥如果要进行微信登录则需要提供
*/
public static synchronized void initWeChatSDK(Context context, String appId, String appSecret) { public static synchronized void initWeChatSDK(Context context, String appId, String appSecret) {
if (sWeChatManager != null) { if (sWeChatManager != null) {
throw new UnsupportedOperationException("WeChatManager has already been initialized"); throw new UnsupportedOperationException("WeChatManager has already been initialized");
@ -90,13 +96,15 @@ public class WeChatManager {
} }
static void handleOnWxEntryResp(BaseResp baseResp) { static void handleOnWxEntryResp(BaseResp baseResp) {
Timber.d("baseResp.type = " + baseResp.getType()); Timber.d("handleOnWxEntryResp type = " + baseResp.getType() + "errStr = " + baseResp.errStr);
if (ConstantsAPI.COMMAND_SENDAUTH == baseResp.getType()) { if (ConstantsAPI.COMMAND_SENDAUTH == baseResp.getType()) {
handAuthResp(baseResp); handAuthResp(baseResp);
} else if (ConstantsAPI.COMMAND_PAY_BY_WX == baseResp.getType()) { } else if (ConstantsAPI.COMMAND_PAY_BY_WX == baseResp.getType()) {
handleOnWxEntryPayResp(baseResp); handleOnWxEntryPayResp(baseResp);
} else if (ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM == baseResp.getType()) { } else if (ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM == baseResp.getType()) {
handleMiniProgramResp(baseResp); handleMiniProgramResp(baseResp);
} else if (ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX == baseResp.getType()) {
handleSendMessageResp(baseResp);
} }
} }
@ -110,20 +118,22 @@ public class WeChatManager {
* @param userName 小程序原始id * @param userName 小程序原始id
* @param path 拉起小程序页面的可带参路径不填默认拉起小程序首页 * @param path 拉起小程序页面的可带参路径不填默认拉起小程序首页
*/ */
public void navToMinProgram(String userName, String path) { public void navToMinProgram(String userName, String path, boolean isRelease) {
WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req(); WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
req.userName = userName; req.userName = userName;
req.path = path; req.path = path;
// 可选打开开发版,体验版和正式版 // 可选打开开发版,体验版和正式版
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE; req.miniprogramType = isRelease ? WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE : WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;
mWxApi.sendReq(req); mWxApi.sendReq(req);
} }
private static void handleMiniProgramResp(BaseResp baseResp) { private static void handleMiniProgramResp(BaseResp baseResp) {
WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp; if (baseResp instanceof WXLaunchMiniProgram.Resp) {
//对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性 WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
String extMsg = launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
Timber.d("extMsg = " + extMsg); String extMsg = launchMiniProResp.extMsg;
Timber.d("handleMiniProgramResp = " + baseResp.errStr);
}
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -184,7 +194,6 @@ public class WeChatManager {
} }
@NonNull @NonNull
@SuppressWarnings("WeakerAccess")
public LiveData<Status<String>> authResult() { public LiveData<Status<String>> authResult() {
return mAuthCode; return mAuthCode;
} }
@ -288,4 +297,23 @@ public class WeChatManager {
} }
} }
} ///////////////////////////////////////////////////////////////////////////
// 支付
///////////////////////////////////////////////////////////////////////////
public boolean share(WeChatShareInfo.ShareContent content) {
try {
SendMessageToWX.Req baseReq = WeChatShareInfo.buildReq(content);
mWxApi.sendReq(baseReq);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static void handleSendMessageResp(BaseResp baseResp) {
// no op
}
}

@ -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…
Cancel
Save