- /**
- * 登录
- */
- public class Login extends Activity {
- private EditText etName, etPass;
- private Button bnLogin;
- private CheckBox isCheck;
- /** 0--正在搜索,1--已匹配,2--未找到匹配,3--正在连接 */
- private int WIFI_STATE = 0;
- private int LOGIN_STATE = -1;
- private AppContext appContext;
- /** 登录等待动画 */
- private ProgressDialog mProDialog;
- public static boolean ISLOGIN = false;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
- initViews();
- initValue();
- initWifi();
- appContext = (AppContext) getApplication();
- // 如果路由器上已经登录,直接跳转到主页面
- if (ISLOGIN) {
- redirect();
- }
- bnLogin.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if (WIFI_STATE == 0) {
- DialogKit.ToastMessage(Login.this, "正在启动连接热点,请稍后!");
- return;
- }
- if (WIFI_STATE == 2) {
- DialogKit.ToastMessage(Login.this, "未找到匹配的热点!");
- return;
- }
- String username = etName.getText().toString();
- String passwd = etPass.getText().toString();
- if (!validate(username, passwd))
- return;
- loading();
- }
- });
- }
- /**
- * 登录验证
- */
- private void loading(){
- mProDialog = ProgressDialog.show(Login.this, null, "正在登录,请稍后...", true, true);
- final Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- LOGIN_STATE = msg.what;
- if(mProDialog != null){
- mProDialog.dismiss();
- mProDialog = null;
- }
- DialogKit.showMsg(Login.this, LOGIN_STATE);
- }
- };
- new Thread(){
- public void run(){
- String username = etName.getText().toString();
- String passwd = etPass.getText().toString();
- String url = "http://" + appContext.getWifiRouteAddr() + GlobalConst.ROUTER_LOGIN_POSTFIX;
- int state = Integer.valueOf(addRouteLogin(url, username, passwd));
- handler.obtainMessage(state).sendToTarget();
- if (state == 1 || state == 7 ) {
- ISLOGIN = true;
- saveSetting();
- redirect();
- }
- }
- }.start();
- }
- /**
- * 初始化网络连接
- *
- * @param handler
- *
- * @return 1--已匹配 ,2--未匹配
- */
- private void initWifi() {
- WifiKit wifi = new WifiKit(this);
- // 如果wifi关闭,开启
- if (!wifi.isOpen()) {
- wifi.openWifi();
- wifi.startScan();
- DialogKit.ToastMessage(this, "正在开启wifi");
- } else if (wifi.getWifiName() != null && wifi.getWifiName().startsWith(GlobalConst.WIFI_PREFIX)) {
- WIFI_STATE = 1;
- DialogKit.ToastMessage(this, "已经连接上无线热点 :) ");
- return;
- }
- // android不允许线程改变ui,使用handler接收线程发送的消息
- final Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- WIFI_STATE = msg.what;
- switch (WIFI_STATE) {
- case 0:
- DialogKit.ToastMessage(Login.this, "正在扫描无线热点,请稍候...");
- break;
- case 1:
- DialogKit.ToastMessage(Login.this, "已经连接上无线热点 :) ");
- break;
- case 2:
- DialogKit.ToastMessage(Login.this, "未找到匹配的热点!");
- break;
- case 3:
- DialogKit.ToastMessage(Login.this, "正在连接...");
- break;
- default:
- DialogKit.ToastMessage(Login.this, "无线网络连接错误,请联系管理员...");
- break;
- }
- }
- };
- new Thread() {
- @Override
- public void run() {
- WifiKit wifi = new WifiKit(Login.this);
- // 提示正在扫描
- handler.obtainMessage(0).sendToTarget();
- try {
- Thread.sleep(5000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (wifi.getWifiName() != null
- && wifi.getWifiName().startsWith(GlobalConst.WIFI_PREFIX)) {
- handler.obtainMessage(1).sendToTarget();
- return;
- }
- wifi.startScan();
- List<ScanResult> list = wifi.getWifiList();
- if (list != null) {
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i).SSID.startsWith(GlobalConst.WIFI_PREFIX)) {
- // 搜索到匹配的热点,连接
- wifi.addNetwork(wifi.CreateWifiInfo(list.get(i).SSID));
- handler.obtainMessage(3).sendToTarget();
- try {
- // 先提示正在连接,等待3秒提示已经连接上
- Thread.sleep(3000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- handler.obtainMessage(1).sendToTarget();
- return;
- }
- }
- }
- // 提示未搜索到热点
- handler.obtainMessage(2).sendToTarget();
- return;
- }
- }.start();
- }
- /**
- * 初始化输入框的值
- */
- private void initValue() {
- SharedPreferences preferences = getPreferences(Activity.MODE_PRIVATE);
- etName.setText(preferences.getString("username", null));
- isCheck.setChecked(preferences.getBoolean("remember", true));
- if (isCheck.isChecked()) {
- etPass.setText(SystemKit.decrypt(preferences.getString("password",
- null)));
- } else {
- etPass.setText(null);
- }
- }
- /**
- * 初始化界面布局
- */
- private void initViews() {
- etName = (EditText) findViewById(R.id.userEditText);
- etPass = (EditText) findViewById(R.id.pwdEditText);
- bnLogin = (Button) findViewById(R.id.bnLogin);
- isCheck = (CheckBox) findViewById(R.id.ischecked);
- }
- /**
- * 页面跳转
- */
- public void redirect() {
- Bundle data = new Bundle();
- data.putString("username", etName.getText().toString());
- data.putString("password", etPass.getText().toString());
- Intent intent = new Intent(this, Main.class);
- intent.putExtras(data);
- startActivity(intent);
- finish();
- }
- /**
- * 判断登录用户
- *
- * @throws Exception
- */
- public static boolean loginPro(String username, String password) {
- JSONObject jsonObj;
- Map<String, String> map = new HashMap<String, String>();
- map.put("username", username);
- map.put("password", password);
- try {
- jsonObj = new JSONObject(HttpKit.doPost(GlobalConst.LOGIN_URL, map));
- if (jsonObj.getBoolean("success"))
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- /**
- * 路由器端登录验证
- */
- public String addRouteLogin(String url, String username, String password) {
- Map<String, String> map = new HashMap<String, String>();
- map.put("name", username);
- map.put("admpass", password);
- try {
- // 匹配 mod = "1"
- String msg = HttpKit.doPost(url, map);
- int i = msg.indexOf("mod") + 7;
- return msg.substring(i, i + 1);
- // 没有路由做测试,假设已经正确登录,返回1
- // return "1";
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "0";
- }
- /**
- * 非空验证
- *
- * @param password
- * @param username
- */
- private boolean validate(String username, String password) {
- if ("".equals(username)) {
- DialogKit.showDialog(this, "请输入用户名!", false);
- return false;
- }
- if ("".equals(password)) {
- DialogKit.showDialog(this, "请输入密码!", false);
- return false;
- }
- return true;
- }
- /**
- * 保存用户名和密码到配置文件中
- */
- public boolean saveSetting() {
- SharedPreferences saveconfigs;
- saveconfigs = getPreferences(Activity.MODE_PRIVATE);
- SharedPreferences.Editor editor = saveconfigs.edit();
- editor.putString("username", etName.getText().toString());
- editor.putString("password",
- SystemKit.encrypt(etPass.getText().toString()));
- editor.putBoolean("remember", isCheck.isChecked());
- return editor.commit();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1712201411300.html
来源: http://www.codesnippet.cn/detail/1712201411300.html