删除数据库后 ContentProvider 未调用 onCreate

ContentProvider not called onCreate after deleting database

我创建了 ContentProvider,它会在应用程序启动时创建一个 数据库

现在在该应用程序中,我正在执行当用户从应用程序注销时删除数据库的过程。

之后,当我再次登录时,ContentProvider 无法调用覆盖的 class.

onCreate()

有什么方法可以使用 ContentProvider 重新创建数据库吗?

我找到了解决方案,

首先我提到了 Refresh/Reload database reference in custom ContentProvider after restore 但对答案不满意,因为它只是为了关闭数据库。

所以我创建了如下答案:

DBHelper.java

/**
 * Delete database
 */
public static void reCreateDatabase(Context mContext) {
    ContentResolver resolver = mContext.getContentResolver();
    ContentProviderClient client = resolver.acquireContentProviderClient(KOOPSContentProvider.AUTHORITY);

    assert client != null;
    KOOPSContentProvider provider = (KOOPSContentProvider) client.getLocalContentProvider();

    assert provider != null;
    provider.resetDatabase();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        client.close();
    else
        client.release();

    LOGD("Database Deleted...");
}

public void removeDatabase(Context mContext) {
    mContext.deleteDatabase(DATABASE_NAME);
    LOGD("Database Deleted...");
}

KOOPSContentProvider.java

public void resetDatabase() {
    if(dbHelper != null) {
        dbHelper.removeDatabase(getContext());
        dbHelper = new DbHelper(getContext());
    } else {
        LOGD("Database NULL");
    }
}

用作:

DbHelper.reCreateDatabase(mContext);

谢谢 :) :)