Skip to content

Latest commit

 

History

History
172 lines (139 loc) · 9.4 KB

File metadata and controls

172 lines (139 loc) · 9.4 KB

创建Stub授权器

编写:jdneo - 原文:http://developer.android.com/training/sync-adapters/creating-authenticator.html

Sync Adapter框架假定你的Sync Adapter在同步数据时,设备存储端关联了一个账户,且服务器端需要进行登录验证。因此,你需要提供一个叫做授权器(Authenticator)的组件作为Sync Adapter的一部分。该组件会集成在Android账户及认证框架中,并提供一个标准的接口来处理用户凭据,比如登录信息。

如果你的应用不使用账户,你仍然需要提供一个授权器组件。在这种情况下,授权器所处理的信息将被忽略,所以你可以提供一个包含了方法存根(Stub Method)的授权器组件。同时你需要提供一个绑定Service,来允许Sync Adapter框架来调用授权器的方法。

这节课将向你展示如何定义一个能够满足Sync Adapter框架要求的Stub授权器。如果你想要提供可以处理用户账户的实际的授权器,可以阅读:AbstractAccountAuthenticator

添加一个Stub授权器组件

要在你的应用中添加一个Stub授权器,首先我们需要创建一个继承AbstractAccountAuthenticator的类,在所有需要重写的方法中,我们不进行任何处理,仅返回null或者抛出异常。

下面的代码片段是一个Stub授权器的例子:

/*
 * Implement AbstractAccountAuthenticator and stub out all
 * of its methods
 */
public class Authenticator extends AbstractAccountAuthenticator {
    // Simple constructor
    public Authenticator(Context context) {
        super(context);
    }
    // Editing properties is not supported
    @Override
    public Bundle editProperties(
            AccountAuthenticatorResponse r, String s) {
        throw new UnsupportedOperationException();
    }
    // Don't add additional accounts
    @Override
    public Bundle addAccount(
            AccountAuthenticatorResponse r,
            String s,
            String s2,
            String[] strings,
            Bundle bundle) throws NetworkErrorException {
        return null;
    }
    // Ignore attempts to confirm credentials
    @Override
    public Bundle confirmCredentials(
            AccountAuthenticatorResponse r,
            Account account,
            Bundle bundle) throws NetworkErrorException {
        return null;
    }
    // Getting an authentication token is not supported
    @Override
    public Bundle getAuthToken(
            AccountAuthenticatorResponse r,
            Account account,
            String s,
            Bundle bundle) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }
    // Getting a label for the auth token is not supported
    @Override
    public String getAuthTokenLabel(String s) {
        throw new UnsupportedOperationException();
    }
    // Updating user credentials is not supported
    @Override
    public Bundle updateCredentials(
            AccountAuthenticatorResponse r,
            Account account,
            String s, Bundle bundle) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }
    // Checking features for the account is not supported
    @Override
    public Bundle hasFeatures(
        AccountAuthenticatorResponse r,
        Account account, String[] strings) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }
}

将授权器绑定到框架

为了让Sync Adapter框架可以访问你的授权器,你必须为它创建一个绑定服务。这一服务提供一个Android Binder对象,允许框架调用你的授权器,并且在授权器和框架间传递数据。

因为框架会在它第一次需要访问授权器时启动该Service,你也可以使用该服务来实例化授权器,具体而言,我们需要在服务的Service.onCreate()方法中调用授权器的构造函数。

下面的代码样例展示了如何定义绑定Service

/**
 * A bound Service that instantiates the authenticator
 * when started.
 */
public class AuthenticatorService extends Service {
    ...
    // Instance field that stores the authenticator object
    private Authenticator mAuthenticator;
    @Override
    public void onCreate() {
        // Create a new authenticator object
        mAuthenticator = new Authenticator(this);
    }
    /*
     * When the system binds to this Service to make the RPC call
     * return the authenticator's IBinder.
     */
    @Override
    public IBinder onBind(Intent intent) {
        return mAuthenticator.getIBinder();
    }
}

添加授权器的元数据(Metadata)文件

若要将你的授权器组件集成到Sync Adapter框架和账户框架中,你需要为这些框架提供带有描述组件信息的元数据。该元数据声明了你为Sync Adapter创建的账户类型以及系统所显示的UI元素(如果你希望用户可以看到你创建的账户类型)。在你的项目目录/res/xml/下,将元数据声明于一个XML文件中。你可以自己为该文件按命名,通常我们将它命名为authenticator.xml

在这个XML文件中,包含了一个<account-authenticator>标签,它有下列一些属性:

android:accountType

Sync Adapter框架要求每一个适配器都有一个域名形式的账户类型。框架会将它作为Sync Adapter内部标识的一部分。如果服务端需要登陆,账户类型会和账户一起发送到服务端作为登录凭据的一部分。

如果你的服务端不需要登录,你仍然需要提供一个账户类型(该属性的值用你能控制的一个域名即可)。虽然框架会使用它来管理Sync Adapter,但该属性的值不会发送到你的服务端。

android:icon

指向一个包含图标的Drawable资源。如果你在res/xml/syncadapter.xml中通过指定android:userVisible="true"让Sync Adapter可见,那么你必须提供图标资源。它会在系统的设置中的账户(Accounts)这一栏内显示。

android:smallIcon

指向一个包含微小版本图标的Drawable资源。当屏幕尺寸较小时,这一资源可能会替代android:icon中所指定的图标资源。

android:label

指明了用户账户类型的本地化String。如果你在res/xml/syncadapter.xml中通过指定android:userVisible="true"让Sync Adapter可见,那么你需要提供该String。它会在系统的设置中的账户这一栏内显示,就在你为授权器定义的图标旁边。

下面的代码样例展示了你之前为授权器创建的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:accountType="example.com"
        android:icon="@drawable/ic_launcher"
        android:smallIcon="@drawable/ic_launcher"
        android:label="@string/app_name"/>

在Manifest清单文件中声明授权器

在之前的步骤中,你已经创建了一个绑定服务,将授权器和Sync Adapter框架连接了起来。为了让系统可以识别该服务,你需要在清单文件中添加<service>标签,将它作为<application>的子标签:

    <service
            android:name="com.example.android.syncadapter.AuthenticatorService">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator"/>
        </intent-filter>
        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>

<intent-filter>标签配置了一个可以被android.accounts.AccountAuthenticator这一Action所激活的过滤器,这一Intent会在系统要运行授权器时由系统发出。当过滤器被激活后,系统会启动AuthenticatorService,即之前用来封装授权器的Service

<meta-data>标签声明了授权器的元数据。android:name属性将元数据和授权器框架连接起来。android:resource指定了你之前所创建的授权器元数据文件的名字。

除了授权器之外,Sync Adapter框架也需要一个Content Provider。如果你的应用并没有使用Content Provider,可以阅读下一节课程学习如何创建一个Stub Content Provider;如果你的应用已经使用了ContentProvider,可以直接阅读:创建Sync Adapter