【问题标题】:Selecting an account for saving contacts选择用于保存联系人的帐户
【发布时间】:2017-08-10 02:38:50
【问题描述】:

我正在制作一个电话簿应用程序。

我可以看到一些联系人有不同的ACCOUNT_TYPE_AND_DATA_SET(com.whatsapp、com.viber.voip、com.google、com.android.huawei.sim、com.android.huawei.phone 等)。

问题来了:我如何才能获得用于保存联系人的帐户(权限)列表?

【问题讨论】:

    标签: android contacts contactscontract


    【解决方案1】:

    您可以为此使用AccountManager 服务:

    Account[] accounts = AccountManager.get(this).getAccounts();
    for (Account account : accounts) {
       Log.d(TAG, account.type + " / " + account.name);
    }
    

    注意,需要GET_ACCOUNTS 权限,如果您的目标是Android M 及更高版本,您还需要通过Runtime Permissions 模型向用户请求此权限。

    更新

    跳过所有不支持联系人的帐户,或支持联系人但只读的帐户(如 Viber 和 Whatsapp):

    final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
    for (SyncAdapterType sync : syncs) {
        Log.d(TAG, "found SyncAdapter: " + sync.accountType);
        if (ContactsContract.AUTHORITY.equals(sync.authority)) {
            Log.d(TAG, "found SyncAdapter that supports contacts: " + sync.accountType);
            if (sync.supportsUploading()) {
                Log.d(TAG, "found SyncAdapter that supports contacts and is not read-only: " + sync.accountType);
                // we'll now get a list of all accounts under that accountType:
                Account[] accounts = AccountManager.get(this).getAccountsByType(sync.accountType);
                for (Account account : accounts) {
                   Log.d(TAG, account.type + " / " + account.name);
                }
            }
        }
    }
    

    随意探索SyncAdapterType 中的其他好东西,例如isUserVisible,您可能也想检查一下。

    【讨论】:

    • 我可以通过这种方式获取所有帐户。但是我可以使用其中哪些来保存联系人?例如,我有 3 个帐户(google、whatsapp、viber),但实际上我只能在 google 帐户上保存联系人(也可以在本地电话存储和 SIM 卡中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    相关资源
    最近更新 更多