我有 3 种方法可以尝试完成此任务,但从未测试过它们:
解决方案 1 - 自动设置同步:
Account[] accounts = AccountManager.get(this).getAccountsByType(theAccountType);
for (Account account : accounts) {
Log.d(TAG, "got " + account.type + " / " + account.name);
ContentResolver.setSyncAutomatically(account,ContactsContract.AUTHORITY,false); // disable auto-syncing on that sync-adapter
ContentResolver.setIsSyncable(account,ContactsContract.AUTHORITY,false); // 也禁用定期同步 }
解决方案 2 - removeAccountExplicitly:
我认为,根据您的目标 API,如果您尝试禁用不属于您自己的应用程序的同步适配器(即使用不同的证书签名),Android 可能会引发异常。但是,值得一试。
Account[] accounts = AccountManager.get(this).getAccountsByType(theAccountType);
for (Account account : accounts) {
Log.d(TAG, "got " + account.type + " / " + account.name);
AccountManager.removeAccountExplicitly(account); // completely removing the account, not just disabling sync... beware.
}
解决方案 3 - 启动同步适配器的设置屏幕:
您拥有的第二个最佳选项是启动特定设置屏幕,以允许用户通过单击快速禁用同步:
final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
for (SyncAdapterType sync : syncs) {
Log.d(TAG, "found SyncAdapter: " + sync.accountType);
if (theAccountType.equals(sync.accountType)) {
Log.d(TAG, "found it: " + sync);
String activityStr = sync.getSettingsActivity();
Intent intent = new Intent(Intent.ACTION_VIEW, activityStr);
// launch the intent
}
}