package com.example.login; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class MainActivity extends ActionBarActivity { String DATABASE_NAME="login.sqlite"; String DATABASE_PATH="/databases/"; SQLiteDatabase database=null; //lấy đường dẫn cho database private String path_data(){ return getApplicationInfo().dataDir + DATABASE_PATH + DATABASE_NAME; } //hàm kết nối với database private void connectData(){ File dbfile=getDatabasePath(DATABASE_NAME); if(!dbfile.exists()){ try{ CopyDatafromassets(); Toast.makeText(this,"Sao chép CSDL vào hệ thống thành công!",Toast.LENGTH_LONG).show(); }catch (Exception e){ Toast.makeText(this, e.toString(),Toast.LENGTH_LONG).show(); } } } //hàm sao chép dữ liệu từ assets đến database của phần mềm private void CopyDatafromassets() { try{ InputStream myinput=getAssets().open(DATABASE_NAME); String outfilename= path_data(); File f=new File(getApplicationInfo().dataDir + DATABASE_PATH); if(!f.exists()){ f.mkdir(); } OutputStream myoutput=new FileOutputStream(outfilename); byte[] buffer= new byte[1024]; int length; while((length=myinput.read(buffer))>0){ myoutput.write(buffer,0,length); } myoutput.flush(); myoutput.close(); myinput.close(); }catch (Exception e){ Log.e("Lỗi sao chép", e.toString()); } } int i=0; TextView username; TextView password; Button login; Button registration; @Override // tạo thuộc tính protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connectData(); // kết nối dữ liệu connectView(); // ánh xạ clickbtnlogin(); // đăng nhập clickbtnRegistration(); // đăng ký } private void clickbtnRegistration() { registration.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Registration.class); startActivity(intent); } }); } private void kiemtrataikhoan(String tentaikhoan, String pass) { database=openOrCreateDatabase(DATABASE_NAME,MODE_PRIVATE,null); Cursor cursor =database.rawQuery("SELECT * FROM taikhoan WHERE tentaikhoan=? AND matkhau=?",new String[]{tentaikhoan,pass}); while (cursor.moveToNext()){ i=cursor.getInt(0); } cursor.close(); } private void clickbtnlogin() { login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i=0; kiemtrataikhoan(username.getText().toString(),password.getText().toString()); if(i==0){ Toast.makeText(MainActivity.this,"Tài khoản không đúng",Toast.LENGTH_LONG).show(); } else if(i==1){ Intent intent= new Intent(MainActivity.this,Admin.class); startActivity(intent); } else{ Intent intent= new Intent(MainActivity.this,Thongtin.class); intent.putExtra("idlogin",i); startActivity(intent); } } }); } private void connectView() { username = (TextView) findViewById(R.id.username); password = (TextView) findViewById(R.id.password); login = (Button) findViewById(R.id.btn_login); registration = (Button) findViewById(R.id.btn_registration); } }