WeChat Share SDK
1.注册开发者帐号
2.创建应用
创建应用 申请APP_ID
下载签名工具 应用信息资料里填些签名。
签名工具是一个apk,下载后安装到手机,根据提示填写包名生成签名。
3.接入SDK
- module下build.gradle添加:
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:1.0.2'
}
或
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:1.0.2'
}
(其中前者包含统计功能)
-
包名目录下建包wxapi,并在该包下新增WXEntryActivity类继承Actvity,并实现 接口用以接收分享回调信息。
-
Android.xml中添加
<activity
android:name=".wxapi.WXEntryActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
- 代码实现
类WXEntryActivity.java
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
public static final String APP_ID = "待应用审核通过填入生成的APP_ID";
private IWXAPI iwxapi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wxentry);
iwxapi = WXAPIFactory.createWXAPI(this, APP_ID, true);
iwxapi.handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq baseReq) {
Log.e("yc", "onReq:"+baseReq.toString());
}
@Override
public void onResp(BaseResp baseResp) {
Log.e("yc", "onResp:errCode:"+baseResp.errCode+" type:"+baseResp.getType()+" errStr:"+baseResp.errStr);
Bundle bundle = new Bundle();
switch (baseResp.errCode){
case BaseResp.ErrCode.ERR_OK:
if(baseResp.getType() == 1) {
//登录授权成功
Message msg = Message.obtain();
msg.obj = (SendAuth.Resp)baseResp;
WeChatLoginBiz.handler.sendMessage(msg);
}else if(baseResp.getType() == 2) {
//分享成功
Toast.makeText(App.getContext(), "分享成功", Toast.LENGTH_SHORT).show();
}
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
if(baseResp.getType() == 1){
//登录用户未授权
}else if(baseResp.getType() == 2) {
//分享取消
Toast.makeText(App.getContext(), "取消了分享", Toast.LENGTH_SHORT).show();
}
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
//拒绝
break;
}
finish();
}
}
业务处理类WeChatShareBiz.java
public class WeChatShareBiz {
private IWXAPI iwxapi;
public WeChatShareBiz(Context context){
iwxapi = WXAPIFactory.createWXAPI(context, WXEntryActivity.APP_ID);
iwxapi.registerApp(WXEntryActivity.APP_ID);
}
//分享给朋友
public void shareToFriend(){
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = "http://www.baidu.com";
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = "Title";
msg.description = "Content";
BitmapDrawable bmpDraw = (BitmapDrawable) App.getContext().getResources().getDrawable(
R.mipmap.ic_launcher);
Bitmap thumb = bmpDraw.getBitmap();
msg.setThumbImage(thumb);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
iwxapi.sendReq(req);
}
//分享到朋友圈
public void shareToCircle(){
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = "http://www.baidu.com";
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = "Title";
msg.description = "Content";
BitmapDrawable bmpDraw = (BitmapDrawable) App.getContext().getResources().getDrawable(
R.mipmap.ic_launcher);
Bitmap thumb = bmpDraw.getBitmap();
msg.setThumbImage(thumb);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneTimeline;
iwxapi.sendReq(req);
}
}
注意:请求scene的区别,分享给朋友:SendMessageToWX.Req.WXSceneSession,分享到朋友圈:SendMessageToWX.Req.WXSceneTimeline
- 混淆
-keep class com.tencent.mm.opensdk.** {
*;
}
-keep class com.tencent.wxop.** {
*;
}
-keep class com.tencent.mm.sdk.** {
*;
}