【问题标题】:Android load Shared Preferences on UI threadAndroid 在 UI 线程上加载共享首选项
【发布时间】:2017-10-04 22:42:34
【问题描述】:

在我的应用程序中,我实现了密码屏幕,但需要运行检查以查看是否应显示 Activity A 或 Activity 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);
}

【问题讨论】:

    标签: java android android-intent android-activity android-sharedpreferences


    【解决方案1】:

    Check 阻塞了 UI 线程,这就是应用滞后的原因。使用AsyncTaskhere is a link to AsyncTask documentation 在后台线程中执行延迟进程是一个好习惯。 请随时提出更多问题。

    【讨论】:

    • 感谢您的回复。你能提供一些你会如何建议这样做的代码吗?正如我过去使用 AsyncTask 来加载这些值,虽然它更快,但在密码活动中,用户实际上可以看到由于 onPostExecute 而更新的内容。因此,如果您可以建议一些有关如何建议这样做的代码,那就太好了-谢谢。此外,Launcher Activity 中的 onCreate 是该文件中唯一的代码,因为它没有视图,而 PasswordActivity 有一个视图(用户输入密码的 EditText 和 TextView)
    • app的流程是什么,app启动时先启动哪个activity再启动!
    • Launcher Activity > Shared Preference 检查是否显示 Intro Activity 或 Password Activity > 如果 Password Activity > Shared Preference 获取保存的密码以检查用户的输入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    相关资源
    最近更新 更多