1:先去ShareSDK平台注册账号,关键是获取AppKey,这玩意在你集成ShareSDK分享组件的时候会用得上
2:把ShareSDK分享组件集成到app中,详情参考http://wiki.mob.com/android-sharesdk%e5%ae%8c%e6%95%b4%e7%9a%84%e9%9b%86%e6%88%90%e6%96%87%e6%a1%a3/
3:上微信开发者平台搞到AppId和AppSecret,没有账号的请注册一个,如无意外都要交300大元的申请费用,详细教程http://bbs.mob.com/thread-95-1-4.html
微信开发者官网http://open.weixin.qq.com
4:ShareSDK做了一个九宫格的傻瓜式组件,我一般是不用了,为了迎合产品经理与设计师的审美需求,我更多的是用自定义api
这里我做了一下封装,方便使用,ps:调用ShareSDK的api需要初始化
public class ShareUtils {
private Context mContext;
private ShareUtils() {
}
private static class ShareUtilsFactory {
private static ShareUtils instance = new ShareUtils();
}
/* 获取实例 */
public static ShareUtils getInstance() {
return ShareUtilsFactory.instance;
}
public void init(Context ctx) {
mContext = ctx;
ShareSDK.initSDK(mContext);
}
/**
* 分享到微信好友
* @param bundle
*/
public void shareWechat(Bundle bundle) {
ShareParams sp = new ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE);// 设置成分享网页
String title, summary, thumb, url;
title=bundle.getString("title");
summary=bundle.getString("summary");
thumb=bundle.getString("thumb");
url=bundle.getString("url");
sp.setTitle(title);
sp.setText(summary);
sp.setShareType(Platform.SHARE_WEBPAGE);
sp.setImageUrl(thumb);
sp.setUrl(url);
Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
wechat.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onError(Platform arg0, int arg1, Throwable arg2) {
}
@Override
public void onComplete(Platform arg0, int arg1,
HashMap<String, Object> arg2) {
}
@Override
public void onCancel(Platform arg0, int arg1) {
}
});
wechat.share(sp);
}
/**
* 分享到微信朋友圈
* @param bundle
*/
public void shareWechatMoments(Bundle bundle) {
ShareParams sp = new ShareParams();
String title, summary, thumb, url;
title=bundle.getString("title");
summary=bundle.getString("summary");
thumb=bundle.getString("thumb");
url=bundle.getString("url");
sp.setTitle(title);
sp.setText(summary);
sp.setShareType(Platform.SHARE_WEBPAGE);
sp.setImageUrl(thumb);
sp.setUrl(url);
Platform wechatmoments = ShareSDK.getPlatform(WechatMoments.NAME);
wechatmoments.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onError(Platform arg0, int arg1, Throwable arg2) {
}
@Override
public void onComplete(Platform arg0, int arg1,
HashMap<String, Object> arg2) {
}
@Override
public void onCancel(Platform arg0, int arg1) {
}
});
wechatmoments.share(sp);
}
}
调用:
ShareUtils.getInstance().init(context);// 初始化分享工具
shareUtils = ShareUtils.getInstance();
Bundle bundle = new Bundle();
bundle.putString("title", "分享测试标题");
bundle.putString("summary", "测试摘要");
bundle.putString("thumb", "缩略图链接");
bundle.putString("url", "网页链接,请自行替换");
shareUtils.shareWechat(bundle);//分享到微信好友
shareUtils.shareWechatMoments(bundle);//分享到微信朋友圈
更多api说明
http://wiki.mob.com/%e5%88%86%e4%ba%ab%e5%88%b0%e6%8c%87%e5%ae%9a%e5%b9%b3%e5%8f%b0/
5:因为测试所用的应用签名是系统默认的,和正式上线时的签名不一样(自己生成的xxx.keystores),如果不同时修改微信官网登记的签名,就会导致正式发布的包分享失败,很简单,在导出正式包的时候可以查看所使用的应用签名,或者用微信提供的安卓签名查看工具,知道正式的签名,在微信官网上修改就可以了,无需等待官方审核。