0

在我的应用程序中,我已经实现了一个密码屏幕,但是需要运行检查以查看是否应显示活动A或活动B,并且此检查显然是共享首选项的结果,打开应用程序时会出现明显的延迟 - 代码可以在下面看到,但是如何优化它以便更快地打开并且没有延迟?在UI线程上Android加载共享首选项

启动活动

@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); 
} 

偏好的Utils

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); 
} 

回答

1

Ť他检查阻止UI线程,这就是为什么应用程序滞后。在后台使用AsyncTaskhere is a link to AsyncTask documentation在后台执行lagy进程是一种很好的做法。 随意问更多的疑问。

+0

谢谢你的回复。你能提供一些你如何建议这样做的代码吗?由于我以前使用AsyncTask来加载这些值,虽然速度更快,但在密码活动中,用户实际上可以看到由于onPostExecute而更新的内容。如果你因此可以建议一些关于如何建议这样做的代码,那么这很好 - 谢谢。另外,Launcher Activity中的onCreate是该文件中唯一的代码,因为它没有视图,而PasswordActivity有一个视图(用户输入密码和TextView的EditText) –

+0

app.when app的流程是什么首先启动哪个活动,然后再启动! –

+0

启动器活动>共享首选项检查以查看是否显示介绍活动或密码活动>如果密码活动>共享首选项获取保存的密码以检查用户的输入 –