运行一段code只有一次在安装应用程序时 [英] Run a piece of code only once when an application is installed

查看:164
本文介绍了运行一段code只有一次在安装应用程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序运行的一块code只有一次,是当我运行它的第一次(新安装的应用程序)。我怎么能做到这一点,任何人都可以解释给一张code。

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.

其实,在我的Andr​​oid项目,我想创建数据库,并只在第一次运行时插入一些值。在此之后,该特定一块code不应该再运行。我怎样才能实现通过的共享preferences 或此机制的 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.

样品code会更有帮助。

Sample code will be more helpful.

推荐答案

在所有可以使用的 SQLiteOpenHelper 。这是preferred的方式做事情的数据库。这个类有一个的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启动点。如果只有单一的活动中,你可以把你的code。在的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 runned.
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
  }
}

不要忘了把活动申报清单中,以及它的 intentfilters (行动= ,类别= LAUNCHER )。

Don't forget to put activity declaration in manifest, as well as it's intentfilters (action = MAIN, category = LAUNCHER).

如果你有一个以上的活动,你不想重复你的启动逻辑,你可以把你的初始化逻辑中的应用实例,即所有活动(和其他组件,如服务,广播recievers之前创建的,内容提供商)。

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 runned.
      SharedPreferences.Editor editor = prefs.edit();
      editor.putBoolean("firstTime", true);
      editor.commit();
    }
}

所有你需要的工作,放在应用程序标记在AndroidManifest.xml中属性机器人:应用程序名称=

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>

我希望这有助于。

I hope this helps.

抱歉的英文不好。

这篇关于运行一段code只有一次在安装应用程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆