我的Android应用程序中有后台服务,应该在应用程序的数据库中的特定日期属性前一天通知用户。我知道如何执行事务并通知用户,但我无法从服务访问领域对象。
有人可以帮忙吗?提前致谢。
答案 0 :(得分:0)
Realm提供了一种从任何地方访问您的实例的方法。
// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
//(Recommended to add this code in your Application or first Activity)
RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).build();
Realm.setDefaultConfiguration(realmConfig);
//then call it from your service
Realm realm = Realm.getInstance(this);
//access your realm object
realm.beginTransaction();
final Dog managedDog = realm.copyToRealm(dog); // Persist unmanaged objects
Person person = realm.createObject(Person.class); // Create managed objects directly
person.getDogs().add(managedDog);
realm.commitTransaction();