onPerformSync在模拟器上调用,但不在物理设备上调用

时间:2015-09-18 11:20:05

标签: android android-contacts contactscontract android-syncadapter

我已经构建了一个帐户同步适配器,以便在本地联系人簿中显示应用中的联系人。

有一个虚假的authonticator提供帐户。帐户也是可以同步的

    ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
    ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);

此帐户显示在设备上的所有帐户中:

enter image description here

我试图通过系统触发onPerformSync - 从菜单中的设置按'立即同步'并以编程方式执行:

public static void triggerRefresh() {
    Bundle b = new Bundle();
    b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    Account account = AccountAuthenticatorService.GetAccount();

    if (ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY)  ||
            ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY)) {
        ContentResolver.cancelSync(account, ContactsContract.AUTHORITY);
    }

    ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
    ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
    ContentResolver.requestSync(
            account, // Sync account
            ContactsContract.AUTHORITY,authority
            b);
}

它在模拟器上工作正常,但在几个设备(sony,samsung)上它根本没有被触发(我试图在onPerformSync方法中记录smth但从未看到此日志)。

我试图找到这样的问题,但没有任何帮助,我无法使onPerformSync强制被调用。

根据syncAdapter,模拟器和设备之间的主要区别是什么?

1 个答案:

答案 0 :(得分:0)

最后我找到了这个问题的原因:

在您没有互联网连接之前,永远不会调用

onPerformSync方法。在几个设备和模拟器上有一个互联网连接,但在其他设备和模拟器上你不能触发这种方法。

相关问题