gpt4 book ai didi

android - ContentObserver 没有调用

转载 作者:行者123 更新时间:2023-11-30 00:33:59 27 4
gpt4 key购买 nike

除非我第二次运行应用程序,否则不会调用 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();
}
}

我的应用类:

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"/>

最佳答案

一切似乎都很好,可能是您缺少权限?

关于android - ContentObserver 没有调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43731322/

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