package jklc.gzznprojectjklc; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Locale; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.MarshalBase64; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.AndroidHttpTransport; import android.net.ConnectivityManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class LoginActivity extends Activity { public static SqliteDBHelper helper; public EditText txtUserName; public EditText txtUserPwd; public TextView txtIMSI, txtVersion; public Button btnLogin, btnRegister; public SQLiteDatabase db; public String userName; public static boolean flag = false; public ProgressDialog pd; public HashMap<String, Object> hashmap; public static ArrayList<HashMap<String, Object>> arrayList; public String _DepartId = ""; public String _UserId = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login); txtUserName = (EditText) findViewById(R.id.txtUserName); txtUserPwd = (EditText) findViewById(R.id.txtUserPwd); txtIMSI = (TextView) findViewById(R.id.txtIMSI); txtVersion = (TextView) findViewById(R.id.txtVersionInfo); btnRegister = (Button) findViewById(R.id.btnZhuce); btnRegister.setOnClickListener(listenerZhuce); txtUserName.setText(""); txtUserPwd.setText(""); txtVersion.setText("最新版本:" + _getVersionInfo()); txtUserName.requestFocus(); txtUserName.setCursorVisible(true); btnLogin = (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(listenerLogin); PhoneInfo info = new PhoneInfo(LoginActivity.this); String IMEI = info.getPhoneInfo(); txtIMSI.setText(IMEI); ConfigureModel model = _GetConfigure(); if (model.displayflag == null) { } else { btnRegister.setVisibility(View.GONE); if (isOpenNetWork()) { _GetVersionInfo(); } } DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); Constant.displayWidth = displayMetrics.widthPixels; Constant.displayHeight = displayMetrics.heightPixels; } OnClickListener listenerZhuce = new OnClickListener() { @Override public void onClick(View v) { Intent it = new Intent(); it.setClass(LoginActivity.this, ConfigureActivity.class); startActivity(it); } }; public void _GetVersionInfo() { try { UpdateAppManager mUpdateManager = new UpdateAppManager( LoginActivity.this); mUpdateManager.checkUpdate(); } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } } public String _getVersionInfo() { PackageInfo info = null; PackageManager pm; pm = this.getPackageManager(); try { info = pm.getPackageInfo(this.getPackageName(), 0); } catch (NameNotFoundException e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } String version = info.versionName; return version; } OnClickListener listenerLogin = new OnClickListener() { @Override public void onClick(View v) { pd = ProgressDialog.show(LoginActivity.this, "提示", "正在登陆中...", true, false); new Thread() { @Override public void run() { LoginActivity.this.runOnUiThread(new Runnable() { @Override public void run() { _GetLogin(); handler.sendEmptyMessage(0); } }); } }.start(); } }; @SuppressWarnings("static-access") public void _GetLogin() { ConfigureModel model = _GetConfigure(); String ip = model.ipAddress; String port = model.port; if (ip == null || port == null) { ShowDialog("请注册服务器IP和端口号"); return; } String url = "http://" + ip + ":" + port + "/WebServers/WLogincheck.asmx"; String nameSpace = "http://tempuri.org/"; String SOAP_ACTION = "http://tempuri.org/Authentication"; String method = "Authentication"; PhoneInfo info = new PhoneInfo(LoginActivity.this); String _LoginUserName = txtUserName.getText().toString(); String userPwd = txtUserPwd.getText().toString(); String IMEI = info.getPhoneInfo(); SoapObject request = new SoapObject(nameSpace, method); request.addProperty("username", _LoginUserName); request.addProperty("pwd", userPwd); request.addProperty("IMEI", IMEI); AndroidHttpTransport transport = new AndroidHttpTransport(url); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = request; (new MarshalBase64()).register(envelope); envelope.dotNet = true; try { if (isOpenNetWork()) { transport.call(SOAP_ACTION, envelope); if (envelope.getResponse() != null) { if (_LoginUserName.equals("") || userPwd.equals("")) { ShowDialog("用户名或者密码不能为空"); } else { Object object = envelope.getResponse(); String str = object.toString(); String[] RealValue = null; RealValue = str.split(","); String result = RealValue[0].toString(); String message = RealValue[1].toString(); if (result.equals("failures")) { ShowDialog(message); } else { MeasurationActivity.userId = RealValue[1] .toString(); HeadDownActivity.userid = RealValue[1].toString(); GroundDownActivity.userId = RealValue[1].toString(); SurroundingActivity.userId = RealValue[1] .toString(); MeasurationActivity.departId = RealValue[5] .toString(); WorningListActivity.userId = RealValue[1] .toString(); WorningListActivity.departId = RealValue[5] .toString(); WaringCountActivity.userId = RealValue[1] .toString(); SurroundingActivity.departID = RealValue[5] .toString(); HeadDownActivity.departId = RealValue[5].toString(); WarningToday.userId = RealValue[1].toString(); WarningToday.departId = RealValue[5].toString(); GroundDownActivity.departId = RealValue[5] .toString(); FunctionListActivity.userId = RealValue[1] .toString(); _DepartId = RealValue[5].toString(); _UserId = RealValue[1].toString(); ContentValues values = new ContentValues(); SimpleDateFormat fort = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", Locale.getDefault()); Date curDate = new Date(System.currentTimeMillis()); String time = fort.format(curDate); values.put("UserName", _LoginUserName); values.put("UserPwd", userPwd); values.put("PhoneIMEI", IMEI); values.put("DateTime", time); helper = new SqliteDBHelper(getApplicationContext()); db = helper.getReadableDatabase(); Cursor c = db.rawQuery("select * from Users", null); if (c.getCount() == 0) { helper.Insert(values, helper.User_Table); } _InsertUserLoginLog(); _SendLoginLog(); Intent intent = new Intent(); intent.setClass(LoginActivity.this, FunctionListActivity.class); startActivity(intent); LoginActivity.this.finish(); txtUserName.setText(""); txtUserPwd.setText(""); Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show(); } } } } else { try { if (_LoginUserName.equals("") || userPwd.equals("")) { ShowDialog("请输入用户名或者密码"); } else { if (IsLogin(_LoginUserName, userPwd)) { _InsertUserLoginLog(); Toast.makeText(getApplicationContext(), "本地登录成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.setClass(LoginActivity.this, FunctionListActivity.class); startActivity(intent); txtUserName.setText(""); txtUserPwd.setText(""); } else { ShowDialog("用户没有找到,请注意大小写。"); } } } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } } } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); Toast.makeText(getApplicationContext(), "网络或者服务器断开不能登录", Toast.LENGTH_SHORT).show(); } } public boolean IsLogin(String Name, String Pwd) { try { helper = new SqliteDBHelper(this); db = helper.getReadableDatabase(); Cursor c = db.rawQuery( "select * from Users where UserName=? and UserPwd=?", new String[] { Name.toString(), Pwd.toString() }); if (c.getCount() > 0) { c.close(); return true; } return false; } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } return false; } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { pd.dismiss(); } }; private void ShowDialog(String message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setMessage(message); builder.setPositiveButton("确定", null); builder.create().show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { AlertDialog isExit = new AlertDialog.Builder(this).create(); isExit.setTitle("提示"); isExit.setMessage("确定要退出系统吗?"); isExit.setButton("确定", listener); isExit.setButton2("取消", listener); isExit.show(); } return false; } DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { case AlertDialog.BUTTON_POSITIVE: _GetClose(); break; case AlertDialog.BUTTON_NEGATIVE: break; default: break; } } }; public void _GetClose() { try { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } } public ConfigureModel _GetConfigure() { ConfigureModel model = new ConfigureModel(); try { helper = new SqliteDBHelper(getApplicationContext()); db = helper.getReadableDatabase(); Cursor c = db.rawQuery("select * from Server", null); while (c.moveToNext()) { model.id = c.getString(0); model.ipAddress = c.getString(1); model.port = c.getString(2); model.displayflag = c.getString(3); } } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } return model; } @SuppressWarnings("static-access") public void _InsertUserLoginLog() { try { String _LoginUserName = txtUserName.getText().toString(); ContentValues values = new ContentValues(); SimpleDateFormat fort = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); Date curDate = new Date(System.currentTimeMillis()); String time = fort.format(curDate); values.put("UserName", _LoginUserName); values.put("LoginTime", time); values.put("State", 0); helper = new SqliteDBHelper(getApplicationContext()); db = helper.getReadableDatabase(); helper.Insert(values, helper.UserLogin_Log); } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } } public void _SendLoginLog() { try { ArrayList<HashMap<String, Object>> list = _GetLoginLogData(); int count = list.size(); for (int i = 0; i < count; i++) { String id = list.get(i).get("ID").toString(); ConfigureModel model = _GetConfigure(); String ip = model.ipAddress; String port = model.port; String url = "http://" + ip + ":" + port + "/WebServers/yonghudenglu.asmx"; String nameSpace = "http://tempuri.org/"; String SOAP_ACTION = "http://tempuri.org/SetObject"; String method = "SetObject"; SoapObject request = new SoapObject(nameSpace, method); request.addProperty("DepartID", _DepartId); request.addProperty("UserName", hashmap.get("UserName") .toString() == null ? "" : hashmap.get("UserName") .toString()); request.addProperty("BiaoDuan", ""); request.addProperty("GongSi", ""); request.addProperty("DengLuShiJian", hashmap.get("LoginTime") .toString() == null ? "" : hashmap.get("LoginTime") .toString()); request.addProperty("XingMing", ""); request.addProperty("UserID", _UserId); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = request; envelope.dotNet = true; AndroidHttpTransport transport = new AndroidHttpTransport(url); try { if (isOpenNetWork()) { transport.call(SOAP_ACTION, envelope); if (envelope.getResponse() != null) { Object object = envelope.getResponse(); String result = object.toString(); if (!result.equals("0")) { helper = new SqliteDBHelper(this); db = helper.getWritableDatabase(); String sql = "update LoginLog set State = 1 where ID='" + id + "'"; db.execSQL(sql); Toast.makeText(getApplicationContext(), "上传成功", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "上传失败", Toast.LENGTH_LONG).show(); } } } } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } } } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } } public ArrayList<HashMap<String, Object>> _GetLoginLogData() { arrayList = new ArrayList<HashMap<String, Object>>(); try { helper = new SqliteDBHelper(this); SQLiteDatabase mdb = helper.getReadableDatabase(); StringBuilder _sql = new StringBuilder(); _sql.append(" select * from LoginLog where State = 0 "); Cursor c = mdb.rawQuery(_sql.toString(), null); while (c.moveToNext()) { hashmap = new HashMap<String, Object>(); hashmap.put("ID", c.getString(0)); hashmap.put("UserName", c.getString(1)); hashmap.put("LoginTime", c.getString(2)); hashmap.put("State", c.getString(3)); arrayList.add(hashmap); } } catch (Exception e) { ErrorMessageInfo._GetErrorMessage("LoginActivity", e.toString(), getApplicationContext()); } return arrayList; } // 网络监测 private boolean isOpenNetWork() { ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (connManager.getActiveNetworkInfo() != null) { return connManager.getActiveNetworkInfo().isAvailable(); } return false; } }