一.微信登陆分三个步骤:
1).微信授权登陆
2).根据授权登陆code 获取该用户token
3).根据token获取用户资料
4).接收微信的请求及返回值 如果你的程序需要接收微信发送的请求,或者接收发送到微信请求的响应结果,需要下面3步操作:
a. 在你的包名相应目录下新建一个wxapi目录,并在该wxapi目录下新增一个WXEntryActivity类,该类继承自Activity(例如应用程序的包名为net.sourceforge.simcpux
权限:加上exported属性,设置为true:
b. 实现IWXAPIEventHandler接口,微信发送的请求将回调到onReq方法,发送到微信请求的响应结果将回调到onResp方法
c. 在WXEntryActivity中将接收到的intent及实现了IWXAPIEventHandler接口的对象传递给IWXAPI接口的handleIntent方法,示例如下图:
微信官网登陆教程:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317851&token=&lang=zh_CN
微信官网接入指南:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808&token=&lang=zh_CN
二.微信分享直接调用sdk就行,回调跟登陆回调的类是一样的,根据BaseResp的类型来区分是登陆还是分享。
三.微信支付
1).发送一个支付请求
2).接收微信支付的返回值(跟接收微信登陆.分享的返回值类似,我就不写详细操作步骤了)
官网参考地址:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5
四.贴上代码进行讲解
我把微信登陆,分享,支付都封装到了一个类里面了,你们可以参考这个类.封装了6个方法,我对几个需要的方法介绍一下
1).构造方法:初始化对象的时候,顺便初始化微信对象,把app_id注册到微信
2).login() 发起一个登陆的请求 在微信登陆监听Actviity中获取code
3).getAccessToken(String code) 当你从监听Activity中获取了code之后就可以通过这个方法获取微信访问token了
4).getWeiXinUserInfo(final WeiXinToken obj) 获取微信用户信息 传入一个WeiXinToken对象,这个对象是第三步的返回值
5).share(final boolean friendsCircle,final VideoB videoB) 分享给朋友或者朋友圈 如果你有分享图片,图片过大的时候一定要经过压缩,微信官网说明图片不能大
于32kb
6).isWXAppInstalled() 检查微信是否安装
7).wxPay(final BaseActivity activity,String order_id,String payType) 微信支付 我们项目微信支付的一些参数保存在服务器上,所以我这边还请求了自己的
服务器,如果你们是放在本地,直接copy回调函数里面的代码即可.在微信支付监听Actviity中获取支付的状态码
PayReq类属性对应含义请参考微信官方文档:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_12
- public class WeiXinPresenter extends Presenter{
- public static final int IMAGE_SIZE=32768;
- public static final String APP_ID = "";
- public static final String SECRET="";
-
- private IWXAPI wxAPI;
- private IView iView;
- private IUserController userController;
-
- @Override
- public IView getIView() {
- return iView;
- }
-
- public WeiXinPresenter(Context context){
- if(context!=null && context instanceof IView)
- iView =(IView) context;
- if(wxAPI==null){
- wxAPI = WXAPIFactory.createWXAPI(context,APP_ID,true);
- wxAPI.registerApp(APP_ID);
- }
- if(null==userController)
- userController=ControllerFactory.getUserController();
- }
-
-
- public void login(){
- SendAuth.Req req = new SendAuth.Req();
- req.scope = "snsapi_userinfo";
- req.state = String.valueOf(System.currentTimeMillis());
- wxAPI.sendReq(req);
- }
-
-
- public void getAccessToken(String code){
- if(!userController.isLogin()){
- userController.getWeiXinAccessToken(APP_ID,SECRET,code,new RequestDataCallback<WeiXinToken>(){
- @Override
- public void dataCallback(WeiXinToken obj){
- if(obj!=null){
- if(obj.getErrcode()==0){
- if(MLog.debug)
- iView.showToast("授权用户唯一标识:"+obj.getOpenid());
- getWeiXinUserInfo(obj);
- }else{
- iView.showToast(obj.getErrmsg());
- }
- }else{
-
- }
- }
- });
- }else{
-
- }
- }
-
-
- private void getWeiXinUserInfo(final WeiXinToken obj){
- userController.getWeiXinUserInfo(obj.getAccess_token(), obj.getOpenid(), new RequestDataCallback<RegisterB>() {
- @Override
- public void dataCallback(RegisterB registerB){
- registerB.setAccess_token(obj.getAccess_token());
- registerB.setToken_expire_at(obj.getExpires_in());
- if(registerB.getErrcode()==0){
- registerB.setThird_type_name(Constants.WEI_XIN);
- thirdLogin(registerB);
- }else{
- iView.showToast(registerB.getErrmsg());
- }
- }
- });
- }
-
-
- private void thirdLogin(RegisterB registerB){
- userController.thirdAuth(registerB,new RequestDataCallback<UserP>(){
- @Override
- public void dataCallback(UserP user){
- if(checkCallbackData(user, true)){
- if(user.getError()==user.ErrorNone){
- iView.showToast(R.string.login_success);
- getAppController().sendLoginChangeIntent();
- userController.saveLoginUser(user,FileUtil.getFilePath());
- ((ILoginView)iView).toMain();
- }else{
- iView.showToast(user.getError_reason());
- }
- }
- }
- });
- }
-
-
- public void share(final boolean friendsCircle,final VideoB videoB){
- new LoadPicThread(videoB.getCover_url(),new Handler(){
- @Override
- public void handleMessage(Message msg) {
- byte[] bytes=(byte[]) msg.obj;
- if(bytes.length>IMAGE_SIZE){
- iView.showToast(R.string.image_no_big);
- return;
- }
- System.out.println("图片长度:"+bytes.length);
- WXVideoObject videoObject = new WXVideoObject();
- videoObject.videoUrl = videoB.getShare_url() + Constants.WEI_XIN + "&share_from="+com.kaka.utils.Constants.ANDROID;
- WXMediaMessage wxMessage = new WXMediaMessage(videoObject);
- wxMessage.title = videoB.getContent();
- wxMessage.thumbData = bytes;
- SendMessageToWX.Req req = new SendMessageToWX.Req();
-
- req.transaction = String.valueOf(videoB.getId() + System.currentTimeMillis());
- req.message = wxMessage;
- req.scene = friendsCircle ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
- wxAPI.sendReq(req);
- }
- }).start();
- }
-
- private class LoadPicThread extends Thread{
- private String url;
- private Handler handler;
- public LoadPicThread(String url,Handler handler){
- this.url=url;
- this.handler=handler;
- }
-
- @Override
- public void run(){
- try {
- URL picurl = new URL(url);
- HttpURLConnection conn = (HttpURLConnection)picurl.openConnection();
- conn.setConnectTimeout(6000);
- conn.setDoInput(true);
- conn.setUseCaches(false);
- conn.connect();
- Bitmap bmp=BitmapFactory.decodeStream(conn.getInputStream());
-
- ByteArrayOutputStream output = new ByteArrayOutputStream();
-
- bmp.compress(Bitmap.CompressFormat.JPEG, 100, output);
- int options = 100;
- while (output.toByteArray().length > IMAGE_SIZE && options != 10) {
- output.reset();
- bmp.compress(Bitmap.CompressFormat.JPEG, options, output);
- options -= 10;
- }
-
- bmp.recycle();
- byte[] result = output.toByteArray();
- output.close();
-
- Message message=handler.obtainMessage();
- message.obj=result;
- message.sendToTarget();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
-
- public boolean isWXAppInstalled(){
- return wxAPI.isWXAppInstalled();
- }
-
-
- public void wxPay(final BaseActivity activity,String order_id,String payType){
- activity.showProgress("");
- ControllerFactory.getWalletsController().getPayments(order_id, payType, new RequestDataCallback<PaymentsP>() {
- @Override
- public void dataCallback(PaymentsP obj) {
- if(checkCallbackData(obj, true)){
- if(obj.getError()==obj.ErrorNone){
- PayReq req = new PayReq();
-
- req.appId = obj.getAppid();
- req.nonceStr=obj.getNoncestr();
- req.packageValue=obj.getPackage_value();
- req.sign=obj.getSign();
- req.partnerId=obj.getPartnerid();
- req.prepayId=obj.getPrepayid();
- req.timeStamp=obj.getTimestamp();
-
- wxAPI.registerApp(obj.getAppid());
- wxAPI.sendReq(req);
-
- MLog.i("ansen", "开始进行微信支付..");
- iView.showToast("开始进行微信支付..");
- }
- }else{
- iView.showToast(obj.getError_reason());
- }
- activity.hideProgress();
- }
- });
- }
- }
微信登陆以及分享 请求跟返回值的接收 我这边登陆.分享的状态都是发送广播出去,然后结束当前的Activity.
- public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
- private IWXAPI wxAPI;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- if(MLog.debug)
- System.out.println("WXEntryActivity onCreate");
-
- wxAPI = WXAPIFactory.createWXAPI(this,WeiXinPresenter.APP_ID,true);
- wxAPI.registerApp(WeiXinPresenter.APP_ID);
-
- wxAPI.handleIntent(getIntent(), this);
- }
-
- @Override
- protected void onNewIntent(Intent intent){
- super.onNewIntent(intent);
- wxAPI.handleIntent(getIntent(),this);
- if(MLog.debug)
- System.out.println("WXEntryActivity onNewIntent");
- }
-
- @Override
- public void onReq(BaseReq arg0) {
- if(MLog.debug)
- System.out.println("WXEntryActivity onReq:"+arg0);
- if(MLog.debug)
- Toast.makeText(this, "onReq 方法运行", 0).show();
- }
-
- @Override
- public void onResp(BaseResp resp){
- MLog.d("ansen", "onResp.....");
- if(MLog.debug)
- Toast.makeText(this,"onResp 方法运行", 0).show();
- if(resp.getType()==ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX){
- switch (resp.errCode){
- case BaseResp.ErrCode.ERR_OK:
- if(MLog.debug)
- Toast.makeText(WXEntryActivity.this, "分享成功!", Toast.LENGTH_SHORT).show();
- break;
- case BaseResp.ErrCode.ERR_USER_CANCEL:
- break;
- case BaseResp.ErrCode.ERR_AUTH_DENIED:
-
- break;
- }
- Intent intent = new Intent();
- intent.setAction(APIDefineConst.BROADCAST_ACTION_WEIXIN_SHARE);
- LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
- lbm.sendBroadcast(intent);
- }else if(resp.getType()==ConstantsAPI.COMMAND_SENDAUTH){
- SendAuth.Resp authResp = (Resp) resp;
- String code = authResp.code;
- Intent intent = new Intent();
- intent.setAction(APIDefineConst.BROADCAST_ACTION_WEIXIN_TOKEN);
- intent.putExtra("errCode", authResp.errCode);
- if (authResp.errCode == BaseResp.ErrCode.ERR_OK){
- intent.putExtra("code", code);
- }
-
- if(MLog.debug)
- Toast.makeText(this, "WXEntryActivity 发送登陆广播!!!!", 0).show();
- if (android.os.Build.VERSION.SDK_INT >= 12) {
- intent.setFlags(32);
- }
- LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
- lbm.sendBroadcast(intent);
- }
- finish();
- }
- }
微信支付 请求跟返回值的接收 微信支付也是发送广播,如果你们还有需求判断支付成功或者失败,可以在广播的intent中进行传参
- public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
- private IWXAPI wxAPI;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- wxAPI = WXAPIFactory.createWXAPI(this, WeiXinPresenter.APP_ID);
- wxAPI.handleIntent(getIntent(), this);
- }
-
- @Override
- protected void onNewIntent(Intent intent){
- super.onNewIntent(intent);
- setIntent(intent);
- wxAPI.handleIntent(intent, this);
- }
-
- @Override
- public void onReq(BaseReq arg0) {
- }
-
- @Override
- public void onResp(BaseResp resp) {
- MLog.i("微信支付回调..", "ansen onResp");
- if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX){
- if(resp.errCode==BaseResp.ErrCode.ERR_OK){
- Intent intent = new Intent();
- intent.setAction(APIDefineConst.BROADCAST_ACTION_WEIXIN_PAY);
- LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
- lbm.sendBroadcast(intent);
-
- }else{
- }
- }
- finish();
- }
强调一点,一定要注意 接收微信的请求及返回值 的包名跟类名,包名是应用程序的包名+".wxapi" 类名必须是微信指定的类名 并且这两个Activity一定要在AndroidManifest.xml中注册,上传一张是我做的app中包名跟类名的截图
如何在activity中调用微信登陆
1).登陆广播监听内部类 如果接收到了广播就去获取微信token
- private class WXEntryReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent){
- MLog.i("WXEntryReceiver", "接收微信登陆广播");
- if(MLog.debug)
- showToast("接收微信登陆广播");
- if(intent.getAction().equals(APIDefineConst.BROADCAST_ACTION_WEIXIN_TOKEN)){
- int errCode = intent.getExtras().getInt("errCode");
- if(MLog.debug)
- System.out.println("获取错误码:"+errCode);
- if(errCode==BaseResp.ErrCode.ERR_USER_CANCEL||errCode==BaseResp.ErrCode.ERR_AUTH_DENIED){
- requestDataFinish();
- }else{
- String code = intent.getExtras().getString("code");
- xinTestPresenter.getAccessToken(code);
- }
- }
- }
- }
2).定义成员变量
- private WXEntryReceiver wxEntryReceiver=null;
3).在oncreate中注册广播
- wxEntryReceiver= new WXEntryReceiver();
- LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
- IntentFilter filter = new IntentFilter();
- filter.addAction(APIDefineConst.BROADCAST_ACTION_WEIXIN_TOKEN);
- lbm.registerReceiver(wxEntryReceiver,filter);
4).调用微信登陆
- WeiXinPresenter xinTestPresenter=new WeiXinPresenter(this);
- xinTestPresenter.login();
在Activity中调用微信分享跟调用微信支付的代码我就不贴出来了,我这篇博客只是给大家一个参考的地方,遇到问题还是建议第一时间看官方文档.
说说我在做微信登陆碰到的问题
1.微信登陆、分享、支付 回调的activity 包名跟类名一定要严格按照要求去写
2.接收回调的是activity 一定要在AndroidManifest.xml中注册
3.WeiXinPresenter中有两个常量 APP_ID跟SECRET 要去微信申请的时候才有的.你们copy代码的时候要给这两个常量赋值
4.可能访问网络神马的还需要一些权限 记得在AndroidManifest.xml添加权限
5.调用微信的登陆、分享、支付 你的安装包一定要有签名,签名信息一定要跟你在微信官网上申请时签名信息一致
6.微信没有客服支持。。。。。如果出了问题看官方demo 或者 官方API
7.微信sdk经常升级,如果你开发的时候有最新的就用最新的吧....