private static finalStringAPP_ID="";privateIWXAPIapi;privateButtonwx_login;privateButtonwx_share;@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initEvent();}private voidinitEvent() {wx_login.setOnClickListener(this);wx_share.setOnClickListener(this);}private voidinitView() {wx_login= (Button) findViewById(R.id.wx_login);wx_share= (Button) findViewById(R.id.wx_share);}private voidmLogin() {
regToWx();finalSendAuth.Req req =newSendAuth.Req();req.scope="snsapi_userinfo";req.state="wechat_sdk_demo_test";//com.xl.wx.demoapi.sendReq(req);Toast.makeText(this,"mLogin()",Toast.LENGTH_LONG).show();}private voidregToWx() {
Log.e("AAA","------------------------------------>");api= WXAPIFactory.createWXAPI(this,APP_ID, true);api.registerApp(APP_ID);}@Overridepublic voidonReq(BaseReq baseReq) {
}@Overridepublic voidonResp(BaseResp baseResp) {
Log.e("BBB","----------------------------------------------------------->");intresult =0;switch(baseResp.errCode) {caseBaseResp.ErrCode.ERR_OK:
result = R.string.ERR_OK;try{
SendAuth.Resp sendResp = (SendAuth.Resp) baseResp;}catch(Exception e) {
e.printStackTrace();}break;caseBaseResp.ErrCode.ERR_USER_CANCEL:
result = R.string.ERR_USER_CANCEL;finish();break;caseBaseResp.ErrCode.ERR_AUTH_DENIED:
result = R.string.ERR_AUTH_DENIED;finish();break;default:
result = R.string.ERR_AUTH_UNKNOWN;finish();break;}
Toast.makeText(this,result,Toast.LENGTH_LONG).show();}@Overridepublic voidonClick(View view) {switch(view.getId()) {caseR.id.wx_login:
mLogin();break;caseR.id.wx_share:
shareText();break;default:break;}
}private voiddoGetUserInfo(JSONObject response) {
HashMap paramMap =newHashMap();paramMap.put("openid",response.optString("openid"));paramMap.put("access_token",response.optString("access_token"));paramMap.put("lang","zh_CN");String url ="https://api.weixin.qq.com/sns/userinfo?access_token=%1$s&openid=%2$s&lang=zh_CN";// new WXUserInfoTask().execute(String.format(url,// response.optString("access_token"),// response.optString("openid")));}private voidshareText() {//初始化WXTextObject对象,填写分享文本WXTextObject textObj =newWXTextObject();textObj.text="123";//用WXTextObject对象初始化一个WXMediaMessage对象WXMediaMessage msg =newWXMediaMessage();msg.mediaObject= textObj;msg.description="456";//构造一个Req对象SendMessageToWX.Req req =newSendMessageToWX.Req();// req.transaction = buildTransaction("text");req.message= msg;//req.transaction = buildTransaction("text");req.scene= SendMessageToWX.Req.WXSceneSession;api.sendReq(req);Toast.makeText(this,"shareText()----->",Toast.LENGTH_LONG).show();
}
回调界面------------------------------------------------------------------------>
配置权限------------------------------------------------------------------------>
----->填写第三方应用的appid
Activity界面权限---------------------------------------------------------------->
注意:一点要加入微信sdk包