ContentObserver 没有调用
ContentObserver is not calling
ContactObserver 的 OnChange 方法不会调用,除非我 运行 第二次申请。第一个 运行 使得安装似乎不会注册观察者,即使从服务调用 onCreate 也是如此。
在第二个 运行 上,它运行完美。你可以看下面的代码。
public final class ContactSync extends Service {
private ContactObserver mObserver;
public ContactSync() {
mObserver = new ContactObserver();
}
@Override
public void onCreate() {
super.onCreate();
getContentResolver().registerContentObserver(ContactsContract.ProfileSyncState.CONTENT_URI, true, mObserver);
Logger.i("Sync service is ready");
}
@Override
public void onDestroy() {
super.onDestroy();
getContentResolver().unregisterContentObserver(mObserver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void lookup() {
RealmResults<Contact> all = Realm
.getDefaultInstance()
.where(Contact.class)
.findAll();
for (Contact proxy : all) {
Contact concrete = Realm
.getDefaultInstance()
.copyFromRealm(proxy);
Contact raw = ContactResolver
.from(getApplicationContext())
.setUri(concrete.data())
.setPosition(concrete.mPosition)
.query();
if (raw == null) {
delete(proxy);
} else {
update(raw);
}
}
publish();
}
private void publish() {
LocalBroadcastManager
.getInstance(getApplicationContext())
.sendBroadcastSync(new Intent(ContactEdge.PublishEvent.EVENT_PUBLISH));
Logger.i("Published contacts");
}
private void update(@NonNull final Contact contact) {
Realm
.getDefaultInstance()
.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(contact);
Logger.json(contact);
}
});
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutInfo shortcut = new ShortcutInfo.Builder(
getApplicationContext(), contact.getId())
.setShortLabel(contact.getShortLabel())
.setLongLabel(contact.getLongLabel())
.setIcon(contact.getIcon(getApplicationContext()))
.setIntent(contact.getIntent())
.build();
ShortcutManager manager = getSystemService(ShortcutManager.class);
manager.addDynamicShortcuts(Collections.singletonList(shortcut));
}
Logger.i("Updated contact");
}
private void delete(@NonNull final Contact contact) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager manager = getSystemService(ShortcutManager.class);
manager.removeDynamicShortcuts(Collections.singletonList(contact.getId()));
}
Realm
.getDefaultInstance()
.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
contact.deleteFromRealm();
}
});
Logger.i("Deleted contact");
}
final class ContactObserver extends ContentObserver {
ContactObserver() {
super(new Handler(Looper.getMainLooper()));
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
lookup();
}
}
我的申请class:
public final class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
Realm.init(this);
startContactSync();
}
private void startContactSync() {
Intent service = new Intent(this, ContactSync.class);
startService(service);
}
Manifest 文件的 application 元素中的 xml 元素。
<service
android:name=".ui.edge.contact.ContactSync"
android:enabled="true"
android:exported="false"/>
一切似乎都很好,可能是您缺少权限?
ContactObserver 的 OnChange 方法不会调用,除非我 运行 第二次申请。第一个 运行 使得安装似乎不会注册观察者,即使从服务调用 onCreate 也是如此。
在第二个 运行 上,它运行完美。你可以看下面的代码。
public final class ContactSync extends Service {
private ContactObserver mObserver;
public ContactSync() {
mObserver = new ContactObserver();
}
@Override
public void onCreate() {
super.onCreate();
getContentResolver().registerContentObserver(ContactsContract.ProfileSyncState.CONTENT_URI, true, mObserver);
Logger.i("Sync service is ready");
}
@Override
public void onDestroy() {
super.onDestroy();
getContentResolver().unregisterContentObserver(mObserver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void lookup() {
RealmResults<Contact> all = Realm
.getDefaultInstance()
.where(Contact.class)
.findAll();
for (Contact proxy : all) {
Contact concrete = Realm
.getDefaultInstance()
.copyFromRealm(proxy);
Contact raw = ContactResolver
.from(getApplicationContext())
.setUri(concrete.data())
.setPosition(concrete.mPosition)
.query();
if (raw == null) {
delete(proxy);
} else {
update(raw);
}
}
publish();
}
private void publish() {
LocalBroadcastManager
.getInstance(getApplicationContext())
.sendBroadcastSync(new Intent(ContactEdge.PublishEvent.EVENT_PUBLISH));
Logger.i("Published contacts");
}
private void update(@NonNull final Contact contact) {
Realm
.getDefaultInstance()
.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(contact);
Logger.json(contact);
}
});
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutInfo shortcut = new ShortcutInfo.Builder(
getApplicationContext(), contact.getId())
.setShortLabel(contact.getShortLabel())
.setLongLabel(contact.getLongLabel())
.setIcon(contact.getIcon(getApplicationContext()))
.setIntent(contact.getIntent())
.build();
ShortcutManager manager = getSystemService(ShortcutManager.class);
manager.addDynamicShortcuts(Collections.singletonList(shortcut));
}
Logger.i("Updated contact");
}
private void delete(@NonNull final Contact contact) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager manager = getSystemService(ShortcutManager.class);
manager.removeDynamicShortcuts(Collections.singletonList(contact.getId()));
}
Realm
.getDefaultInstance()
.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
contact.deleteFromRealm();
}
});
Logger.i("Deleted contact");
}
final class ContactObserver extends ContentObserver {
ContactObserver() {
super(new Handler(Looper.getMainLooper()));
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
lookup();
}
}
我的申请class:
public final class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
Realm.init(this);
startContactSync();
}
private void startContactSync() {
Intent service = new Intent(this, ContactSync.class);
startService(service);
}
Manifest 文件的 application 元素中的 xml 元素。
<service
android:name=".ui.edge.contact.ContactSync"
android:enabled="true"
android:exported="false"/>
一切似乎都很好,可能是您缺少权限?