分享功能在app开发中算是一个常见功能,使用友盟的一键分享使用起来非常方便。但是实际项目可能会根据需求使用自定义界面的分享。在此做一个记录分享,也把遇到的问题整理出来。
前面的集成过程就不赘述了,根据官方文档一步步来就可以搞定。编写思路就是将功能封装在一个dialogfragment里面。在需要的地方直接调用就可以了。直接是上代码,大部分都有注释。效果如下图,界面大家可以随意编写,主要是介绍分享方法。
public class ShareFrag extends DialogFragment {
PlatformActionListener platformActionListener = new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, HashMap hashMap) {
ShareFrag.this.getDialog().dismiss();
//大部分的回调方法都处于网络线程,因此可以简单默认为回调方法都不在主线程.
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "分享成功!",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.e("TAG", throwable.getMessage());
}
@Override
public void onCancel(Platform platform, int i) {
}
};
private View rootView;
private ShareWebBean webBean;
private String url;
private View.OnClickListener SHARE_LISTENER = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_share_wechat: {
Wechat.ShareParams sp = new Wechat.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE);//非常重要:一定要设置分享属性
sp.setTitle(webBean.getCfg().getShareTitle()); //分享标题
sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle()); //分享文本
sp.setImageUrl(webBean.getCfg().getShareIcon());
sp.setUrl(url); //网友点进链接后,可以看到分享的详情
Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
wechat.setPlatformActionListener(platformActionListener); // 设置分享事件回调
// 执行分享
wechat.share(sp);
break;
}
case R.id.layout_share_moments: {
WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
sp.setTitle(webBean.getCfg().getShareTitle()); //分享标题
sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle()); //分享文本
sp.setUrl(url); //网友点进链接后,可以看到分享的详情
sp.setImageUrl(webBean.getCfg().getShareIcon());
//
//3、非常重要:获取平台对象
Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
wechatMoments.setPlatformActionListener(platformActionListener); // 设置分享事件回调
// 执行分享
wechatMoments.share(sp);
break;
}
case R.id.layout_share_weibo: {
SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
sp.setText(webBean.getCfg().getShareContent() + url); //分享文本
sp.setTitle(webBean.getCfg().getShareTitle());
// sp.setImagePath(img);//本地图片rul
// sp.setImageUrl(imagePath);
//3、非常重要:获取平台对象
Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
sinaWeibo.setPlatformActionListener(platformActionListener); // 设置分享事件回调
sinaWeibo.SSOSetting(false);//false是使用客户端授权
// 执行分享
sinaWeibo.share(sp);
break;
}
case R.id.layout_share_qq: {
QQ.ShareParams sp = new QQ.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
sp.setTitle(webBean.getCfg().getShareTitle());
sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle());
sp.setImageUrl(webBean.getCfg().getShareIcon());
sp.setTitleUrl(url); //网友点进链接后,可以看到分享的详情
//3、非常重要:获取平台对象
Platform qq = ShareSDK.getPlatform(QQ.NAME);
qq.setPlatformActionListener(platformActionListener); // 设置分享事件回调
// 执行分享
qq.share(sp);
break;
}
case R.id.layout_share_zone: {
QZone.ShareParams sp = new QZone.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
sp.setTitle(webBean.getCfg().getShareTitle());
sp.setText(webBean.getCfg().getShareContent()+webBean.getCfg().getShareTitle());
// sp.setImagePath(urlWithHttp);
sp.setImageUrl(webBean.getCfg().getShareIcon());
sp.setTitleUrl(url);
Platform qzone = ShareSDK.getPlatform(QZone.NAME);
qzone.setPlatformActionListener(platformActionListener); // 设置分享事件回调
// 执行图文分享
qzone.share(sp);
break;
}
case R.id.layout_share_collect: {
WechatFavorite.ShareParams sp = new WechatFavorite.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE); //非常重要:一定要设置分享属性
sp.setTitle(webBean.getCfg().getShareTitle());
sp.setText(webBean.getCfg().getShareContent());
// sp.setImagePath(imagePath);
sp.setImageUrl(webBean.getCfg().getShareIcon());
sp.setUrl(url);
Platform wechatFavorite = ShareSDK.getPlatform(WechatFavorite.NAME);
wechatFavorite.setPlatformActionListener(platformActionListener);
wechatFavorite.share(sp);
break;
}
}
}
};
public ShareFrag() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (rootView == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
rootView = inflater.inflate(R.layout.fragment_share_dqg, null, false);
RelativeLayout cancel = (RelativeLayout) rootView.findViewById(R.id.fragment_share_cancel);
RelativeLayout layoutShareWechat = (RelativeLayout) rootView.findViewById(R.id.layout_share_wechat);
RelativeLayout layoutShareQq = (RelativeLayout) rootView.findViewById(R.id.layout_share_qq);
RelativeLayout layoutShareWeibo = (RelativeLayout) rootView.findViewById(R.id.layout_share_weibo);
RelativeLayout layoutShareZone = (RelativeLayout) rootView.findViewById(R.id.layout_share_zone);
RelativeLayout layoutShareMoments = (RelativeLayout)rootView.findViewById(R.id.layout_share_moments);
RelativeLayout layoutShareCollect = (RelativeLayout)rootView.findViewById(R.id.layout_share_collect);
layoutShareQq.setOnClickListener(SHARE_LISTENER);
layoutShareWechat.setOnClickListener(SHARE_LISTENER);
layoutShareWeibo.setOnClickListener(SHARE_LISTENER);
layoutShareZone.setOnClickListener(SHARE_LISTENER);
layoutShareMoments.setOnClickListener(SHARE_LISTENER);
layoutShareCollect.setOnClickListener(SHARE_LISTENER);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
ShareSDK.initSDK(activity);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
Window window = dialog.getWindow();
dialog.setCanceledOnTouchOutside(true);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
window.setLayout(dm.widthPixels, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.BottomDialogStyle);
//设置背景透明,不设置dialog可能会出现黑边
window.setBackgroundDrawable(ContextCompat.getDrawable(getActivity(), R.color.transparent));
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
((ViewGroup) (rootView.getParent())).removeView(rootView);
}
//设置数据,这个方法参数根据实际需要修改
public void setData(ShareWebBean shareWebBean,String uid) {
this.webBean = shareWebBean;
// myApplication = (qiantuhaoApplication) getActivity().getApplication();
url = shareWebBean.getCfg().getShareUrl() + "?tjr=" + uid;
}
}
FragmentManager fm = getSupportFragmentManager();
ShareFrag shareFrag = new ShareFrag();
shareFrag.show(fm, null);
shareFrag.setData(webBean,myApplication.getUid());//设置分享内容,这个方法可以根据各自需求传入不同数据进行处理。
ShareByAppClient="true"
怎样直接调用QQ空间客户端进行分享官方文档里面好像没有说明。有知道的希望可以告知一下共同学习。BypassApproval="true"
BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过审核的应用也可以执行分享,但是仅限于分享文字和图片,不能分享其他类型,默认值为false。此外,微信收藏不支持此字段。