我需要编写一个可以防止工作资料中的联系人同步的应用。在Android 9之前,我只是禁用了程序包com.google.android.syncadapters.contacts
,仅此而已。
[很遗憾,自Android 10起,Play服务负责同步Google帐户中的联系人。但是我不想在工作资料中禁用软件包com.google.android.gms
,因为它还有更多的副作用。
因此,我目前正在寻找一种方法或API以设备管理员或工作资料所有者的身份禁用另一个应用程序的特定组件。
感谢和问候,大卫
我有3种方法来尝试完成此任务,尽管从未测试过:
解决方案1-setSyncAutomatically:
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-明确删除removeAccount:
I think,这取决于目标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
}
}