login 登陸

login寫法範例: 
rivate void login() {
  // // 單機版
  // Intent intent = new Intent();
  // intent.setClass(AccountLoginActivity.this, MainTabActivity.class);
  // startActivity(intent);
  // AccountLoginActivity.this.finish();

  // 聯網版
  if (isLegal()) {
   showWaiting(isWaiting);
   String userName = mUsernameEditText.getText().toString();
   String password = mPasswordEditText.getText().toString();

   RequestParams params = new RequestParams();
   params.add("policeNo", userName);
   params.add("policePassword", password);
   HttpUtils.postAsync(ServerAPI.URL_LOGIN, params, loginResponseHandler);
  }

 }

 private boolean isLegal() {
  boolean legal = true;
  String msg = null;
  String username = mUsernameEditText.getText().toString();
  String password = mPasswordEditText.getText().toString();
  if (username == null || username.length() == 0) {
   legal = false;
   msg = getResources().getString(R.string.user_error_msg1);
  }
  else if (password == null || password.length() == 0) {
   legal = false;
   msg = getResources().getString(R.string.user_error_msg2);
  }
  else if (password.length() < 6 || password.length() > 20) {
   legal = false;
   msg = getResources().getString(R.string.user_error_msg3);
  }
  showErrorMsg(msg);
  return legal;
 }

 private void showErrorMsg(String msg) {
  hideWaiting();
  if (msg == null) {
   if (mMessageBox.isShown())
    mMessageBox.setVisibility(View.INVISIBLE);
  }
  else {
   if (!mMessageBox.isShown()) {
    mMessageBox.setVisibility(View.VISIBLE);
   }
   mMessageBox.setText(msg);
  }
 }

 TextHttpResponseHandler loginResponseHandler = new TextHttpResponseHandler() {

  @Override
  public void onFailure(int statusCode, Header[] headers, String responseString,
          Throwable throwable) {

   Log.w(TAG, statusCode + " Login:" + responseString);
   // hideWaiting();
   showErrorMsg("網絡連接失敗,請重試!" + statusCode);
  }

  @Override
  public void onSuccess(int statusCode, Header[] headers, String responseString) {
   // hideWaiting();
   Log.i(TAG, "Login:" + responseString);

   LoginHttpBean loginHttpBean = new Gson().fromJson(responseString, LoginHttpBean.class);
   if ("200".equals(loginHttpBean.getCode())) {
    BaseApplication.appVersionList.addAll(loginHttpBean.getAppApplicationAllId());
    policeInfoList.addAll(loginHttpBean.getPoliceInfoBean());

    BaseApplication.policeInfoList = policeInfoList;
    // savePoliceBean(loginHttpBean.getPoliceInfoBean().get(0));
    BaseApplication.savePoliceBean(responseString);

    Intent intent = new Intent();
    intent.setClass(AccountLoginActivity.this, MainTabActivity.class);
    startActivity(intent);
    hideWaiting();// 隱藏登錄過程中顯示的loading
    AccountLoginActivity.this.finish();
   }
   else {
    showErrorMsg(loginHttpBean.getCodeMsg());
   }
  }
 };

 // public void savePoliceBean(PoliceBean policeBean) {
 // SharedPreferences mSharedPreferences = getSharedPreferences("police",
 // Context.MODE_PRIVATE);
 // SharedPreferences.Editor editor = mSharedPreferences.edit();
 // editor.putInt("policeId", policeBean.getId());
 // editor.commit();
 // }

//顯示login那個畫面 在activity上面新建層來出現login畫面
 protected final void showWaiting(boolean isBlur) {
  isWaiting = true;
  try {
   WindowManager.LayoutParams layoutParams = null;
   if (isBlur) {
    layoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_BLUR_BEHIND
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT);
   }
   else {
    layoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT);
   }

   if (loadView == null) {
    LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//載入login顯示的佈局
    loadView = inflate.inflate(R.layout.view_loading, null);
   }

   windowManager.addView(loadView, layoutParams);
  }
  catch (Throwable e) {
   e.printStackTrace();
   isWaiting = false;
  }
 }

 protected final void hideWaiting() {
  isWaiting = false;
  try {
   if (loadView != null) {

//移除login層
    windowManager.removeView(loadView);
    loadView = null;
   }
  }
  catch (Throwable e) {
   e.printStackTrace();
  }
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章