您需要设置多个组件才能以编程方式创建一个帐户。你需要:
- AccountAuthenticator
- 提供访问AccountAuthenticator的服务
- 一些权限
认证者
验证者是一个对象,它将在有权管理它的帐户类型和授权者(即linux用户)之间进行映射。
声明身份验证器 是在xml中完成的:
- 创建一个文件
res/xml/authenticator.xml
具有以下内容:
<?xml version="1.0" encoding="utf-8"?><account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" android:accountType="com.company.demo.account.DEMOACCOUNT" android:icon="@drawable/ic_launcher" android:smallIcon="@drawable/ic_launcher" android:label="@string/my_custom_account"/>
请注意accountType:创建帐户时,必须在代码中重用它。图标和标签将由“设置”应用使用,以显示该类型的帐户。
实施AccountAuthenticator
您必须扩展
AbstractAccountAuthenticator才能做到这一点。第三方应用程序将使用它来访问帐户数据。
以下示例不允许任何对第三方应用程序的访问,因此每种方法的实现都很简单。
public class CustomAuthenticator extends AbstractAccountAuthenticator { public CustomAuthenticator(Context context) { super(context); } @Override public Bundle addAccount(AccountAuthenticatorResponse accountAuthenticatorResponse, String s, String s2, String[] strings, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle editProperties(AccountAuthenticatorResponse accountAuthenticatorResponse, String s) { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle confirmCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle getAuthToken(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public String getAuthTokenLabel(String s) { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle updateCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle hasFeatures(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String[] strings) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. }}公开帐户类型的服务
创建一个服务来操纵该类型的帐户:
public class AuthenticatorService extends Service { @Override public IBinder onBind(Intent intent) { CustomAuthenticator authenticator = new CustomAuthenticator(this); return authenticator.getIBinder(); }}在清单中声明服务 :
<service android:name="com.company.demo.account.AuthenticatorService" android:exported="false"> <intent-filter> <action android:name="android.accounts.AccountAuthenticator"/> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"/> </service>
这里,引用声明身份验证器的xml资源的过滤器和元数据是关键点。
权限
在清单中,请确保声明以下权限
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/><uses-permission android:name="android.permission.GET_ACCOUNTS"/><uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
(并非本文中提供的示例代码所需的全部,但您可能还会有一些有关帐户管理的代码,最后它们将非常有用)
用代码创建一个帐户
现在一切就绪,您可以使用以下代码创建一个帐户。注意
boolean通过返回
addAccountExplicitly通知您的成功或失败。
AccountManager accountManager = AccountManager.get(this); //this is Activity Account account = new Account("MyAccount","com.company.demo.account.DEMOACCOUNT"); boolean success = accountManager.addAccountExplicitly(account,"password",null); if(success){ Log.d(TAG,"Account created"); }else{ Log.d(TAG,"Account creation failed. Look at previous logs to investigate"); }最后提示
不要在外部存储上安装您的应用
如果您的应用程序安装在外部存储设备上,则很有可能在卸载sdcard时Android删除您的帐户数据(因为该帐户的身份验证器将不再可用)。因此,为避免这种损失(每次重新启动!!!),您必须仅在内部存储上安装声明身份验证器的应用程序:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" ...
万一遇到麻烦
仔细阅读日志,AccountManger将输出许多日志以帮助您调试代码。



