LoginActivity.java
3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.huaheng.mobilehuaheng;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import com.huaheng.mobilehuaheng.download.OKHttpInterface;
import com.huaheng.mobilehuaheng.download.OKHttpUtils;
import com.huaheng.mobilehuaheng.util.Constant;
import com.huaheng.mobilehuaheng.util.MobileUtils;
import com.huaheng.mobilehuaheng.util.WMSLog;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class LoginActivity extends Activity {
@BindView(R.id.editName)
EditText editName;
@BindView(R.id.editPassword)
EditText editPassword;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
mContext = this;
setTitle("主页");
// Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
// startActivity(intent);
MobileUtils.acquireWakeLock(mContext);
MobileUtils.requestAppPermission(LoginActivity.this);
initView();
}
private void initView() {
editName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
MobileUtils.requestFocus(editPassword);
return false;
}
});
editPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
login();
return false;
}
});
}
@OnClick(R.id.loginBtn)
public void onViewClicked() {
login();
}
public void login() {
String url = Constant.LOGIN_PATH;
String user = editName.getText().toString();
String password = editPassword.getText().toString();
final JSONObject object = new JSONObject();
try {
object.put("name", user);
object.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
OKHttpUtils.as(mContext).commonPost(url, object.toString(), new OKHttpInterface() {
@Override
public void onSuccess(String info) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(info);
int code = jsonObject.getInt("code");
String msg = jsonObject.getString("msg");
String data = jsonObject.getString("data");
Looper.prepare();
WMSLog.d("code:" + code);
if(code == 200) {
MobileUtils.startActivity(mContext, MainActivity.class);
} else {
MobileUtils.showShort(msg);
}
Looper.loop();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFail(String msg) {
WMSLog.d("onFail :" + msg);
}
});
OKHttpUtils.as(mContext).commonPost(url, object.toString(), new OKHttpInterface() {
@Override
public void onSuccess(String msg) {
}
@Override
public void onFail(String msg) {
WMSLog.d("onFail :" + msg);
}
};
}
}