android - ContentProvider的OnCreate返回false会怎样?

标签 android android-contentprovider

文档指出,如果提供程序加载成功,我们应该返回 true,否则返回 false。在我的实现中,如果 DatabaseHelper == null,我将返回 false。

假设现在 DatabaseHelper == null 并且在 onCreate 中返回了 false ,稍后在代码中的某个地方查询提供者,提供者仍在被查询并且因为它会崩溃。

我的问题是ContentProvider的OnCreate返回false有什么用? 在创建失败后我应该如何处理查询?只需再次运行查询中的 onCreate 吗?

最佳答案

what is the use to return false in OnCreate of ContentProvider?

通过快速浏览 Android 源代码,我发现,就目前而言,您返回什么并不重要,它只是被忽略,再次至于现在

在测试和 ActivityThread 中,attachInfonewInstance 之后被调用,所以如果你查看 ContentProvider 来源 at line 1058是调用 onCreate 的地方,看起来像:

/**
 * After being instantiated, this is called to tell the content provider
 * about itself.
 *
 * @param context The context this provider is running in
 * @param info Registered information about this content provider
 */
public void attachInfo(Context context, ProviderInfo info) {
    /*
     * We may be using AsyncTask from binder threads.  Make it init here
     * so its static handler is on the main thread.
     */
    AsyncTask.init();

    /*
     * Only allow it to be set once, so after the content service gives
     * this to us clients can't change it.
     */
    if (mContext == null) {
        mContext = context;
        mMyUid = Process.myUid();
        if (info != null) {
            setReadPermission(info.readPermission);
            setWritePermission(info.writePermission);
            setPathPermissions(info.pathPermissions);
            mExported = info.exported;
        }
        ContentProvider.this.onCreate();
    }
}

请记住,如果文档这么说,谁知道呢,也许这将在未来的版本中使用/修复。


how should I handle for the query after a fail onCreate? just run again the onCreate in query?

我会说是的,不一定 onCreate 但你自己的方法初始化一次并确保你的 DatabaseHelper 左右,这将是你最大的努力,我的意思是根据onCreate

文档

You should defer nontrivial initialization (such as opening, upgrading, and scanning databases) until the content provider is used

所以从技术上讲,您会按预期进行,但那里很荒凉,所以要注意安全。

关于android - ContentProvider的OnCreate返回false会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13813598/

相关文章:

javascript - Cordova console.log 不工作

android - CursorLoader 不显示数据库条目

java - Android 内容解析器删除 - 更新状态

android - 使用 LoaderManager 和 CursorAdapter 的 CRUD

android - 更新 Android CalendarProvider 中的 ExDate 列后未重新生成实例表

android - MVI 与 MVC 和 MVVM 相比有什么区别

android - 从 Android WebView 中启动 map

java - 如何通过 OnItemClickListener 使 imageView 可点击?

java - 嵌套的 Do-While 循环通过游标进行索引 - 不起作用

安卓短信: group by its thread id