用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

这是一个简单的学生考勤管理系统

2017-05-12 作者:林林彩虹举报

[android]代码库

package cindy.android.test.synclistview;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import cindy.android.test.synclistview.R;

import com.info.util.userInfo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Login extends Activity {

	private String userName;
	private String password;
	String strResult = null;

	private EditText view_userName;
	private EditText view_password;
	private CheckBox view_rememberMe;
	private Button view_loginSubmit;
	private Button view_loginRegister;
	private TextView textView1;
	
	private boolean isnet = false;
	
	private static final int MENU_EXIT = Menu.FIRST - 1;
	private static final int MENU_ABOUT = Menu.FIRST;
	
	private final String SHARE_LOGIN_TAG = "MAP_SHARE_LOGIN_TAG";


	private String SHARE_LOGIN_USERNAME = "MAP_LOGIN_USERNAME";

	
	private String SHARE_LOGIN_PASSWORD = "MAP_LOGIN_PASSWORD";

	
	private boolean isNetError;

	
	private ProgressDialog proDialog;

	
	Handler loginHandler = new Handler() {
		public void handleMessage(Message msg) {
			isNetError = msg.getData().getBoolean("isNetError");
			if (proDialog != null) {
				proDialog.dismiss();
			}
			if (isNetError) {
				Toast.makeText(Login.this, "登陆失败:\n1.请检查您网络连接.\n2.请联系我们.!",
						Toast.LENGTH_SHORT).show();
			}
			
			else {
				Toast.makeText(Login.this, "登陆失败,请输入正确的用户名和密码!",
						Toast.LENGTH_SHORT).show();
				
				clearSharePassword();
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		findViewsById();
		initView(false);
		
		setListener();
	}

	
	private void findViewsById() {
		view_userName = (EditText) findViewById(R.id.loginUserNameEdit);
		view_password = (EditText) findViewById(R.id.loginPasswordEdit);
		view_rememberMe = (CheckBox) findViewById(R.id.loginRememberMeCheckBox);
		view_loginSubmit = (Button) findViewById(R.id.loginSubmit);
		view_loginRegister = (Button) findViewById(R.id.loginRegister);
	}

	
	private void initView(boolean isRememberMe) {
		SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
		String userName = share.getString(SHARE_LOGIN_USERNAME, "");
		String password = share.getString(SHARE_LOGIN_PASSWORD, "");
		Log
				.d(this.toString(), "userName=" + userName + " password="
						+ password);
		if (!"".equals(userName)) {
			view_userName.setText(userName);
		}
		if (!"".equals(password)) {
			view_password.setText(password);
			view_rememberMe.setChecked(true);
		}
		
		if (view_password.getText().toString().length() > 0) {
			
		}
		share = null;
	}

	
	private boolean validateLocalLogin(String userName, String password,
			String validateUrl) {
		System.out.println("username:"+userName);
		System.out.println("password:"+password);
		
		boolean loginState = false;

		HttpPost httpRequest =new HttpPost(validateUrl);
		

		List params=new ArrayList();
		params.add(new BasicNameValuePair("username",userName));
		params.add(new BasicNameValuePair("passwd",password));	
		
		try{
			
			httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

		
			
			HttpClient httpclient = new DefaultHttpClient (); 
		    HttpParams httpparams = httpclient . getParams (); 
		    HttpConnectionParams.setConnectionTimeout ( httpparams , 5000 ); 
		    HttpConnectionParams.setSoTimeout ( httpparams , 5000 );
		    HttpResponse httpResponse =  httpclient.execute(httpRequest);
			
			
			if(httpResponse.getStatusLine().getStatusCode()==200){
				
					
				BufferedReader reader=new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));
				String str = reader.readLine();
				reader.close();
				
				strResult = str;
				
				  
				
				System.out.println("strResult: "+strResult);
				System.out.println("str: "+str);
			}
		}catch(Exception e){
			Message message = new Message();
			Bundle bundle = new Bundle();
			bundle.putBoolean("isNetError", true);
			message.setData(bundle);
			loginHandler.sendMessage(message);
			isnet = true;
//			
		}

        if(strResult != null && !strResult.equals("error")){
        	  loginState = true;
        }else{
        	loginState =false;
        }
	
		if (loginState) {
			if (isRememberMe()) {
				saveSharePreferences(true, true);
			} else {
				saveSharePreferences(true, false);
			}
			
			userInfo.setUsername(userName);
		} else {
			
			if (!isNetError) {
				clearSharePassword();
			}
		}
		if (!view_rememberMe.isChecked()) {
			clearSharePassword();
		}
		return loginState;
	}

	
	private void saveSharePreferences(boolean saveUserName, boolean savePassword) {
		SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
		if (saveUserName) {
			Log.d(this.toString(), "saveUserName="
					+ view_userName.getText().toString());
			share.edit().putString(SHARE_LOGIN_USERNAME,
					view_userName.getText().toString()).commit();
		}
		if (savePassword) {
			share.edit().putString(SHARE_LOGIN_PASSWORD,
					view_password.getText().toString()).commit();
		}
		share = null;
	}

	
	private boolean isRememberMe() {
		if (view_rememberMe.isChecked()) {
			return true;
		}
		return false;
	}

	
	private OnClickListener submitListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			proDialog = ProgressDialog.show(Login.this, "提示",
					"正在登陆...", true, true);
		
			Thread loginThread = new Thread(new LoginFailureHandler());
			loginThread.start();
		}
	};


	
	private OnCheckedChangeListener rememberMeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (view_rememberMe.isChecked()) {
				Toast.makeText(Login.this, "如果登录成功,以后账号和密码会自动输入!",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	
	private OnClickListener registerLstener = new OnClickListener() {
		@Override
		public void onClick(View v) {
		
			Toast.makeText(Login.this, "注册功能暂不开放",
					Toast.LENGTH_SHORT).show();
		}
	};

	
	private void setListener() {
		view_loginSubmit.setOnClickListener(submitListener);
		view_loginRegister.setOnClickListener(registerLstener);
		view_rememberMe.setOnCheckedChangeListener(rememberMeListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_EXIT, 0, getResources().getText(R.string.MENU_EXIT));
		menu.add(0, MENU_ABOUT, 0, getResources().getText(R.string.MENU_ABOUT));
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		switch (item.getItemId()) {
		case MENU_EXIT:
			finish();
			break;
		case MENU_ABOUT:
			alertAbout();
			break;
		}
		return true;
	}

	
	private void alertAbout() {
		new AlertDialog.Builder(Login.this).setTitle(R.string.MENU_ABOUT)
				.setMessage(R.string.aboutInfo).setPositiveButton(
						R.string.ok_label,
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int i) {
							}
						}).show();
	}

	//清空账户信息
	private void clearSharePassword() {
		SharedPreferences share = getSharedPreferences(SHARE_LOGIN_TAG, 0);
		share.edit().putString(SHARE_LOGIN_PASSWORD, "").commit();
		share = null;
	}

	class LoginFailureHandler implements Runnable {
		@Override
		public void run() {
				userName = view_userName.getText().toString();
				password = view_password.getText().toString();
				
				String validateURL="http://10.0.2.2/kaoqin/phone/login.php";
				boolean loginState = validateLocalLogin(userName, password,
						validateURL);
				Log.d(this.toString(), "validateLogin");
	
				
				if (loginState) {
					
					Intent intent = new Intent();
					//跳转 到课程信息界面
					intent.setClass(Login.this, courseInfo.class);
					Bundle bundle = new Bundle();
					bundle.putString("MAP_USERNAME", userName);
					intent.putExtras(bundle);
					
					finish();
					startActivity(intent);
					proDialog.dismiss();
				} else {
					
					if(!isnet){
						Message message = new Message();
						Bundle bundle = new Bundle();
						bundle.putBoolean("isNetError", false);
						message.setData(bundle);
						loginHandler.sendMessage(message);
					}
				}

		}

	}
}

[源代码打包下载]




分享到:
更多

网友评论    (发表评论)

共6 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。