在各类的App中,都存在分享功能,可以分享到QQ、微信、微博等,当然分享可以到具体平台进行对接,但是平台众多,如果每个平台都对接一次,不易于扩展,因此可以使用友盟或者ShareSDK
,尤其是ShareSDK,可以对接QQ、QQ空间、微信、朋友圈、微博等。
1、ShareSDK
首先去MobTech
平台注册登录后,进入开发者管理平台
选择创建一个应用,该应用自动生成App Key和App Secret
在选择SDK下载的时候,默认会给你选择几项分享平台。
在点击“保存配置”的时候,系统提示可以免下载SDK,可以配置Gradle文件即可。
2、Gradle配置
(1)根build.gradle
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// 注册MobSDK
classpath "com.mob.sdk:MobSDK:2018.0319.1724"
}
}
(2)app的build.gradle配置
在配置之前,需要添加mob的sdk插件,切记!!!!
apply plugin: 'com.mob.sdk'
因为是组件化的开发,在主App中,要需要声明这个插件,否则会报出以下错误:
Could not find any matches for com.mob:MobTools:+ as no versions of com.mob:MobTools are available.
后边需要添加各类分享平台的appKey
及appSecret
MobSDK {
appKey "创建应用时的appKey"
appSecret "创建应用时的appSecret"
ShareSDK {
loopShare true
devInfo {
SinaWeibo {
appKey "568898243"
appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
callbackUri "http://www.sharesdk.cn"
}
Wechat {
appId "wx4868b35061f87885"
appSecret "64020361b8ec4c99936c0e3999a9f249"
userName "gh_afb25ac019c9"
path "pages/index/index.html?id=1"
withShareTicket true
miniprogramType 2
}
QQ {
appId "100371282"
appKey "aed9b0303e3ed1e27bae87c33761161d"
}
Facebook {
appKey "1412473428822331"
appSecret "a42f4f3f867dc947b9ed6020c2e93558"
callbackUri "https://mob.com"
}
Twitter {
appKey "LRBM0H75rWrU9gNHvlEAA2aOy"
appSecret "gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"
callbackUri "http://mob.com"
}
ShortMessage {}
LinkedIn {
appKey "ejo5ibkye3vo"
appSecret "cC7B2jpxITqPLZ5M"
callbackUri "http://www.sharesdk.cn"
}
Douyin {
appKey "aw9ivykfjvi4hpwo"
appSecret "42b4caa6bda60bd49f05f06d0a4956e1"
}
FacebookMessenger {
appId "107704292745179"
}
WhatsApp {}
WechatMoments {
appId "wx4868b35061f87885"
appSecret "64020361b8ec4c99936c0e3999a9f249"
}
WechatFavorite {
appId "wx4868b35061f87885"
appSecret "64020361b8ec4c99936c0e3999a9f249"
}
QZone {
appId "100371282"
appKey "aed9b0303e3ed1e27bae87c33761161d"
}
}
}
}
配置完以上两步之后,就不需要再集成ShareSDK了。
3、ShareSDK的简单使用
在使用ShareSDK的时候,需要2类参数,一类是分享的数据信息,主要是ShareSDK中PlatForm的ShareParams
参数,可以将分享数据的标题、内容、图片及链接地址等显示在Dialog上;另一类参数是分享的平台,像QQ、微信、朋友圈等等。
public void sharedToQQ(){
//分享的参数数据
Platform.ShareParams sp = new Platform.ShareParams();
sp.setTitle("开学");
sp.setText("终于开学了");
//可以添加分享的图片信息
// sp.setImageUrl("imageUrl");
sp.setTitleUrl("http://sharesdk.cn");
//分享平台
Platform qq = ShareSDK.getPlatform(QQ.NAME);
qq.share(sp);
}
最终的分享结果就是以这样的形式展示的。
这只是简单的对特定平台的数据分享,如果是想要分享到其他平台,那么还需要再写一个对外的接口,这样不易于扩展,因此可以封装一个API,具备扩展性。
public class ShareSDKManager implements PlatformActionListener {
//当前的发布平台
private Platform mCurrentType;
//监听
private PlatformShareDataListener listener;
private ShareSDKManager(){}
private static ShareSDKManager manager;
public static ShareSDKManager getInstance(){
if(manager == null){
synchronized (ShareSDKManager.class){
if(manager == null){
manager = new ShareSDKManager();
}
}
}
return manager;
}
//初始化ShareSDK
public void init(Context context){
MobSDK.init(context);
}
//分享需要2类数据,参数和平台
public void shareData(ShareData data,PlatformShareDataListener listener){
this.listener = listener;
switch (data.type){
case QQ:
mCurrentType = ShareSDK.getPlatform(QQ.NAME);
break;
case QZONE:
mCurrentType = ShareSDK.getPlatform(QZone.NAME);
break;
case WECHAT:
mCurrentType = ShareSDK.getPlatform(Wechat.NAME);
break;
case WECHAT_MOMENT:
mCurrentType = ShareSDK.getPlatform(WechatMoments.NAME);
break;
}
//设置监听
mCurrentType.setPlatformActionListener(this);
//分享数据
mCurrentType.share(data.params);
}
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
if(listener != null){
listener.onComplete(i,hashMap);
}
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
if(listener != null){
listener.onError(i,throwable);
}
}
@Override
public void onCancel(Platform platform, int i) {
if(listener != null){
listener.onCancel(i);
}
}
//可分享的平台
public enum PlatFormType{
QQ,QZONE,WECHAT,WECHAT_MOMENT;
}
//状态回调
public interface PlatformShareDataListener{
void onComplete(int var2, HashMap<String, Object> var3);
void onError(int var2, Throwable var3);
void onCancel(int var2);
}
}
在使用的时候,只需要输入分享的数据和平台即可,这样在九宫格的使用过程中,可以根据分享的平台类型,灵活选择。
//分享到QQ
// SharedManager.getInstance().sharedToQQ();
ShareData shareData = new ShareData();
shareData.mParams = new Platform.ShareParams();
shareData.mParams.setTitle("title");
shareData.mParams.setText("content");
shareData.mParams.setTitleUrl("https://www.baidu.com");
shareData.mTypes = SharedManager.PlatFormType.QQ;
SharedManager.getInstance().shareData(shareData, new SharedManager.PlatFormShareListener() {
@Override
public void onComplete(int i, HashMap<String, Object> hashMap) {
Toast.makeText(MainActivity.this,"分享成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(int i, Throwable throwable) {
}
@Override
public void onCancel(int i) {
}
});
4、ShareSDK关于微信的分享
前面一小节,针对QQ分享这一环节,做了简单的介绍,当将平台切换到微信时,点击分享时,并没有反应。
这是因为微信的安全机制,需要审核,因此在gradle中添加以下代码,可以绕过审核,进入分享界面。
Wechat {
appId "wx4868b35061f87885"
appSecret "64020361b8ec4c99936c0e3999a9f249"
//将bypassApproval 设置为true,可绕过审核
bypassApproval true
userName "gh_afb25ac019c9"
path "pages/index/index.html?id=1"
withShareTicket true
miniprogramType 2
}
5、自定义Dialog九宫格
在自定义出Dialog九宫格之后,在应用层只需要将该Dialog实例化出来即可。
public class ShareDialog extends Dialog {
//屏幕
private DisplayMetrics dm;
private Context mContext;
private TextView tv_qq,tv_qzone,tv_wechat,wetchat_moment;
//分享的数据
private String title;
private String content;
private String textUrl;
public ShareDialog(@NonNull Context context) {
super(context);
mContext = context;
dm = context.getResources().getDisplayMetrics();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_share_dialog);
initView();
}
private void initView() {
Window window = getWindow();
window.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams layoutParams = window.getAttributes();
//设置宽度
layoutParams.width = dm.widthPixels;
//设置背景
window.setAttributes(layoutParams);
window.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.dialog_bg));
//设置单击事件
tv_qq = findViewById(R.id.tv_qq);
tv_qq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//分享到QQ
shareData(ShareSDKManager.PlatFormType.QQ);
}
});
tv_qzone = findViewById(R.id.tv_qzone);
tv_qzone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//分享到QQ空间
shareData(ShareSDKManager.PlatFormType.QZONE);
}
});
tv_wechat = findViewById(R.id.tv_wechat);
tv_wechat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//分享到微信
shareData(ShareSDKManager.PlatFormType.WECHAT);
}
});
wetchat_moment = findViewById(R.id.wetchat_moment);
wetchat_moment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//分享到朋友圈
shareData(ShareSDKManager.PlatFormType.WECHAT_MOMENT);
}
});
}
//设置数据
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTextUrl() {
return textUrl;
}
public void setTextUrl(String textUrl) {
this.textUrl = textUrl;
}
private ShareSDKManager.PlatformShareDataListener listener = new ShareSDKManager.PlatformShareDataListener() {
@Override
public void onComplete(int var2, HashMap<String, Object> var3) {
}
@Override
public void onError(int var2, Throwable var3) {
}
@Override
public void onCancel(int var2) {
}
};
private void shareData(ShareSDKManager.PlatFormType type) {
Platform.ShareParams shareParams = new Platform.ShareParams();
shareParams.setTitle(title);
shareParams.setText(content);
shareParams.setTitleUrl(textUrl);
ShareData shareData = new ShareData(shareParams,type);
//分享
ShareSDKManager.getInstance().shareData(shareData,listener);
}
}