Code library/Remember password + force deprecation, Remember password in code library
1. ActivityCollector is a tool class.
2. Use SharedPreferences. Editor to save the key-Value Pair type and use SharedPreferences to retrieve the data.
3. Create a forced deprecation broadcast and add the final activity logic and applications related to the AlertDialog class.
BaseActivity
import android.content.IntentFilter;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;/** * Created by YRC on 2016/11/2. */public class BaseActivity extends AppCompatActivity { private ForceOfflineReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onResume() { super.onResume(); IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction("com.example.yrc.rememberyourpassword.FORCE_OFFLINE"); receiver=new ForceOfflineReceiver(); registerReceiver(receiver,intentFilter); } @Override protected void onPause() { super.onPause(); if (receiver!=null){ unregisterReceiver(receiver); receiver=null; } } @Override protected void onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); }}
LoginActivity
import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;/** * Created by YRC on 2016/11/2. */public class LoginActivity extends BaseActivity { private SharedPreferences pref; private SharedPreferences.Editor editor; private EditText accountEdit; private EditText passwordEdit; private CheckBox rememberPass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); pref= PreferenceManager.getDefaultSharedPreferences(this); accountEdit= (EditText) findViewById(R.id.AccountEdit); passwordEdit= (EditText) findViewById(R.id.PassportEdit); rememberPass= (CheckBox) findViewById(R.id.checkBox); Button login = (Button) findViewById(R.id.loginBtn); boolean isRemember=pref.getBoolean("remember_password",false); if (isRemember){ String account=pref.getString("account",""); String password=pref.getString("password",""); accountEdit.setText(account); passwordEdit.setText(password); rememberPass.setChecked(true); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account=accountEdit.getText().toString(); String password=passwordEdit.getText().toString(); if (account.equals("admin")&& password.equals("123456")){ editor=pref.edit(); if (rememberPass.isChecked()){ editor.putBoolean("remember_password",true); editor.putString("account",account); editor.putString("password",password); }else { editor.clear(); } editor.apply(); Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); }else { Toast.makeText(LoginActivity.this,"account or password is invalid", Toast.LENGTH_SHORT).show(); } } }); }}
ForceOfflineReceiver
Import Android. content. BroadcastReceiver;ImportAndroid. content. Context;ImportAndroid. content. DialogInterface;ImportAndroid. content. Intent;ImportAndroid. support. v7.app. AlertDialog;/*** Created by YRC on 2016/11/2.*/Public classForceOfflineReceiverExtendsBroadcastReceiver {@ OverridePublic voidOnReceive (FinalContext context, Intent intent) {AlertDialog. Builder dialogBuilder =NewAlertDialog. Builder (context); dialogBuilder. setTitle ("Warning"); DialogBuilder. setMessage ("You are force to be offLine"); DialogBuilder. setCancelable (False); DialogBuilder. setPositiveButton ("OK",NewDialogInterface. OnClickListener () {@ OverridePublic voidOnClick (DialogInterface dialog,IntWhich) {ActivityCollector. finishAll (); Intent intent =NewIntent (context, LoginActivity.Class); Context. startActivity (intent );}//Because the activity is started in the broadcast receiver, it must beIntentAdd this flag and set the dialog box type. Otherwise, it cannot be displayed in the broadcast receiver. }); DialogBuilder. show ();}}
Login. xml
<TableLayoutXmlns:Android=Https://schemas.android.com/apk/res/android" Xmlns:Tools=Https://schemas.android.com/tools" Android: Layout_width ="Match_parent" Android: Layout_height ="Match_parent" Android: StretchColumns ="1" Tools: Context ="Com. example. yrc. rememberyourpassword. MainActivity"> <TableRow Android: Layout_width ="Match_parent" Android: Layout_height ="Match_parent"> <TextView Android: Text ="Account :" Android: Layout_column ="0"/> <EditText Android: Layout_width ="Wrap_content" Android: Layout_height ="Wrap_content" Android: Hint ="Input your acccount" Android: Id ="@ + Id/AccountEdit" Android: Layout_column ="1"/>
TableRow> <TableRow Android: Layout_width ="Match_parent" Android: Layout_height ="Match_parent"> <TextView Android: Text ="Passport" Android: Layout_column ="0"/> <EditText Android: Layout_width ="Wrap_content" Android: Layout_height ="Wrap_content" Android: Id ="@ + Id/PassportEdit" Android: Layout_column ="1"/>
TableRow> <TableRow Android: Layout_width ="Match_parent" Android: Layout_height ="Match_parent"> <CheckBox Android: Text ="Remember password" Android: Id ="@ + Id/checkBox" Android: Layout_column ="0" Android: Layout_span ="2"/>
TableRow>