Android 使用sqlitecursorloader从intentservice插入sqlite
我正在使用Commonware中的sqlitecursorloader库。我在Intentservice中收到GCM消息,并希望在sqlite数据库中插入一个条目。如果应用程序已打开,则应更新listview 我尝试在加载程序初始化的活动中使用此选项:Android 使用sqlitecursorloader从intentservice插入sqlite,android,sqlite,intentservice,commonsware-cwac,Android,Sqlite,Intentservice,Commonsware Cwac,我正在使用Commonware中的sqlitecursorloader库。我在Intentservice中收到GCM消息,并希望在sqlite数据库中插入一个条目。如果应用程序已打开,则应更新listview 我尝试在加载程序初始化的活动中使用此选项: public static void createDbEntry(String Title, String Message) { ContentValues values=new ContentValues(2); values
public static void createDbEntry(String Title, String Message) {
ContentValues values=new ContentValues(2);
values.put(DatabaseHelper.TITLE, Title);
values.put(DatabaseHelper.MESSAGE, Message);
loader.insert("constants", DatabaseHelper.TITLE, values);
}
在intentservice中:
MainActivity.createDbEntry(title,message);
据我所知,这在大多数情况下都是有效的,但如果加载程序被回收,我会得到一个空指针
我应该在intentservice中初始化一个新的加载程序吗
请帮助,我是android开发新手。在静态数据成员中放置
加载程序
,对于任何加载程序
,都是不好的,更不用说SQLiteCursorLoader
,因为你会泄露你的活动
我现在也正式停止了SQLiteCursorLoader
要么:
- 切换到使用
和常规ContentProvider
,或CursorLoader
- 使用事件总线(
、Square的Otto、greenrobot的EventBus等)让您的服务通知您的UI层根据您的更改进行自我刷新LocalBroadcastManager
成为静态的。我建议的两个备选方案,如果实现正确,应该不会导致泄漏。我如何使用DatabaseHelper类直接插入数据库?@Lorof:CallgetWriteableDatabase()
,并使用insert()
或execSQL()
。游标加载程序用于加载列表视图,而不是加载数据库。您可以在asynctask中进行插入。