运行code只有一次的Android [英] Run Code Only Once in Android
本文介绍了运行code只有一次的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要运行一块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 preferences
For 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();
}
这篇关于运行code只有一次的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文