GET_ACCOUNT 未在 android N 中授予权限

GET_ACCOUNT permission is not granted in android N

我在做客户经理。我想查询账号是否存在

 private static final String TAG = "UserAccountUtil";

    public static Account getAccount(Context context) {
        if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "GET_ACCOUNTS not present.");

        }

        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(Constant.ACCOUNT_TYPE);
        if (accounts.length > 0) {
            Log.d(TAG, "GET_ACCOUNTS  present..."+accounts[0]);
            return accounts[0];
        } else {
            Log.d(TAG, "GET_ACCOUNTS not  present...");

            return null;
        }
    }

它在日志中总是 returns 空或 "GET_ACCOUNTS not present."。我在清单中添加了 also.I 我也在请求 运行 时间许可。

GET_ACCOUNTS--> Beginning with Android 6.0 (API level 23), if an app shares the signature of the authenticator that manages an account, it does not need "GET_ACCOUNTS" permission to read information about that account. On Android 5.1 and lower, all apps need "GET_ACCOUNTS" permission to read information about any account.The GET_ACCOUNTS permission is now Dead

您可以使用 READ_CONTACTS 权限代替。

  • 检查到Build.VERSION.SDK_INT

演示

if (accounts.length > 0 && android.os.Build.VERSION.SDK_INT<23 ) {
            Log.d(TAG, "GET_ACCOUNTS  present..."+accounts[0]);
            return accounts[0];
        } else {
            Log.d(TAG, "GET_ACCOUNTS not  present...");

            return null;
        }