我完成了一个Android应用。这是一个财务应用程序,因此其他人一定不能看到所有者的财务。现在,我停止了用户锁定问题。如果用户为应用设置了用户锁,则每次启动Activity都要求输入密码。之前不是那样。仅在用户首次进入应用程序时要求输入密码。我不明白发生了什么。我找不到我的错误。
查看日志没有错误。储物柜位于导航抽屉中,并按了MainActivity。我将SwitchCompat启用和禁用。我删除了其他未提及的代码,只保留了用于用户密码的代码。
应用程序类
public class App extends Application {
private static long TIME_PAUSE = 1500L;
private long mLastPause;
private static App mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
new DbHelper(getApplicationContext()).getWritableDatabase();
this.mLastPause = (System.currentTimeMillis() - TIME_PAUSE);
FirebaseAuth.getInstance().useAppLanguage();
}
public static App getInstance()
{
return mInstance;
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
public boolean isEnterPassword()
{
return System.currentTimeMillis() - this.mLastPause >= TIME_PAUSE;
}
MainActivity
public class MainActivity extends BaseActionBar
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onStart() {
super.onStart();
if ((App.getInstance().isEnterPassword()) && (mHelper.isProtectionPassword()) && (getSupportFragmentManager().findFragmentByTag(EnterPasswordDialogFragment.class.getSimpleName()) == null)) {
showDialog(EnterPasswordDialogFragment.newInstance(this.mHelper.getPassword()));
}
}
NavigationView navigationView;
SwitchCompat vibrateSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadLocale();
setContentView(R.layout.activity_main);
dbHelper = new DbHelper(this);
initActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.locker) {
navigationView.getMenu().findItem(R.id.locker).setActionView(new SwitchCompat(this));
vibrateSwitch = (SwitchCompat) navigationView.getMenu().findItem(R.id.locker).getActionView();
vibrateSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mHelper.setProtectionPassword(isChecked);
}
});
if (!vibrateSwitch.isChecked()) {
if (!this.mHelper.isProtectionPassword()) {
showDialog(LoginActivity.newInstance(passwordListener));
}
}
vibrateSwitch.setChecked(false);
this.mHelper.setPassword("");
this.mHelper.setProtectionPassword(false);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
SystemHelper
public class SystemHelper {
public static final String settings = "settings";
private Context context;
private SystemHelper(Context context1) {
this.context = context1;
}
public static SystemHelper getInstance(Context context) {
return new SystemHelper(context);
}
public SharedPreferences getPreferenses() {
Context contextL = this.context;
return contextL.getSharedPreferences("settings", 4);
}
public void setPassword(String paramString) {
SharedPreferences.Editor localEditor = getPreferenses().edit();
localEditor.putString("key_password", paramString);
localEditor.commit();
}
public void setProtectionPassword(boolean paramBoolean) {
SharedPreferences.Editor localEditor = getPreferenses().edit();
localEditor.putBoolean("cb_protection_password", paramBoolean);
localEditor.commit();
}
public boolean isProtectionPassword() {
return getPreferenses().getBoolean("cb_protection_password", false);
}
public String getPassword() {
return getPreferenses().getString("key_password", "");
}
}