Social
微信 微博 支付宝 QQ 【 登陆 、分享、支付 原生分享】 。独立library 调用方便 详看文档
github
本Library各包均单独使用其中common库必须依赖,
截止2020-05-31,
微博使用最新的9.12.0.aar 。
支付宝sdk aar 。QQ339/微信是最新6.6.4。
银联、QQ、京东 支付后续添加
主要提供服务:
支付: 微信支付,支付宝支付
登录: 支付宝授权,微信授权,QQ授权,微博授权
分享: QQ分享/QQ空间分享,微博分享/微博故事分享,微信分享/微信朋友圈分享 系统原生分享
集成方法1: 推荐
implementation 'com.mhy.social:common:1.2'//必须 下面按需
implementation 'com.mhy.social:ali:1.2'
implementation 'com.mhy.social:wx:1.2'
implementation 'com.mhy.social:wb:1.2'
implementation 'com.mhy.social:qq:1.2'
//没错 1.2就是懒人版,sdk均已内置 仅依赖本库即可
集成方法2:
//如果需要集成微博 、支付宝 根 添加如下
allprojects {
maven { url "https://dl.bintray.com/thelasterstar/maven/" }//微博仓库
flatDir {
dirs 'libs'//支付宝aar放在对应libs
}
}
//主库必须依赖
//无论使用本库任一library都必须依赖此library
//此库未依赖任何第三方,仅支持系统分享、支付宝个人首款码支付,跳转微信扫一扫页面
implementation 'com.mhy.social:common:1.2.2'
//以下按需依赖
//支付宝
implementation 'com.mhy.social:ali:1.2.2'
implementation (name: 'alipaySdk-15.7.6-20200521195109', ext: 'aar')
//QQ
implementation 'com.mhy.social:qq:1.2.2'
implementation files('libs/open_sdk_r8353806_lite.jar')
//微信
implementation 'com.mhy.social:wx:1.2.2'
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:6.6.4'
//微博 不支持x86
implementation 'com.mhy.social:wb:1.2.2'
implementation 'com.sina.weibo.sdk:core:9.12.0:openDefaultRelease@aar'
//
配置
<!-- 微信分享 -->
<activity
android:name=".wxapi.WXEntryActivity"
android:exported="true"/>
<!-- 微信支付 -->
<activity
android:name=".wxapi.WXPayEntryActivity"
android:exported="true"/>
<!-- qq登陆认证 -->
<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" />
<data android:scheme="tencent换成你的appid" />
</intent-filter>
</activity>
Application配置
//集成微信
在你包名下 新建wxapi包里面新建类 分享登陆WXEntryActivity 支付WXPayEntryActivity
两个类按需新建 都建无妨 只需按如下继承即可,不需要其他代码
public class WXEntryActivity extends BaseWXActivity {}
public class WXPayEntryActivity extends BaseWXPayEntryActivity{}
在你的Application里 onCreate()里 【记得别忘AndroidManifest.xml里添加】
WxSocial.setWeixinId("你的微信appid");
QqSocial.setAppId("你的QQ appid");
WbSocial.setWbApp("2045436852",//你的微博Key
"http://www.sina.com",//你对应的授权回调
"email,direct_messages_read,direct_messages_write,"+ "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
+ "follow_app_official_microblog," + "invitation_write");
//第三个参数是在微博开放平台为应用申请的高级权限
支付宝从后端返回拼接字符串》调用的时候传入
App配置
**先说接收回调数据*******************************
因微博和QQ需要onActivityresult()返回数据 因此他俩登陆 均使用 AuthApi和ShareApi获取实例后在你当前Activity/ Fragment 中
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//微博和QQ需要
if (api != null) {
api.onActivityResult(requestCode, resultCode, data);
api = null;
}
if (spi != null) {
spi.onActivityResult(requestCode, resultCode, data);
spi = null;
}
super.onActivityResult(requestCode, resultCode, data);
}
**然后 所有回调都从以下3个人接口对应出来 只需实现方法即可**
//登陆回调
private AuthApi.OnAuthListener onAuthListener = new AuthApi.OnAuthListener() {
@Override
public void onComplete(int type, Object user) {
//这里根据type使用不同对象
switch (type){
case SocialType.ALIPAY_Auth:
// ali AuthResult
break;
case SocialType.QQ_Auth:
// JSONObject data = (JSONObject) user;
// String openID = data.getString("openid");
// String accessToken = data.getString("access_token");
// String expires = data.getString("expires_in");
break;
case SocialType.WEIBO_Auth:
// wb(Oauth2AccessToken)user
break;
case SocialType.WEIXIN_Auth:
// wx((WeiXin)user).getCode()
break;
}
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(int type, String error) {
Toast.makeText(MainActivity.this, "登录失败:" + error, Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(int type) {
Toast.makeText(MainActivity.this, "登录取消", Toast.LENGTH_SHORT).show();
}
};
//支付回调
private PayApi.OnPayListener onPayListener = new PayApi.OnPayListener() {
@Override
public void onPayOk() {
Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onPayFail(String code, String msg) {
Toast.makeText(MainActivity.this, "支付失败:" + msg, Toast.LENGTH_SHORT).show();
}
};
//分享回调
private ShareApi.OnShareListener onShareListener = new ShareApi.OnShareListener() {
@Override
public void onShareOk(int type) {
Toast.makeText(MainActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onShareFail(int type, String msg) {
Toast.makeText(MainActivity.this, "分享失败:" + msg, Toast.LENGTH_SHORT).show();
}
};
```
**再说使用*******************************
//微信分享
findViewById(R.id.btn_share_wx).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WxShare mShareApi = new WxShare(MainActivity.this, ShareEntity.TYPE_WX, onShareListener);
mShareApi.doShare(createWXShareEntity(false));
v.startAnimation(shake);
}
});
//微信朋友圈分享
findViewById(R.id.btn_share_wx_circle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WxShare mShareApi = new WxShare(MainActivity.this, ShareEntity.TYPE_PYQ, onShareListener);
mShareApi.doShare(createWXShareEntity(true));
v.startAnimation(shake);
}
});
//微信登录
findViewById(R.id.btn_login_wx).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// WxAuth wxAuth=new WxAuth(MainActivity.this, onAuthListener);
// wxAuth.doAuth();
WxAuth authApi = new WxAuth(MainActivity.this, onAuthListener);
authApi.doAuth();
v.startAnimation(shake);
}
});
//qq登录
findViewById(R.id.btn_login_qq).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
QqAuth authApi = new QqAuth(MainActivity.this, onAuthListener);
authApi.doAuth(false);
v.startAnimation(shake);
api = authApi;//onActivityResult()内使用
}
});
//原生分享
findViewById(R.id.btn_share_local).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(shake);
ShareUtil shareUtil = new ShareUtil(MainActivity.this);
// shareUtil.shareFile(new File(getExternalFilesDir(null) + "/ccc.JPG"));
shareUtil.shareText("【flutter凉了吗?】知乎:… https://www.zhihu.com/question/374113031/answer/1253795562?utm_source=com.eg.android.alipaygphone&utm_medium=social&utm_oi=1020568397012209664 (分享自知乎网)");
}
});
//长按 打开小程序
findViewById(R.id.btn_mini_qq).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(shake);
QqAuth authApi = new QqAuth(MainActivity.this, onAuthListener);
authApi.doOpenMiniApp("1108108864", "pages/tabBar/index/index", "release");
}
});
//qq分享
findViewById(R.id.btn_share_qq).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
QqShare mShareApi = new QqShare(MainActivity.this, SocialType.QQ_Share, onShareListener);
mShareApi.doShare(QQShareEntity.createImageInfo(getExternalFilesDir(null) + "/aaa.png", "app"));
spi = mShareApi;
v.startAnimation(shake);
}
});
//qq空间分享
findViewById(R.id.btn_share_qq_zone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<String> imgUrls = new ArrayList<>();
imgUrls.add(getExternalFilesDir(null) + "/aaa.png");
imgUrls.add(getExternalFilesDir(null) + "/bbb.jpg");
QqShare mShareApi = new QqShare(MainActivity.this, SocialType.QQ_ZONE_Share, onShareListener);
mShareApi.doShare(QQShareEntity.createImageTextInfoToQZone("toptitle", "http://www.baidu.com", imgUrls, "summary", "我"));
spi = mShareApi;
v.startAnimation(shake);
}
});
//支付宝登陆
findViewById(R.id.btn_login_ali).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AliAuth authApi = new AliAuth(MainActivity.this, onAuthListener);
authApi.doAuth("");
v.startAnimation(shake);
}
});
//支付宝支付
findViewById(R.id.btn_pay_alipay).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AliPay authApi = new AliPay(MainActivity.this, onPayListener);
authApi.doPay(new AliPayContent(""));
v.startAnimation(shake);
}
});
//微信支付
findViewById(R.id.btn_pay_wx).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
JSONObject jsonObject = new JSONObject("source json data...");
//服务端获取
jsonObject = jsonObject.getJSONObject("pay_message");
WxPayContent req = new WxPayContent(
jsonObject.getString("appid"),
jsonObject.getString("partnerid"),
jsonObject.getString("prepayid"),
jsonObject.getString("packagestr"),
jsonObject.getString("noncestr"),
jsonObject.getString("timestamp"),
jsonObject.getString("sign"));
PayApi wxApi = new WxPay(MainActivity.this, onPayListener);
wxApi.doPay(req);
v.startAnimation(shake);
} catch (JSONException ignored) {
}
}
});
//小程序分享
findViewById(R.id.btn_mini_wx).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(shake);
// WxShare wxShare=new WxShare(MainActivity.this,SocialType.WEIXIN_Share,onShareListener);
// wxShare.doShare(WxShareEntity.createMiniApp( miniAppid, miniPath, webpageUrl, title, desc, imgUrl));
WxAuth wxAuth = new WxAuth(MainActivity.this, onAuthListener);
wxAuth.doOpenMiniApp("gh_d43f693ca31f", "", WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE);
}
});
//微博分享
findViewById(R.id.btn_share_weibo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WbShare wbShare = new WbShare(MainActivity.this, SocialType.WEIBO_Share, onShareListener);
wbShare.doShareStory(WbShareEntity.createImageStory(getExternalFilesDir(null) + "/aaa.png"));
spi = wbShare;
v.startAnimation(shake);
}
});
//微博登录
findViewById(R.id.btn_login_weibo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WbAuth wbAuth = new WbAuth(MainActivity.this, onAuthListener);
wbAuth.doAuth();
api = wbAuth;
v.startAnimation(shake);
}
});
关于分享 使用对应ShareEntity 的createxxx 方法传参调用对应分享内容包装
问题:集成方法2中ali库 已经在jcenter仓库里了,dependencies不下来不知道咋回事(难道我网络问题?)。有知道的大佬可以回复下谢谢。解决办法: 下载aar