Android应用中怎么接入微信分享功能
今天就跟大家聊聊有关Android应用中怎么接入微信分享功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
成都创新互联专注于企业网络营销推广、网站重做改版、门源网站定制设计、自适应品牌网站建设、html5、商城开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为门源等各大城市提供网站开发制作服务。
注册应用信息并下载必要工具
在微信开放平台注册应用信息
下载 签名生成工具获取待接入应用的签名
应用信息填入签名
注意:由于一般调试签名和正式签名不一致,测试时可填入测试包的签名,上线时需要改为正式包的签名
接入
在build.gradle中,添加如下依赖:
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' }
或:
dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' }
其中,前者包含统计功能
AndroidManifest.xml中添加权限和微信回调Activity
这里使用了一个透明的Activity来处理微信分享结果回调
主题:
Activity
当微信分享成功,点击返回App时,微信会主动调用WXEntryActivity,并将结果通过Intent传给WXEntryActivity,此时调用api.handleIntent(getIntent(), this)对结果进行处理,handleIntent第二个参数为IWXAPIEventHandler,分享结果会在onResp方法中处理,在这里我们发送一个广播,并在WXShare对象中对该广播进行监听
public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler { private IWXAPI api; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Logger.i("WXEntryActivity"); WXShare share = new WXShare(this); api = share // .register() .getApi(); //注意: //第三方开发者如果使用透明界面来实现WXEntryActivity,需要判断handleIntent的返回值,如果返回值为false,则说明入参不合法未被SDK处理,应finish当前透明界面,避免外部通过传递非法参数的Intent导致停留在透明界面,引起用户的疑惑 try { if (!api.handleIntent(getIntent(), this)) { finish(); } } catch (Exception e) { e.printStackTrace(); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Logger.i("onNewIntent"); setIntent(intent); if (!api.handleIntent(intent, this)) { finish(); } } @Override public void onReq(BaseReq baseReq) { } @Override public void onResp(BaseResp baseResp) { Intent intent = new Intent(WXShare.ACTION_SHARE_RESPONSE); intent.putExtra(WXShare.EXTRA_RESULT, new WXShare.Response(baseResp)); sendBroadcast(intent); finish(); } }
工具类
public class WXShare { public static final String APP_ID = "wx0123456789"; public static final String ACTION_SHARE_RESPONSE = "action_wx_share_response"; public static final String EXTRA_RESULT = "result"; private final Context context; private final IWXAPI api; private OnResponseListener listener; private ResponseReceiver receiver; public WXShare(Context context) { api = WXAPIFactory.createWXAPI(context, APP_ID); this.context = context; } public WXShare register() { // 微信分享 api.registerApp(APP_ID); receiver = new ResponseReceiver(); IntentFilter filter = new IntentFilter(ACTION_SHARE_RESPONSE); context.registerReceiver(receiver, filter); return this; } public void unregister() { try { api.unregisterApp(); context.unregisterReceiver(receiver); } catch (Exception e) { e.printStackTrace(); } } public WXShare share(String text) { WXTextObject textObj = new WXTextObject(); textObj.text = text; WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; // msg.title = "Will be ignored"; msg.description = text; SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("text"); req.message = msg; req.scene = SendMessageToWX.Req.WXSceneSession; boolean result = api.sendReq(req); Logger.i("text shared: " + result); return this; } public IWXAPI getApi() { return api; } public void setListener(OnResponseListener listener) { this.listener = listener; } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); } private class ResponseReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Response response = intent.getParcelableExtra(EXTRA_RESULT); Logger.d("type: " + response.getType()); Logger.d("errCode: " + response.errCode); String result; if (listener != null) { if (response.errCode == BaseResp.ErrCode.ERR_OK) { listener.onSuccess(); } else if (response.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) { listener.onCancel(); } else { switch (response.errCode) { case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "发送被拒绝"; break; case BaseResp.ErrCode.ERR_UNSUPPORT: result = "不支持错误"; break; default: result = "发送返回"; break; } listener.onFail(result); } } } } public static class Response extends BaseResp implements Parcelable { public int errCode; public String errStr; public String transaction; public String openId; private int type; private boolean checkResult; public Response(BaseResp baseResp) { errCode = baseResp.errCode; errStr = baseResp.errStr; transaction = baseResp.transaction; openId = baseResp.openId; type = baseResp.getType(); checkResult = baseResp.checkArgs(); } @Override public int getType() { return type; } @Override public boolean checkArgs() { return checkResult; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.errCode); dest.writeString(this.errStr); dest.writeString(this.transaction); dest.writeString(this.openId); dest.writeInt(this.type); dest.writeByte(this.checkResult ? (byte) 1 : (byte) 0); } protected Response(Parcel in) { this.errCode = in.readInt(); this.errStr = in.readString(); this.transaction = in.readString(); this.openId = in.readString(); this.type = in.readInt(); this.checkResult = in.readByte() != 0; } public static final CreatorCREATOR = new Creator () { @Override public Response createFromParcel(Parcel source) { return new Response(source); } @Override public Response[] newArray(int size) { return new Response[size]; } }; } }
接口
public interface OnResponseListener { void onSuccess(); void onCancel(); void onFail(String message); }
使用
在需要使用微信分享的Activity中:
@Override protected void onCreate(Bundle savedInstanceState) { wxShare = new WXShare(this); wxShare.setListener(new OnResponseListener() { @Override public void onSuccess() { // 分享成功 } @Override public void onCancel() { // 分享取消 } @Override public void onFail(String message) { // 分享失败 } }); } @Override protected void onStart() { super.onStart(); wxShare.register(); } @Override protected void onDestroy() { wxShare.unregister(); super.onDestroy(); }
启动分享
wxShare.share("这是要分享的文字");
到这里微信分享就完成啦!
另外,在微信官方例程中,有个定时刷新应用注册信息的receiver
AndroidManifest.xml中添加:
代码:
public class AppRegister extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final IWXAPI api = WXAPIFactory.createWXAPI(context, null); // 将该app注册到微信 api.registerApp(WXShare.APP_ID); } }
看完上述内容,你们对Android应用中怎么接入微信分享功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
文章标题:Android应用中怎么接入微信分享功能
文章路径:http://azwzsj.com/article/pphchc.html