标签:
1、单击登陆图片按钮 LoginActivity
private IWXAPI mWeixinAPI; public void loginMsgChat(){ if (mWeixinAPI == null) { mWeixinAPI = WXAPIFactory.createWXAPI(this, AppID, false); } if (!mWeixinAPI.isWXAppInstalled()) { Toast.makeText(context, "请安装微信", Toast.LENGTH_SHORT).show(); return; } mWeixinAPI.registerApp(AppID); SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "wechat_sdk_gushen";; mWeixinAPI.sendReq(req); }
2、微信客户端回调 WXEntryActivity
WXEntryActivity extends Activity implements IWXAPIEventHandler private IWXAPI api; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); api = WXAPIFactory.createWXAPI(this, Content.MSG_APPID, false); api.registerApp(Content.MSG_APPID); api.handleIntent(getIntent(), this); } @Override public void onReq(BaseReq arg0) { // TODO Auto-generated method stub } @Override public void onResp(BaseResp arg0) { // TODO Auto-generated method stub int result = 0; switch (arg0.errCode) { case BaseResp.ErrCode.ERR_OK: result = R.string.errcode_success; sendAuthResp = (SendAuth.Resp) arg0; handler.sendEmptyMessage(2); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = R.string.errcode_cancel; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = R.string.errcode_deny; break; default: result = R.string.errcode_unknown; break; } finish(); } private Handler handler = new Handler(Looper.myLooper()) { public void handleMessage(Message msg) { switch (msg.what) { case 11: Intent intent = new Intent(WXEntryActivity.this, LoginActivity.class); HashMap<String, Object> j1 = (HashMap<String, Object>) msg.obj; Bundle bundle = new Bundle(); bundle.putString("thirdid", j1.get("openid").toString()); bundle.putString("fromConnect", "" + 3); bundle.putString("nameConnect", j1.get("nickname").toString()); bundle.putString("headimgurl", j1.get("headimgurl").toString()); intent.putExtras(bundle); startActivity(intent); break; case 2: new Thread1().start(); break; case 3: new Thread2().start(); break; default: break; } } }; public void WeiAT() { String url_constant1 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Content.MSG_APPID + "&secret=" + Content.MSG_AppSecret + "&code=" + sendAuthResp.token + "&grant_type=authorization_code"; } public void WeiGeren() { String url_constant2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + ACCESS_TOKEN + "&openid=" + OPENID; Message message = new Message(); message.what = 11; message.obj = hm; handler.sendMessage(message); }
@Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); setIntent(intent); Bundle bundle=new Bundle(); bundle=intent.getExtras(); if(bundle!=null){ String s1=bundle.getString("thirdid"); String s2=bundle.getString("fromConnect"); String s3=bundle.getString("nameConnect"); photoUrl = bundle.getString("headimgurl"); thirdPair.clear(); thirdPair.add(new BasicNameValuePair("thirdid",s1)); thirdPair.add(new BasicNameValuePair("fromConnect", s2)); thirdPair.add(new BasicNameValuePair("nameConnect", s3)); 接着是第三方登录接口 } }
标签:
原文地址:http://blog.csdn.net/xinnian25/article/details/43952117