首页 > 解决方案 > (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE) vs Accountmanager.get(context)

问题描述

What's the difference between -

AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

and

AccountManager accountManager =Accountmanager.get(context)

What should i use when-

1.Retrieving list of already created accounts in device

2.Adding my app's account to device

标签: androidaccountaccountmanagerandroid-account

解决方案


如果您仔细研究,Accountmanager.get(context)您会发现它基本上是使用空检查的第一个选项的快捷方式:

public static AccountManager get(Context context) {
    if (context == null) throw new IllegalArgumentException("context is null");
    return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
}

推荐阅读