Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据从Android中的wakeful intent服务插入数据库_Android_Database_Google Cloud Messaging_Background Service - Fatal编程技术网

将数据从Android中的wakeful intent服务插入数据库

将数据从Android中的wakeful intent服务插入数据库,android,database,google-cloud-messaging,background-service,Android,Database,Google Cloud Messaging,Background Service,在Android中使用数据库需要一些帮助 我正在尝试从doWakefulWork()内部WakefulIntentService向MoEngage提供的数据库中插入数据 下面是我将数据插入数据库的函数: private void addToDb(Bundle extras) { String msgDetails = "my string"; ContentValues values = new ContentValues(); if (null != msgDetail

在Android中使用数据库需要一些帮助

我正在尝试从
doWakefulWork()
内部
WakefulIntentService
MoEngage
提供的数据库中插入数据

下面是我将数据插入数据库的函数:

private void addToDb(Bundle extras) {
    String msgDetails = "my string";
    ContentValues values = new ContentValues();
    if (null != msgDetails) {
        values.put("msg", msgDetails);
    }

    Uri newRecord = App.context.getContentResolver().insert(MoEDataContract.MessageEntity.getContentUri(App.context),
            values);
    App.context.getContentResolver().notifyChange(newRecord, (ContentObserver) null);

    if (MoEHelperUtils.isDebugEnabled()) {
        if (null != newRecord) {
            Log.d(MoEHelper.TAG, "PushMessagingListener: added new record with entry: " + newRecord);
        } else {
            Log.d(MoEHelper.TAG, "PushMessagingListener: FAILED to add new record with entry: ");
        }
    }
}
但是,当我打开应用程序并尝试查询数据库时,光标返回0 count

如果仅在
doWakefulWork()
中添加值后立即尝试查询,则光标将显示数据库中的值

Cursor cursor = MoEController.getAllMessages(App.context);
我是不是错过了什么?任何帮助都将不胜感激。谢谢

请看一看


看起来您应该创建一个事务并将其标记为成功。

以下是有关如何通过ContentValue在Android上插入数据的示例:

  • -若要将数据插入提供程序,请调用ContentResolver.insert()方法。此方法将新行插入到提供程序中,并返回该行的内容URI
上下文可以为null,也可以不为null。我发现了这种可能性,因为在关于在中插入数据的示例代码中,上下文为空。因此,我假设“应用程序”可能使用单例模式

你可以看看这个

如果内容提供者使用SQLiteOpenHelper,这将不起作用,因为内容提供者的onCreate()在应用程序的onCreate()之前被调用