1.使用了工具类ActivityCollector
2.使用SharedPreferences.Editor对数据进行键值对类型的保存,使用SharedPreferences取出数据
3.新建强制下线广播,并添加终结活动逻辑,以及AlertDialog类的相关应用
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; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AlertDialog; /** * Created by YRC on 2016/11/2. */ public class ForceOfflineReceiver extends BroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder dialogBuilder=new AlertDialog.Builder(context); dialogBuilder.setTitle("Warning"); dialogBuilder.setMessage("You are force to be offLine"); dialogBuilder.setCancelable(false); dialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { ActivityCollector.finishAll(); Intent intent = new Intent(context,LoginActivity.class); context.startActivity(intent); }//由于是在广播接收器里启动活动,所以必须为Intent加入这个标志,并设置对话框类型,否则无法在广播接收器里弹出 }); dialogBuilder.show(); } }
login.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://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="记住密码" android:id="@+id/checkBox" android:layout_column="0" android:layout_span="2" /> </TableRow>