ContentObserver没有调用

时间:2017-05-02 06:19:04

标签: android android-service contentobserver

ContactObserver的OnChange方法没有调用,除非我第二次运行应用程序。第一次运行使得安装似乎不会使寄存器观察者甚至onCreate从Service调用。

在第二轮比赛中,它完美无瑕。你可以看到下面的代码。

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文件的应用程序元素中的xml元素。

        <service
        android:name=".ui.edge.contact.ContactSync"
        android:enabled="true"
        android:exported="false"/>

1 个答案:

答案 0 :(得分:1)

一切似乎都很好,可能你没有得到许可?