我要运行一块code一次性将应用程序安装后。在这之后的特定部分的code不应该得到甚至升级调用。
我怎样才能做到这在Android中?
解决方案 检查布尔X是真正的共享preferences 如果不是: 一个。运行特殊的code 湾保存的X共享preferences为真例如:
共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
如果(!prefs.getBoolean(firstTime,FALSE)){
//运行一次code
共享preferences.Editor编辑器= prefs.edit();
editor.putBoolean(firstTime,真正的);
editor.commit();
}
I want to run a piece of code once only after the application gets installed. After that that particular piece of code should not get called even for an upgrade.
How can i do this in Android?
解决方案 Check if boolean X is True in shared preferences If not: a. Run the special code b. Save x as true in shared preferencesFor example:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
// run your one time code
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}