在我的应用程序中,我已经实现了密码屏幕,但是需要运行检查以查看是否应显示活动A或活动B,并且此检查显然是共享首选项的结果,但是,打开时会出现明显的延迟应用程序 - 代码可以在下面看到,但是如何优化它以使其更快地打开并且没有滞后?
启动器活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean loggedIn = PreferenceUtils.getBoolean(LauncherActivity.this, PreferenceUtils.LOGGED_IN_STATE);
boolean passwordEnabled = PreferenceUtils.getBoolean(LauncherActivity.this, PreferenceUtils.APPLICATION_PASSWORD_ENABLED);
Log.d(TAG, "LOGGED IN: " + loggedIn + " " + "PW: " + passwordEnabled);
if (!loggedIn) {
// Start Login Activity
Intent intent = new Intent(LauncherActivity.this, ApplicationIntro.class);
startActivity(intent);
//PreferenceUtils.clearPreferences(LauncherActivity.this);
} else {
if (passwordEnabled) {
// Start Password Activity
Intent intent = new Intent(LauncherActivity.this, PasswordActivity.class);
startActivity(intent);
} else {
// Start Main Activity
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
startActivity(intent);
}
}
finish();
}
密码活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
setupActionBar();
setupPreferences();
findViews();
setupValues();
setListeners();
}
...
private void setupPreferences() {
attemptsRemaining = PreferenceUtils.getInt(PasswordActivity.this, PreferenceUtils.APPLICATION_PASSWORD_ATTEMPTS);
storedPassword = PreferenceUtils.getString(PasswordActivity.this, PreferenceUtils.APPLICATION_PASSWORD);
duressPassword = PreferenceUtils.getString(PasswordActivity.this, PreferenceUtils.DURESS_PASSWORD);
}
偏好设施
public static void saveString(Context context, String key, String value) {
SharedPreferences preferences = new SecurePreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
public static String getString(Context context, String key) {
SharedPreferences preferences = new SecurePreferences(context);
return preferences.getString(key, "");
}
public static void saveBoolean(Context context, String key, boolean value) {
SharedPreferences preferences = new SecurePreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBoolean(Context context, String key) {
SharedPreferences preferences = new SecurePreferences(context);
return preferences.getBoolean(key, false);
}
答案 0 :(得分:1)
Check阻止了UI线程,这就是应用程序滞后的原因。使用AsyncTask
here is a link to AsyncTask documentation在后台线程中执行lagy过程是一个很好的做法。
随意询问进一步的询问。