将数据从Android中的wakeful intent服务插入数据库
在Android中使用数据库需要一些帮助 我正在尝试从将数据从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
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李>