微信登录

1.导入依赖

dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}

2.在app的build.gradle

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.bw.movie"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        release {
            keyAlias 'bw_movie'
            //自己的密码
            keyPassword '123456'
            //自己的keystore文件路径
            storeFile file('F:\\highgaosix\\Movie\\app\\movie.keystore')
            storePassword '123456'
        }
        debug {
            keyAlias 'bw_movie'
            keyPassword '123456'
            storeFile file('F:\\highgaosix\\Movie\\app\\movie.keystore')
            storePassword '123456'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
        debug {
            signingConfig signingConfigs.debug
        }
    }

    lintOptions {

        checkReleaseBuilds false

        abortOnError false

    }
}

3.创建Constants类

public class Constants {
    public static final String APP_ID = "wxb3852e6a6b7d9516";

    public static IWXAPI wx_api;
}

4.创建wxapi包 必须名字也一样 然后在这个 包下面 建一个类 ,类名WXEntryActivity。

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Constants.wx_api.handleIntent(getIntent(), this);
    }

    //微信请求相应
    @Override
    public void onReq(BaseReq baseReq) {

    }

    //发送到微信请求的响应结果
    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                Log.i("WXTest","onResp OK");

                if(resp instanceof SendAuth.Resp){
                    SendAuth.Resp newResp = (SendAuth.Resp) resp;
                    //获取微信传回的code
                    String code = newResp.code;
                    Log.i("WXTest","onResp code = "+code);
                  	//可以拿到你的code值了 
                }

                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                Log.i("WXTest","onResp ERR_USER_CANCEL ");
                //发送取消
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                Log.i("WXTest","onResp ERR_AUTH_DENIED");
                //发送被拒绝
                break;
            default:
                Log.i("WXTest","onResp default errCode " + resp.errCode);
                //发送返回
                break;
        }
        finish();
    }
}

//清单文件

   <activity
            android:name=".wxapi.WXEntryActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:taskAffinity="com.bw.movie"
            android:theme="@android:style/Theme.NoDisplay" />
  1. 最后 微信登录的点击按钮 点击完之后code值请看第4部里面有返回的code值
weixin_long.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Constants.wx_api = WXAPIFactory.createWXAPI(getApplicationContext(), Constants.APP_ID, true);
                Constants.wx_api.registerApp(Constants.APP_ID);

                final SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk_demo_test";
                Constants.wx_api.sendReq(req);
            }
        });

你可能感兴趣的:(微信)