我有3种方法来尝试完成此任务,但从未进行过测试:
解决方案1-自动setSync:
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-明确删除帐户:
我认为,如果您尝试禁用不属于您自己的应用的同步适配器(即使用其他证书签名),则取决于目标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-启动syncadapter的设置屏幕:
您的第二个最佳选择是启动特定设置屏幕,以允许用户单击一次即可快速禁用同步:
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
}
}
本文链接:https://www.f2er.com/2718466.html