你能告诉我,如何在第一次运行时知道应用程序,因为我有一些属性只需要一次....但我不知道用什么方法做到这一点? 谢谢大家!
答案 0 :(得分:2)
这很简单,真的。使用SharedPreferences
。例如:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean("first-time", true)) {
// do something, the app is being launched for the first time!
prefs.edit().setBoolean("first-time", false).commit(); // record the fact that the app has been started at least once
}
例如,您可以将此代码段放在主要活动的onCreate()
方法中。
答案 1 :(得分:1)
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefEditor = settings.edit();
int runCount = 0;
if(settings.contains("RunCount")) { // your app run before
// this many times:
runCount = settings.getInt("RunCount", 0);
}
prefEditor.putInt("RunCount", ++runCount);
prefEditor.commit();