<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yeejay.reader" > ...
defaultConfig {
applicationId "com.yeejay.reader"
...
}
signingConfigs {
release {
storeFile file('keystore.jks')
storePassword 'xxx'
keyAlias 'xxx'
keyPassword 'xxx'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
signingConfig signingConfigs.release
}
}
dependencies {
...
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
...
}
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
public static final String APP_ID = "xxxxxxxxxx";
public static final String APP_SECRET = "xxxxxxxxxx";
private static final String WEIXIN_SCOPE = "snsapi_userinfo";
private static final String WEIXIN_STATE = "login_wx";
private IWXAPI api;
private SendAuth.Req req;
private TextView loginWx;
private User user;
private String wxCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_wx);
loginWx = (TextView) findViewById(R.id.login_wx);
api = WXAPIFactory.createWXAPI(this, APP_ID, false);
api.registerApp(APP_ID);
try {
api.handleIntent(getIntent(), this);
} catch (NullPointerException e) {
e.printStackTrace();
}
initListener();
}
private void initListener() {
loginWx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendAuth();
}
});
}
private void sendAuth() {
req = new SendAuth.Req();
req.scope = WEIXIN_SCOPE;
req.state = WEIXIN_STATE;
api.sendReq(req);
}
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
int type = baseResp.getType();
if (type == 1) {
// 登录
SendAuth.Resp sendAuthResp = (SendAuth.Resp) baseResp;
wxCode = sendAuthResp.code;
String state = ((SendAuth.Resp) baseResp).state;
int errCode = baseResp.errCode;
Intent intent = new Intent(WXEntryActivity.this, HomeActivity.class);
intent.putExtra("login", "login");
intent.putExtra("wxCode", wxCode);
startActivity(intent);
YeeUtils.animOpenActivity(this);
finish();
}
}
@Override
public void finish() {
super.finish();
YeeUtils.animCloseActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
api.unregisterApp();
}
}
api = WXAPIFactory.createWXAPI(this, APP_ID, false);
api.registerApp(APP_ID);
try {
api.handleIntent(getIntent(), this);
} catch (NullPointerException e) {
e.printStackTrace();
}
private void getResult(String code) {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+ APP_ID
+ "&secret="
+ APP_SECRET
+ "&code="
+ code
+ "&grant_type=authorization_code";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new SuccessResponse(), new ErrorListener());
StudyApplication.getInstance().newRequestQueue(this).add(request);
}
class SuccessResponse implements Response.Listener<JSONObject> {
@Override
public void onResponse(JSONObject response) {
if (null != response) {
String openid = null;
try {
openid = response.getString("openid")
.toString().trim();
String access_token = response
.getString("access_token").toString().trim();
SharePreferenceHelper.putValue(getApplicationContext(), "access_token", access_token);
getUID(openid, access_token);
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void getUID(final String openId, final String accessToken) {
String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new UIDSuccessResponse(), new ErrorListener());
StudyApplication.getInstance().newRequestQueue(this).add(request);
}
class UIDSuccessResponse implements Response.Listener<JSONObject> {
@Override
public void onResponse(JSONObject response) {
if (response == null) {
return;
}
String nickname = null;
nickname = response.optString("nickname");
String unionid = response.optString("unionid");
String headUrl = response.optString("headimgurl");
SharePreferenceHelper.putValue(getApplicationContext(), "nickname", nickname);
SharePreferenceHelper.putValue(getApplicationContext(), "photourl", headUrl);
SharePreferenceHelper.putValue(getApplicationContext(), "uin", unionid);
if (user == null) {
user = new User();
}
user.setUin(unionid);
user.setNickName(nickname);
user.setHeadImgUrl(headUrl);
...
}
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有