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

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;
        }
    }
}
某草草
某草草

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!