欢迎使用CSDN-markdown编辑器

来源:互联网 发布:四川大学网络教学 编辑:程序博客网 时间:2024/06/05 20:17

简单了解建立Android 账户流程

     当使用Android 手机时,我们能够在设置页面--账户 发现有些应用( 360手机助手,微博,QQ,微信,淘宝)创建了账户系统,所以在开发Android系统中,定制自己的账户系统也可以使用Google的原生框架。     具体使用Google API  AccountManager 类获取系统的账户管理类, 获取的方法如下:
 AccountManager mAccountManager = (AccountManager) getSystemService(ACCOUNT_SERVICE); 或者 AccountManager accountManager = AccountManager.get(context);
    通过 AccountManager 对象可以对账号系统进行操作。 1. 获取账户信息    首先查看一下如何获取用户已有的账户信息,读取系统当前的账户信息,需要在 manifest 文件中申明一个读取账户的权限,如下:
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>  
    (1)获取所有账户信息     
若想获取到当前设备所有的账户信息,现使用:accountManager.getAccounts();  
    (2)获取特定的账户信息
若只获取自己或者特定的账户信息,则使用:accountManager.getAccountsByType("com.jhwwq"); //(参数是自定义的账户类型) 
2.建立自己的账号服务    由上面介绍获取 Android 本身现有的账号信息,可通过getAccounts来获取,现在创建自己的账号系统。    (1).在 manifest 文件中声明一个关于账号的Service
<service                  android:name="com.kifile.account.app.account.AccountService"                  android:enabled="true"                  android:exported="true">              <intent-filter>                  <action android:name="android.accounts.AccountAuthenticator"/>              </intent-filter>              <meta-data                      android:name="android.accounts.AccountAuthenticator"                      android:resource="@xml/authenticator"/>          </service>  
    通过设置 intent-filter 告知系统,当前应用中有一个账号服务,至于具体的账号信息则放在 meta-data 中的 android:resource 文件中提供, 该文件为authenticator.xml,放置路径为 res/xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>  <account-authenticator          xmlns:android="http://schemas.android.com/apk/res/android"          android:accountType="com.jhwwq"          android:icon="@drawable/ic_launcher"          android:smallIcon="@drawable/ic_launcher"          android:label="@string/app_name"/>  
    这时就可以向系统提供相关的账户信息,用于在 Android Setting 目录下显示对应的账号信息。在这里,定义了当前的账户类型为"com.jhwwq",在账户系统中显示的标签为@string/app_name 对应的 String 对象,显示的 icon 为ic_launcher。    已经向系统声明了一个账户相关的服务,Android 提供抽象类AbstractAccountAuthenticator,也是通过它来实现:
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 onfirmCredentials(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;          }      }  
    Android 需要从自定义的 Service 中获取一个 AbstractAccountAuthenticator 对象,然后再调用对象中的方法来实现账号系统的具体操作当我们创建好了一个AbstractAccountAuthenticator 类后,我们需要从 Service 中取得这个类的对象,代码如下:
private Authenticator authenticator;      @Override      public void onCreate() {          super.onCreate();          authenticator = new Authenticator(this);      }      @Override      public IBinder onBind(Intent intent) {          return authenticator.getIBinder();      }  
    AccountService 类在 onCreate 的时候创建一个 Authenticator 对象,然后再 bindService 的时候,将 Authenticator 的  IBinder 传递回去,以供调用。

3.添加账户

    通过上面的步骤,已经能够在添加账户的界面看到属于自己的账户类别了,但是你会发现当你点击它们的时候,没有任何作用,那么我们应该怎么在设备上完成添加账户的操作呢?    (1)加入添加账户的权限        添加账户也需要对应的权限,你应该在 manifest 文件中加入
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/> 
    (2)重写 Authenticator 的 addAccount 方法        当用户在添加账户页面选择账户进行添加或者调用accountManager.addAccount 的时候,系统会默认调用 AbstractAccountAuthenticator 中的 addAccount 方法,因此你需要重写 addAccount 方法,直接添加默认账户,或者跳转到某个页面,让用户填写用户信息,然后添加账户。    (3)使用 addAccountExplicitly 直接添加账户

如果你希望直接添加账户信息,你可以使用以下方法:

Account account = new Account("jhwwq,,,,12","com.jhwwq");          accountManager.addAccountExplicitly(account,password,userdata);  
0 0
原创粉丝点击