安装应用程序时只运行一段代码 [英] Run a piece of code only once when an application is installed
问题描述
我只想在我的应用程序中运行一段代码,并且是在我第一次运行它时(新安装的应用程序).我怎么能做到这一点,谁能解释给一段代码.
I want to run a piece of code only once in my application and is when i run it for the first time (newly installed app). How could i do this, can anyone explain giving a piece of code.
实际上,在我的 android 项目中,我只想在第一次运行时创建数据库并插入一些值.之后,该特定代码段不应再次运行.我如何通过SharedPreferences 或Preferences 实现这种机制.
Actually, in my android project i want to create database and insert some values on the first run only. After that, that particular piece of code should not run again. How can i achieve this mechanism through SharedPreferences or Preferences.
示例代码会更有帮助.
推荐答案
在所有你可以使用之前 SQLiteOpenHelper.这是用数据库做事的首选方式.这个类有一个 onCreate(SQLiteDatabase)
方法,在第一次创建数据库时调用.我觉得它很适合你.
Before all you can use SQLiteOpenHelper. It is preferred way to do things with database. This class have a onCreate(SQLiteDatabase)
method, that called when first creating database. I think it suits you well.
如果您想要更大的灵活性并且您的第一次逻辑不仅仅与数据库绑定,您可以使用之前提供的示例.你只需要把它放在启动点.
If you want more flexibility and your first time logic is not tied only with database, you can use sample provided earlier. You just need to put it in startup spot.
有 2 个启动点.如果你只有一个活动,你可以把你的代码放在 onCreate
方法中,所以它会是这样的:
There are 2 startup spots. If you have only single activity, you can put your code in onCreate
method, so it will be like this:
public void onCreate(Bundle savedInstanceState) {
// don't forget to call super method.
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("firstTime", false)) {
// <---- run your one time code here
databaseSetup();
// mark first time has ran.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
}
不要忘记将 活动声明放在清单中,以及intentfilters(动作 = MAIN
,类别 = LAUNCHER
).
Don't forget to put activity declaration in manifest, as well as it's intentfilters (action = MAIN
, category = LAUNCHER
).
如果您有多个活动并且您不想复制您的启动逻辑,您可以将您的初始化逻辑放在应用程序实例中,该实例在所有活动(以及其他组件,例如服务、广播接收器、内容提供者).
If you have more than one activity and you don't want to duplicate your startup logic you can just put your initialization logic in Application instance, that is created before all activities (and other components, such as services, broadcast recievers, content providers).
像这样创建类:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("firstTime", false)) {
// <---- run your one time code here
databaseSetup();
// mark first time has ran.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
}
要让此工作正常运行,您只需将其放在 AndroidManifest.xml 属性 android:name=".App" 中的 application
标记中.
All you need for this to work, is put in application
tag in AndroidManifest.xml attribute android:name=".App".
<!-- other xml stuff -->
<application ... android:name=".App">
<!-- yet another stuff like nextline -->
<activity ... />
</application>
这篇关于安装应用程序时只运行一段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!