gpt4 book ai didi

android - 使用 sqlitecursorloader 从 intentservice 插入到 sqlite

转载 作者:行者123 更新时间:2023-11-29 00:21:43 24 4
gpt4 key购买 nike

我正在使用来自 commonsware 的 sqlitecursorloader 库。我在 Intentservice 中收到 GCM 消息,并想在 sqlite 数据库中插入一个条目。如果应用程序打开,它应该更新 ListView 。

我尝试在加载程序初始化的 Activity 中使用它:

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 开发的新手。

最佳答案

Loader 放在静态数据成员中是不好的,对于任何 Loader,更不用说 SQLiteCursorLoader,因为你会泄漏你的 Activity

我现在也正式停止使用 SQLiteCursorLoader

或者:

  • 切换到使用 ContentProvider 和常规 CursorLoader,或者

  • 使用事件总线(LocalBroadcastManager、Square 的 Otto、greenrobot 的 EventBus 等)让您的服务通知您的 UI 层根据您的更改自行刷新

    <

关于android - 使用 sqlitecursorloader 从 intentservice 插入到 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22559944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com