【发布时间】:2016-08-08 15:29:01
【问题描述】:
我正在扩展 NotificationListenerService 上进行领域插入,如下所示:
public class NLService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// building realmObject here
mRealm = Realm.getDefaultInstance();
RealmHelper.saveObj(myRealmObject, mRealm);
// mRealm.waitForChange(); / mRealm.refresh();
mRealm.close();
}
}
public class RealmHelper {
public static RealmModel saveObj(RealmObject realmObject, Realm realm) {
realm.beginTransaction();
RealmObject newRealmObject = realm.copyToRealm(realmObject);
realm.commitTransaction();
return newRealmObject;
}
}
使用比 v0.88.3 更新的 Realm,如果在 NLService 中插入任何内容,则不会调用单个 RealmChangeListener (rcl)。
我尝试将 rcl 直接附加到 Realm、RealmResults 和 RealmObject,但没有任何效果。
该应用程序具有例如简单的 rcl 用于 RealmResults<myRealmObject>.size() 和
几个 RecyclerAdapter 和 RealmRecyclerViewAdapter 中的 rcl 永远不会被调用。
但是,重新运行查询会起作用,并且会显示“丢失的数据”。 此外,如果在 ui- 或任何其他线程上插入任何内容,则会调用 rcl 并显示“丢失数据”。
我在 Realm 0.88.3 上呆了几个月,因为我无法将它用于任何较新的 Realm 版本。在 NLService 中调用了 0.88.3 mRealm.refresh();,这在较新的版本中不可用,.waitForChange 无休止地阻塞。
Manifest.xml:
<service
android:name=".service.NLService"
android:label="@string/nl_service"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
【问题讨论】:
-
您在哪里尝试添加
RealmChangeListener?您是否偶然知道NotificationListenerService是在后台线程(非looper)、后台线程(带looper)还是在UI 线程上运行? -
最大的问题是,
NotificationListenerService是如何在您的 AndroidManifest.xml 中声明的? -
我对 NotificationListenerService 不太熟悉,但如果它是在单独的进程中调用的,那就是它不起作用的原因。
-
@EpicPandaForce 查看编辑。所有 RealmChangeListener 都在 UI 线程上。