The realization of Android automatic login function

Landing page layout design:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/account" />

        <EditText
            android:id="@+id/edtaccount"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password" />

        <EditText
            android:id="@+id/edtpassword"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:singleLine="true" />
    </LinearLayout>

    <Button
        android:id="@+id/btnlogin"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/login" />

 Logout page layout design:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/logout page"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btncancel"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/cancel" />

 LoginActivity.java:

package com.xiaoyan.autologin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	// define the component
	private EditText edtAccount;
	private EditText edtPassword;
	private Button btnLogin;

	// used to record account and password
	private String strAccount = "";
	private String strPassword = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.login_main);

		// set the title
		setTitle("Login");

		// Get the sharedpreferences object
		SharedPreferences share = getSharedPreferences("Login",
				Context.MODE_PRIVATE);
		strAccount = share.getString("Account", "");
		strPassword = share.getString("Password", "");

		// Determine if you have logged in before
		if (share == null) {
			init();
		} else {
			// Determine if you have just logged out
			if (share.getBoolean("LoginBool", false)) {
				// Jump to the logout page and destroy the current activity
				Intent intent = new Intent(LoginActivity.this,
						CancelActivity.class);
				startActivity(intent);
				finish();
			} else {

				init();
			}
		}

	}

	private void init() {

		// Initialization module
		edtAccount = (EditText) findViewById(R.id.edtaccount);
		edtPassword = (EditText) findViewById(R.id.edtpassword);
		btnLogin = (Button) findViewById(R.id.btnlogin);

		edtAccount.setText (strAccount);
		edtPassword.setText(strPassword);
		
		// listen button
		btnLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Determine if the account and password are empty or not
				if (edtAccount.getText().toString().equals("")
						|| edtPassword.getText().toString().equals("")) {
					Toast.makeText(LoginActivity.this, "The account or password cannot be empty",
							Toast.LENGTH_SHORT).show();
				} else {
					// Create a SharedPreferences object to store the account and password and make it private
					SharedPreferences share = getSharedPreferences("Login",
							Context.MODE_PRIVATE);
					// Get the editor to store data in sharedpreferences
					Editor editor = share.edit();
					editor.putString("Account", edtAccount.getText().toString());
					editor.putString("Password", edtPassword.getText()
							.toString());
					editor.putBoolean("LoginBool", true);
					// Submit data to sharedpreferences
					editor.commit();

					// Jump to the logout page and destroy the current activity
					Intent intent = new Intent(LoginActivity.this,
							CancelActivity.class);
					startActivity(intent);
					finish();
				}

			}
		});
	}

}

 CancelActivity.java:

package com.xiaoyan.autologin;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class CancelActivity extends Activity {

	// define the component
	private Button btnCancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate (savedInstanceState);
		setContentView(R.layout.cancel_activity);

		// set the title
		setTitle("Cancel");
		// initialize the page
		init();

	}

	private void init() {
		// Initialization module
		btnCancel = (Button) findViewById(R.id.btncancel);

		// Listen for the logout button
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				// cancel the account and destroy the current page
				SharedPreferences share = getSharedPreferences("Login",
						Context.MODE_PRIVATE);
				share.edit().putBoolean("LoginBool", false).commit();
				
				Intent intent = new Intent(CancelActivity.this,
						LoginActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325115769&siteId=291194637