WeChat API open.weixin.qq.com/cgi-bin/sho… 1. Apply for your AppID note: the package name is correct and the signature is correct. The signature generation tool provided by wechat can get the signature by adding the package name res.wx.qq.com/open/zh_CN/… 2. Download the development kit open.weixin.qq.com/cgi-bin/sho… 3. Introduce development kits into the project




Introduce development kits into the project

4. Use the development kit the following codes are registered in the MainActivity [1] wechat

Private static Final String APP_ID=" Your appId "; private IWXAPI api; private void regToWx(){ api= WXAPIFactory.createWXAPI(this,APP_ID,true); api.registerApp(APP_ID); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Register with wechat regToWx(); }Copy the code

[2] Log in to wechat

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Register with wechat regToWx(); findViewById(R.id.wechat_login).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Final sendauth.req Req = new sendauth.req (); req.scope = "snsapi_userinfo"; req.state = "wechat_sdk_demo_test"; api.sendReq(req); }}); }Copy the code

5. Here’s the most important step… [1] Create wxAPI package under your package name. Wxapi




Your package name.wxAPI

[2] Create a new activity WXEntryActivity under WxAPI [3] add the activity registration in androidmanifest.xml


Copy the code

[4] Inherit Activity in WXEntryActivity and implement IWXAPIEventHandler interface




Implement IWXAPIEventHandler interface

Rewrite onCreate method in onCreate need to re-register wechat or wechat will not execute back




Override the onCreate method

Implement onReq and onResp methods. In onResp, you can get code, AppID, AppSecret, etc., via API for access_token; The access_token interface is invoked to obtain basic data resources or help users to perform basic operations. Please refer to open.weixin.qq.com/cgi-bin/sho…




OnReq and onResp methods