在应用程序卸载时删除会议室数据库 [英] Remove room database on app uninstall

查看:86
本文介绍了在应用程序卸载时删除会议室数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,并且正在使用Android Room Persistence Library处理我的数据库层. Room Library的运作就像魅力一样,一切都很好.但是我希望在用户卸载应用程序时删除该机房创建的数据库.我尝试卸载该应用程序,然后再次安装,但是不知何故数据库仍然存在,并且应用程序能够从中获取旧数据.

I am making an app and I am using Android Room Persistence Library to handle my database layer. Room Library works like charm and everything is fine with it. But I want the database that room creates to be removed when the user uninstall the app. I tried uninstalling the app and then installed again, but somehow the database was still there and app was able to get the old data from it.

我想也许是因为我的应用程序数据备份在设置中设置为自动,而android正在将其备份到云中并再次恢复,但是从设置中关闭备份无济于事.即使这种方法对我来说听起来也不是一个好的解决方案.

I thought maybe because my app data backup is set to auto in the settings and android is backing it up on cloud and bringing it back again but turning off backup from settings didn't help. Even if that worked that doesn't sound like a good solution to me.

我创建了一个非常简单的类,该类扩展了RoomDatabase,下面是有助于回答问题的代码.

I have created a very simple class that extends RoomDatabase and below is the code if that helps answering the question.

我知道我可以在数据库构建器上使用fallbackToDestructiveMigration()并增加数据库版本.它将清除数据库中的数据.那不是我想要的.

I know that I can use fallbackToDestructiveMigration() on the database builder and increase the database version. It will clear the data from the database. That is not what I want.

@Database(entities = {UnleashedEntity.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract DaoContract MyDao();

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
                            .build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

修改 我知道我可以使用ACTION_PACKAGE_REMOVED意图使我的应用知道卸载的情况.我想知道的是,是否有databasebuilder的配置可以完成这项工作,以及应用卸载后数据库如何继续存在.

Edit I know I can use ACTION_PACKAGE_REMOVED intent to make my app aware of the uninstall. What I wanted to know, Is there a configuration of the databasebuilder that does the job, and How come the database persists after app uninstall.

推荐答案

是否有完成该工作的databasebuilder配置

Is there a configuration of the databasebuilder that does the job

AFAIK会议室不知道应用程序如何卸载,因此Room可能不会在卸载时为您擦除数据库.

AFAIK room does not know about app getting uninstall so Room probably won't wipe the database for you on uninstall.

应用卸载后数据库如何持续存在

How come the database persists after app uninstall

从Android 6.0(API级别23)开始,Android提供自动备份功能(默认情况下处于启用状态).根据文档,它将以下内容备份到Google驱动器:

Beginning with Android 6.0 (API level 23), Android offers the Auto Backup feature which is enabled by default. According to the documentations, it backup the following to google drive:

  • 共享的首选项文件.
  • 保存到应用程序内部存储中的文件,可以通过getFilesDir()或getDir(String,int)访问.
  • getDatabasePath(String)返回的目录中的文件,其中还包括使用SQLiteOpenHelper类创建的文件.
  • getExternalFilesDir(String)返回的目录中外部存储上的文件.

这也备份了您的会议室数据库.

This backup your room database too.

如何禁用自动备份

在您的清单中

<manifest ... >
    ...
    <application android:allowBackup="false" ... >
        ...
    </application>
</manifest>

这篇关于在应用程序卸载时删除会议室数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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