Android integrated WeChat login
某草草
某草草 2017-06-23 09:15:06
0
2
1207

Recently, there is a project that needs to integrate WeChat third-party login, and requires the use of native SDK. The client is developed by Unity3D, so it is necessary to use Android Studio to make a plugin for U3D. I found some information online and pondered it myself. There are a few questions
1. After pulling up WeChat authorization, the user clicked to log in and returned to MainActivity, but the code was not obtained, so I set a lock in MainActivity and judged whether the code was in the onResume method. The value has been returned. Although it can be used, it always feels awkward.
2. This project needs to be converted into an arr file for u3d to use. How can I pass the code to U3D? The subsequent work of obtaining access_token is completed on the server side. .
The following is the code I wrote. It can be used under Android. Everything is normal and the code can be obtained. Please tell me how to optimize my code so that the code can be obtained after the user clicks login in WeChat authorization and returns to MainActivity, and can be packaged into arr, u3D can obtain it, thank you very much, the following is my code

MainActivity

package com.mazingtec.hxddz;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.mazingtec.hxddz.wxapi.WXEntryActivity;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.unity3d.player.UnityPlayerActivity;

public class MainActivity extends UnityPlayerActivity implements View.OnClickListener {

    boolean lock = true;
    Button btnWeChat;
    public String code;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnWeChat = (Button) findViewById(R.id.btnWeChat);
        btnWeChat.setOnClickListener(this);
    }

    @Override
    protected  void onResume()
    {
        super.onResume();

        if(WXEntryActivity.isOK && lock)
        {
            lock = false;
            code = WXEntryActivity.CODE;
        }
    }

    private void GetCode()
    {
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(this, WXEntryActivity.APP_ID, true);
        iwxapi.registerApp(WXEntryActivity.APP_ID);

        if (iwxapi != null && iwxapi.isWXAppInstalled()) {
            SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "We_did_it";
            iwxapi.sendReq(req);
        } else
            Toast.makeText(this, "用户未安装微信", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v)
    {
        if(v == btnWeChat)
        {
            GetCode();
        }
    }
}

WXEntryActivity

package com.mazingtec.hxddz.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by wei on 2017/6/21.
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    public static final String APP_ID = "wxopq239o809uqewfojoasidfj";
    private IWXAPI iwxapi;
    public static String CODE;
    public static boolean isOK = false;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        iwxapi = WXAPIFactory.createWXAPI(this, APP_ID, true);
        iwxapi.handleIntent(this.getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        setIntent(intent);
        iwxapi.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp)
    {
        switch (baseResp.errCode)
        {
            case BaseResp.ErrCode.ERR_OK:
                //发送成功
                SendAuth.Resp sendResp = (SendAuth.Resp) baseResp;
                if (sendResp != null) {
                    CODE = sendResp.code;
                    isOK = true;
                }
                WXEntryActivity.this.finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                isOK = false;
                WXEntryActivity.this.finish();
                //发送取消
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                isOK = false;
                WXEntryActivity.this.finish();
                //发送被拒绝
                break;
            default:
                isOK = false;
                WXEntryActivity.this.finish();
                //发送返回
                break;
        }
    }
}
某草草
某草草

reply all(2)
世界只因有你
  1. The problem is how to pass the Code obtained from WXEntryActivity back to MainActivity. If it is convenient, just carry this code through Intent and return it to MainActivity. GetIntent in MainActivity and then take out this code. Secondly, it can be solved through EventBus. But it’s a little outweighed by the gain.

  2. Looks like it should be a communication problem between Unity3D and android, right? There are quite a few online solutions

伊谢尔伦

Laxative.
I have not integrated WeChat login, nor have I used U3D, so I dare not make any comments.
But I have a question, shouldn’t the code returned by WeChat SDK be returned through onActivityResult or the like?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template