相关疑难解决方法(0)

检查应用程序是否首次运行

我是Android开发新手,我想在安装后首先根据应用程序设置一些应用程序的属性.有没有办法找到应用程序第一次运行,然后设置其第一个运行属性?

android

94
推荐指数
3
解决办法
9万
查看次数

Room Database强制OnCreate回调

我正在使用RoomDatabase开发一个需要预先填充其数据的应用程序; 我已经设法通过添加onCreate()回调来实现它,但只有在第一次访问数据库时才会调用它(比如调用其中一个Daos函数).

有没有办法在不进行任何读或写操作的情况下强制创建数据库?

这是我的代码,MyDatabase.get()被称为App.onCreate()

@Database(entities = {Entity1.class, Entity2.class}, version = 1, exportSchema = true)
public abstract class MyDatabase extends RoomDatabase {

    private static MyDatabase sInstance;

    public synchronized static TaxCodeDatabase get(Context context) {
        if (sInstance == null) {
            sInstance = buildDatabase(context);
        }
        return sInstance;
    }

    private static MyCodeDatabase buildDatabase(final Context context) {
        return Room.databaseBuilder(context,
                MyCodeDatabase.class,
                "my-database")
                .addCallback(new Callback() {
                    @Override
                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
                        super.onCreate(db);
                        sInstance.preFillData(context);
                      });
                    }
                })
                .build();
    }

    public abstract Entity1Dao …
Run Code Online (Sandbox Code Playgroud)

java android android-room

10
推荐指数
2
解决办法
2917
查看次数

使用 Hilt 预填充 Room 数据库,无需创建额外的数据库实例

我试图确保我的数据库始终包含初始行。我通读了如何在首次运行时填充 Android Room 数据库表?我遇到的主要问题是,在创建数据库时,我没有可以使用 Hilt 访问的实例(或者我不知道如何访问它?)。如果我尝试重新使用provideDatabase我编写的 Hilt 方法,则会导致 SQLite 数据库泄漏(大概是因为周围没有人使用这些生成的实例来关闭数据库)。这是我的代码:

@Module
@InstallIn(ApplicationComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun provideDatabase(@ApplicationContext context: Context): GameDatabase {
        return Room.databaseBuilder(context, GameDatabase::class.java, GameDatabase.GAME_DB_NAME)
            .addCallback(
                object : RoomDatabase.Callback() {
                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)
                        // Initialize the database with the first game
                        ioThread {
                            provideDatabase(context).gameDao().createNewGame(Game())
                        }
                    }

                    override fun onOpen(db: SupportSQLiteDatabase) {
                        super.onOpen(db) 

                        // Ensure there is always one game in the database
                        // This will capture the case of the app …
Run Code Online (Sandbox Code Playgroud)

android android-room dagger-hilt

3
推荐指数
1
解决办法
1848
查看次数

How to pre-populate Room database at first run?

I am creating an android app in java that uses room SQLite to store data. The app have two tables:

  • CoursesTable
  • StudentsTable

I want to add default values for course_name entity so when the user open the app find the courses.

For example :

Math - Sport - Art - Music

@Entity
public class CoursesTable {
   @PrimaryKey(autoGenerate = true)
   int course_id;
   String course_name;
}
Run Code Online (Sandbox Code Playgroud)

sql android android-room

1
推荐指数
1
解决办法
351
查看次数

标签 统计

android ×4

android-room ×3

dagger-hilt ×1

java ×1

sql ×1