大家在平时使用 Android 手机的时候,都会发现有些应用(例如 qq,微信,淘宝)为自己创建了账号系统,并且能够在设置页面看到他,可是当自己希望为自己的软件写一个账号系统的时候总是不知从何入手,现在我们就从头开始,一步一步打造属于自己应用的账号系统。
在进行设备账户管理的时候,我们会通过一个 AccountManager 类获取系统的账户管理类,获取的方法如下:
或者
1.获取账户信息
首先我们来查看一下如何获取用户已有的账户信息,如果你希望读取系统当前的账户信息,那么你首先需要在 manifest 文件中申明一个读取账户的权限,如下:
(1)获取所有账户信息
如果你希望获取到当前设备所有的账户信息,你可以使用:
(2)获取特定的账户信息
如果你只希望获取自己或者特定的账户信息,你就应该使用:
后面的参数是你自己定义的账户类型,怎么设置我会在接下来的文章中写出来。
然后,我们就可以通过上面获取到的工具类,读取到手机上的账户信息了,这里我写了一个 ListView 的 Adapter 用于展示当前系统中的账号信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class AccountAdapter extends BaseAdapter { private Account[] mAccounts; public AccountAdapter(Account[] accounts) { this .mAccounts = accounts; } @Override public int getCount() { return mAccounts != null mAccounts.length : 0 ; } @Override public Object getItem( int position) { return mAccounts[position]; } @Override public long getItemId( int position) { return 0 ; } @Override public View getView( int position, View convertView, ViewGroup parent) { TextView tv = new TextView(getBaseContext()); tv.setText(mAccounts[position].name + + mAccounts[position].type); return tv; } } |
同你当前设备进行对比,你就会发现我们已经将当前所有的账户信息显示了出来,同样地如果你只希望显示部分的账户信息,你就可以通过 getAccountByType 获取对应的账户列表。
2.建立自己的账号服务
通过上面的部分,我们已经知道了如何获取 Android 本身现有的账号信息,现在我们就开始着手建立属于自己的账号系统吧。
你需要知道的是,如果你希望建立自己的账号系统,那么你得在 manifest 文件中声明一个关于账号的Service,如下
Ok,到了这里,我们已经向系统声明了一个账户相关的服务,现在让我们来具体实现他。
Android 为我们提供了一个叫做 AbstractAccountAuthenticator 的抽象类,也是通过它来实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | public static class Authenticator extends AbstractAccountAuthenticator { public Authenticator(Context context) { super (context); } @Override public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { return null ; } @Override public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException { return null ; } @Override public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException { return null ; } @Override public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { return null ; } @Override public String getAuthTokenLabel(String authTokenType) { return null ; } @Override public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { return null ; } @Override public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException { return null ; } } |
当我们创建好了一个AbstractAccountAuthenticator 类后,我们需要从 Service 中取得这个类的对象,代码如下:
当你完成上面的步骤之后,你就会发现,在你的设置页面点击添加账户时就会出现你自定义的账户了,如下:
3.添加账户
虽然通过上面的步骤,我们已经能够在添加账户的界面看到属于我们自己的账户类别了,但是你会发现当你点击它们的时候,没有任何作用,那么我们应该怎么在设备上完成添加账户的操作呢?
(1)加入添加账户的权限
添加账户也需要对应的权限,你应该在 manifest 文件中加入
(2)重写 Authenticator 的 addAccount 方法
当用户在添加账户页面选择账户进行添加或者调用accountManager.addAccount 的时候,系统会默认调用 AbstractAccountAuthenticator 中的 addAccount 方法,因此你需要重写 addAccount 方法,直接添加默认账户,或者跳转到某个页面,让用户填写用户信息,然后添加账户。
(3)使用 addAccountExplicitly 直接添加账户
如果你希望直接添加账户信息,你可以使用以下方法:
本文暂时就到这里了,通过本文,你可以初步了解到 Android 的账户信息的建立流程,希望对大家有所帮助。
联系客服