Android 中的变量,即使在应用程序销毁后仍然存在

Variable in Android which stays even after app destroys

我需要一个在应用程序关闭后不会被销毁的变量。 例如。我想确定,用户之前是否启动过该应用程序,或者这是第一次。 如果用户这样做,则变量定义为 1,如果用户首次启动应用程序,则变量定义为 0。

那么有什么方法可以在应用关闭后保留变量及其值吗?

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

像这样保存变量

sharedpreferences.edit().putBoolean("is_first_time",false).apply()

并在下次检索相同的变量

Boolena isFirstTime = sharedpreferences.getBoolean("is_first_time",true).apply()