如何以编程方式以设备管理员身份禁用syncadapter或提供程序?

我需要编写一个可以防止联系人在工作资料中同步的应用。 在Android 9之前,我只是禁用了软件包com.google.android.syncadapters.contacts,仅此而已。

很遗憾,自Android 10起,Play服务负责同步Google帐户中的联系人。但是我不想在工作资料中禁用软件包com.google.android.gms,因为这样做有很多副作用。

因此,我目前正在寻找一种方法或API以设备管理员或工作资料所有者的身份禁用另一个应用程序的特定组件。

感谢和问候, 大卫

xaiwei007 回答:如何以编程方式以设备管理员身份禁用syncadapter或提供程序?

我有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

大家都在问