Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==========================================MainActivity
- package com.example.com.myshared;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- EditText txtUser;
- EditText txtPass;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtUser=(EditText)findViewById(R.id.user);
- txtPass=(EditText)findViewById(R.id.pass);
- if (checkUser())
- {
- //if key found , start the second activity.
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- Intent iEntry = new Intent(this,Entry.class);
- iEntry.putExtra("putUser", pref.getString("userLogged", "error"));
- this.startActivity(iEntry);
- }
- }
- private boolean checkUser()
- {
- //declaration of shared preferences
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- //getting value of key "user"
- String userName = pref.getString("userLogged","error");
- //return false if key "user" not exists
- return (!userName.equals("error"));
- }
- public void btnLogin(View v)
- {
- SharedPreferences pref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- String userName=txtUser.getText().toString().toLowerCase();
- String password= txtPass.getText().toString();
- String passDB=pref.getString(userName,"ErrorP");
- if(userName.equals("") || password.equals("")) {
- Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
- return;
- }
- else if(!pref.contains(userName)) {
- Toast.makeText(this, "Please register", Toast.LENGTH_LONG).show();
- return;
- }
- else if(password.equals(passDB)) {
- Toast.makeText(this, "Good to see you", Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(this, "Wrong user name or password", Toast.LENGTH_LONG).show();
- return;
- }
- //declaration of editor to edit shared preferences
- SharedPreferences.Editor editor=pref.edit();
- //create key "user" with value of userName, if key exists, it will be overwritten
- editor.putString("userLogged",userName);
- //commit the changes
- editor.commit();
- Intent iEntry = new Intent(this,Entry.class);
- iEntry.putExtra("putUser", pref.getString("userLogged","error"));
- this.startActivity(iEntry);
- }
- public void btnReg(View v)
- {
- Intent regIntent = new Intent(this, Register.class);
- this.startActivity(regIntent);
- }
- }
- //======================================REGISTER CLASS
- package com.example.com.myshared;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Register extends Activity
- {
- EditText txtUser, txtPass1, txtPass2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.register);
- setPointer();
- }
- public void setPointer()
- {
- txtUser=(EditText)findViewById(R.id.user);
- txtPass1=(EditText)findViewById(R.id.pass1);
- txtPass2=(EditText)findViewById(R.id.pass2);
- }
- public void btnRegister(View v)
- {
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref", MODE_PRIVATE);
- //declaration of editor to edit shared preferences
- SharedPreferences.Editor editor=pref.edit();
- //insert into string value from EditText
- String userName=txtUser.getText().toString().toLowerCase();
- String password1=txtPass1.getText().toString();
- String password2=txtPass2.getText().toString();
- if(userName.equals("")||password1.equals("")){
- Toast.makeText(this, "Please type username and password", Toast.LENGTH_LONG).show();
- return;
- }
- else if(pref.contains(userName)) {
- Toast.makeText(this, "The username exist, please type another username", Toast.LENGTH_LONG).show();
- return;
- }
- else if(!password1.equals(password2)){
- Toast.makeText(this, "The password must be equals", Toast.LENGTH_LONG).show();
- return;
- }
- //create key "user name" with value of your password
- editor.putString(userName, password1);
- //commit the changes
- editor.commit();
- Intent iLogin = new Intent(this, MainActivity.class);
- this.startActivity(iLogin);
- }
- public void btnCancel(View v)
- {
- Intent iLogin = new Intent(this, MainActivity.class);
- this.startActivity(iLogin);
- }
- }
- //===========================================ENTRY CLASS
- package com.example.com.myshared;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- public class Entry extends Activity {
- TextView txt;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.entry);
- txt=(TextView)findViewById(R.id.text);
- txt.setText("Hello " + getIntent().getStringExtra("putUser"));
- }
- public void btnLogout(View v)
- {
- //declaration of shared preferences
- SharedPreferences pref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- //declaration of editor
- SharedPreferences.Editor editor=pref.edit();
- //remove key user and pass
- editor.remove("userLogged");
- //commit changes
- editor.commit();
- //start another window - main window
- Intent iMain = new Intent(this,MainActivity.class);
- this.startActivity(iMain);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement