运行同步适配器

注意:我们建议将 WorkManager 作为大多数后台处理用例的推荐解决方案。请参阅后台处理指南,了解哪种解决方案最适合您。

在本课程之前的课程中,您学习了如何创建封装数据传输代码的同步适配器组件,以及如何添加附加组件,以便将同步适配器插入到系统中。您现在拥有安装包含同步适配器的应用所需的一切,但您看到的代码实际上并没有运行同步适配器。

您应该尝试根据计划或作为某些事件的间接结果来运行同步适配器。例如,您可能希望同步适配器按固定计划运行,无论是在某个时间段之后还是在一天的特定时间。当设备上存储的数据发生更改时,您可能也希望运行同步适配器。您应该避免将同步适配器作为用户操作的直接结果来运行,因为这样做您无法充分利用同步适配器框架的调度能力。例如,您应该避免在用户界面中提供刷新按钮。

您有以下选项来运行同步适配器

当服务器数据发生更改时
响应来自服务器的消息来运行同步适配器,该消息指示基于服务器的数据已更改。此选项允许您将数据从服务器刷新到设备,而不会通过轮询服务器降低性能或浪费电池寿命。
当设备数据发生更改时
当设备上的数据发生更改时运行同步适配器。此选项允许您将修改后的数据从设备发送到服务器,如果您需要确保服务器始终拥有最新的设备数据,此选项特别有用。如果您确实将数据存储在您的内容提供程序中,则此选项易于实现。如果您使用的是 stub 内容提供程序,则检测数据更改可能更困难。
按固定间隔
在您选择的间隔到期后运行同步适配器,或每天在某个特定时间运行它。
按需
响应用户操作来运行同步适配器。但是,为了提供最佳用户体验,您应主要依靠更自动化的选项之一。通过使用自动化选项,您可以节省电池和网络资源。

本课的其余部分将更详细地描述每个选项。

当服务器数据发生更改时运行同步适配器

如果您的应用从服务器传输数据并且服务器数据频繁更改,您可以使用同步适配器来响应数据更改进行下载。要运行同步适配器,让服务器向您应用中的 BroadcastReceiver 发送一条特殊消息。响应此消息,调用 ContentResolver.requestSync() 以向同步适配器框架发出信号来运行您的同步适配器。

Google Cloud Messaging (GCM) 提供了您需要使此消息系统工作的服务器和设备组件。使用 GCM 触发传输比轮询服务器获取状态更可靠、更高效。轮询需要一个始终处于活动状态的 Service,而 GCM 使用在消息到达时激活的 BroadcastReceiver。定期轮询即使没有可用更新也会消耗电池电量,而 GCM 仅在需要时发送消息。

注意:如果您使用 GCM 通过向安装了您的应用的所有设备广播来触发同步适配器,请记住它们大约在同一时间收到您的消息。这种情况可能导致您的同步适配器的多个实例同时运行,从而导致服务器和网络过载。为了避免所有设备的广播出现这种情况,您应该考虑为每个设备唯一的时间段延迟同步适配器的启动。

以下代码片段向您展示了如何响应传入的 GCM 消息运行 requestSync()

Kotlin

...
// Constants
// Content provider authority
const val AUTHORITY = "com.example.android.datasync.provider"
// Account type
const val ACCOUNT_TYPE = "com.example.android.datasync"
// Account
const val ACCOUNT = "default_account"
// Incoming Intent key for extended data
const val KEY_SYNC_REQUEST = "com.example.android.datasync.KEY_SYNC_REQUEST"
...
class GcmBroadcastReceiver : BroadcastReceiver() {
    ...
    override fun onReceive(context: Context, intent: Intent) {
        // Get a GCM object instance
        val gcm: GoogleCloudMessaging = GoogleCloudMessaging.getInstance(context)
        // Get the type of GCM message
        val messageType: String? = gcm.getMessageType(intent)
        /*
         * Test the message type and examine the message contents.
         * Since GCM is a general-purpose messaging system, you
         * may receive normal messages that don't require a sync
         * adapter run.
         * The following code tests for a a boolean flag indicating
         * that the message is requesting a transfer from the device.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE == messageType
            && intent.getBooleanExtra(KEY_SYNC_REQUEST, false)) {
            /*
             * Signal the framework to run your sync adapter. Assume that
             * app initialization has already created the account.
             */
            ContentResolver.requestSync(mAccount, AUTHORITY, null)
            ...
        }
        ...
    }
    ...
}

Java

public class GcmBroadcastReceiver extends BroadcastReceiver {
    ...
    // Constants
    // Content provider authority
    public static final String AUTHORITY = "com.example.android.datasync.provider";
    // Account type
    public static final String ACCOUNT_TYPE = "com.example.android.datasync";
    // Account
    public static final String ACCOUNT = "default_account";
    // Incoming Intent key for extended data
    public static final String KEY_SYNC_REQUEST =
            "com.example.android.datasync.KEY_SYNC_REQUEST";
    ...
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get a GCM object instance
        GoogleCloudMessaging gcm =
                GoogleCloudMessaging.getInstance(context);
        // Get the type of GCM message
        String messageType = gcm.getMessageType(intent);
        /*
         * Test the message type and examine the message contents.
         * Since GCM is a general-purpose messaging system, you
         * may receive normal messages that don't require a sync
         * adapter run.
         * The following code tests for a a boolean flag indicating
         * that the message is requesting a transfer from the device.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)
            &&
            intent.getBooleanExtra(KEY_SYNC_REQUEST)) {
            /*
             * Signal the framework to run your sync adapter. Assume that
             * app initialization has already created the account.
             */
            ContentResolver.requestSync(mAccount, AUTHORITY, null);
            ...
        }
        ...
    }
    ...
}

当内容提供程序数据发生更改时运行同步适配器

如果您的应用在内容提供程序中收集数据,并且您希望在更新提供程序时更新服务器,则可以设置您的应用以自动运行您的同步适配器。为此,您需要为内容提供程序注册一个观察者。当内容提供程序中的数据发生更改时,内容提供程序框架会调用观察者。在观察者中,调用 requestSync() 来通知框架运行您的同步适配器。

注意:如果您正在使用 stub 内容提供程序,则内容提供程序中没有任何数据,并且永远不会调用 onChange()。在这种情况下,您必须提供自己的机制来检测设备数据的更改。此机制还负责在数据更改时调用 requestSync()

要为您的内容提供程序创建一个观察者,请扩展 ContentObserver 类并实现其两种形式的 onChange() 方法。在 onChange() 中,调用 requestSync() 来启动同步适配器。

要注册观察者,请在调用 registerContentObserver() 时将其作为参数传入。在此调用中,您还必须传入要观察的数据的内容 URI。内容提供程序框架会将此观察 URI 与作为参数传入修改您提供程序的 ContentResolver 方法(例如 ContentResolver.insert())的内容 URI 进行比较。如果匹配,则会调用您的 ContentObserver.onChange() 实现。

以下代码片段向您展示了如何定义一个在表更改时调用 requestSync()ContentObserver

Kotlin

// Constants
// Content provider scheme
const val SCHEME = "content://"
// Content provider authority
const val AUTHORITY = "com.example.android.datasync.provider"
// Path for the content provider table
const val TABLE_PATH = "data_table"
...
class MainActivity : FragmentActivity() {
    ...
    // A content URI for the content provider's data table
    private lateinit var uri: Uri
    // A content resolver for accessing the provider
    private lateinit var mResolver: ContentResolver
    ...
    inner class TableObserver(...) : ContentObserver(...) {
        /*
         * Define a method that's called when data in the
         * observed content provider changes.
         * This method signature is provided for compatibility with
         * older platforms.
         */
        override fun onChange(selfChange: Boolean) {
            /*
             * Invoke the method signature available as of
             * Android platform version 4.1, with a null URI.
             */
            onChange(selfChange, null)
        }

        /*
         * Define a method that's called when data in the
         * observed content provider changes.
         */
        override fun onChange(selfChange: Boolean, changeUri: Uri?) {
            /*
             * Ask the framework to run your sync adapter.
             * To maintain backward compatibility, assume that
             * changeUri is null.
             */
            ContentResolver.requestSync(account, AUTHORITY, null)
        }
        ...
    }
    ...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        // Get the content resolver object for your app
        mResolver = contentResolver
        // Construct a URI that points to the content provider data table
        uri = Uri.Builder()
                .scheme(SCHEME)
                .authority(AUTHORITY)
                .path(TABLE_PATH)
                .build()
        /*
         * Create a content observer object.
         * Its code does not mutate the provider, so set
         * selfChange to "false"
         */
        val observer = TableObserver(false)
        /*
         * Register the observer for the data table. The table's path
         * and any of its subpaths trigger the observer.
         */
        mResolver.registerContentObserver(uri, true, observer)
        ...
    }
    ...
}

Java

public class MainActivity extends FragmentActivity {
    ...
    // Constants
    // Content provider scheme
    public static final String SCHEME = "content://";
    // Content provider authority
    public static final String AUTHORITY = "com.example.android.datasync.provider";
    // Path for the content provider table
    public static final String TABLE_PATH = "data_table";
    // Account
    public static final String ACCOUNT = "default_account";
    // Global variables
    // A content URI for the content provider's data table
    Uri uri;
    // A content resolver for accessing the provider
    ContentResolver mResolver;
    ...
    public class TableObserver extends ContentObserver {
        /*
         * Define a method that's called when data in the
         * observed content provider changes.
         * This method signature is provided for compatibility with
         * older platforms.
         */
        @Override
        public void onChange(boolean selfChange) {
            /*
             * Invoke the method signature available as of
             * Android platform version 4.1, with a null URI.
             */
            onChange(selfChange, null);
        }
        /*
         * Define a method that's called when data in the
         * observed content provider changes.
         */
        @Override
        public void onChange(boolean selfChange, Uri changeUri) {
            /*
             * Ask the framework to run your sync adapter.
             * To maintain backward compatibility, assume that
             * changeUri is null.
             */
            ContentResolver.requestSync(mAccount, AUTHORITY, null);
        }
        ...
    }
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        // Get the content resolver object for your app
        mResolver = getContentResolver();
        // Construct a URI that points to the content provider data table
        uri = new Uri.Builder()
                  .scheme(SCHEME)
                  .authority(AUTHORITY)
                  .path(TABLE_PATH)
                  .build();
        /*
         * Create a content observer object.
         * Its code does not mutate the provider, so set
         * selfChange to "false"
         */
        TableObserver observer = new TableObserver(false);
        /*
         * Register the observer for the data table. The table's path
         * and any of its subpaths trigger the observer.
         */
        mResolver.registerContentObserver(uri, true, observer);
        ...
    }
    ...
}

定期运行同步适配器

您可以通过设置运行间隔的等待时间,或者在一天的特定时间运行,或者两者兼有来定期运行同步适配器。定期运行同步适配器可以使您的更新间隔大致与服务器的更新间隔匹配。

类似地,您可以在服务器相对空闲时从设备上传数据,通过安排您的同步适配器在夜间运行。大多数用户在夜间会保持设备开启并充电,因此这段时间通常可用。此外,设备在您的同步适配器运行时没有运行其他任务。但是,如果采用这种方法,您需要确保每个设备在稍有不同的时间触发数据传输。如果所有设备同时运行您的同步适配器,您很可能会使服务器和蜂窝提供商数据网络过载。

通常,如果您的用户不需要即时更新,但期望定期更新,则定期运行是合理的。如果您希望在最新数据的可用性与不过度使用设备资源的较小同步适配器运行的效率之间取得平衡,则定期运行也是合理的。

要定期运行您的同步适配器,请调用 addPeriodicSync()。这会安排您的同步适配器在经过一定时间后运行。由于同步适配器框架必须考虑其他同步适配器执行并尝试最大化电池效率,因此经过的时间可能会有几秒的偏差。此外,如果网络不可用,框架将不会运行您的同步适配器。

请注意,addPeriodicSync() 不会在一天中的特定时间运行同步适配器。要在每天大致相同的时间运行您的同步适配器,请使用重复警报作为触发器。有关重复警报的更详细描述,请参阅 AlarmManager 的参考文档。如果您使用 setInexactRepeating() 方法设置具有一定变化的时间触发器,您仍然应该随机化启动时间,以确保来自不同设备的同步适配器运行时间错开。

方法 addPeriodicSync() 不会禁用 setSyncAutomatically(),因此您可能会在相对较短的时间内获得多次同步运行。此外,在调用 addPeriodicSync() 时只允许使用少数同步适配器控制标志;不允许使用的标志在 addPeriodicSync() 的参考文档中有所描述。

以下代码片段向您展示了如何安排定期同步适配器运行

Kotlin

// Content provider authority
const val AUTHORITY = "com.example.android.datasync.provider"
// Account
const val ACCOUNT = "default_account"
// Sync interval constants
const val SECONDS_PER_MINUTE = 60L
const val SYNC_INTERVAL_IN_MINUTES = 60L
const val SYNC_INTERVAL = SYNC_INTERVAL_IN_MINUTES * SECONDS_PER_MINUTE
...
class MainActivity : FragmentActivity() {
    ...
    // A content resolver for accessing the provider
    private lateinit var mResolver: ContentResolver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        // Get the content resolver for your app
        mResolver = contentResolver
        /*
         * Turn on periodic syncing
         */
        ContentResolver.addPeriodicSync(
                mAccount,
                AUTHORITY,
                Bundle.EMPTY,
                SYNC_INTERVAL)
        ...
    }
    ...
}

Java

public class MainActivity extends FragmentActivity {
    ...
    // Constants
    // Content provider authority
    public static final String AUTHORITY = "com.example.android.datasync.provider";
    // Account
    public static final String ACCOUNT = "default_account";
    // Sync interval constants
    public static final long SECONDS_PER_MINUTE = 60L;
    public static final long SYNC_INTERVAL_IN_MINUTES = 60L;
    public static final long SYNC_INTERVAL =
            SYNC_INTERVAL_IN_MINUTES *
            SECONDS_PER_MINUTE;
    // Global variables
    // A content resolver for accessing the provider
    ContentResolver mResolver;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        // Get the content resolver for your app
        mResolver = getContentResolver();
        /*
         * Turn on periodic syncing
         */
        ContentResolver.addPeriodicSync(
                mAccount,
                AUTHORITY,
                Bundle.EMPTY,
                SYNC_INTERVAL);
        ...
    }
    ...
}

按需运行同步适配器

响应用户请求来运行同步适配器是运行同步适配器的最不推荐策略。框架专门设计用于在按照计划运行同步适配器时节省电池电量。响应数据更改运行同步的选项能有效利用电池电量,因为电量用于提供新数据。

相比之下,允许用户按需运行同步意味着同步自行运行,这会低效地使用网络和电源资源。此外,提供按需同步会导致用户请求同步,即使没有证据表明数据已更改,并且运行不刷新数据的同步是无效使用电池电量的行为。一般来说,您的应用应该使用其他信号触发同步或按固定间隔安排同步,而无需用户输入。

但是,如果您仍然希望按需运行同步适配器,请为手动同步适配器运行设置同步适配器标志,然后调用 ContentResolver.requestSync()

使用以下标志按需运行传输

SYNC_EXTRAS_MANUAL
强制进行手动同步。同步适配器框架会忽略现有设置,例如 setSyncAutomatically() 设置的标志。
SYNC_EXTRAS_EXPEDITED
强制同步立即开始。如果您不设置此标志,系统可能会在运行同步请求前等待几秒钟,因为它试图通过在短时间内安排许多请求来优化电池使用。

以下代码片段向您展示了如何响应按钮点击调用 requestSync()

Kotlin

// Constants
// Content provider authority
val AUTHORITY = "com.example.android.datasync.provider"
// Account type
val ACCOUNT_TYPE = "com.example.android.datasync"
// Account
val ACCOUNT = "default_account"
...
class MainActivity : FragmentActivity() {
    ...
    // Instance fields
    private lateinit var mAccount: Account
    ...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        /*
         * Create the placeholder account. The code for CreateSyncAccount
         * is listed in the lesson Creating a Sync Adapter
         */

        mAccount = createSyncAccount()
        ...
    }

    /**
     * Respond to a button click by calling requestSync(). This is an
     * asynchronous operation.
     *
     * This method is attached to the refresh button in the layout
     * XML file
     *
     * @param v The View associated with the method call,
     * in this case a Button
     */
    fun onRefreshButtonClick(v: View) {
        // Pass the settings flags by inserting them in a bundle
        val settingsBundle = Bundle().apply {
            putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true)
            putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true)
        }
        /*
         * Request the sync for the default account, authority, and
         * manual sync settings
         */
        ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle)
    }

Java

public class MainActivity extends FragmentActivity {
    ...
    // Constants
    // Content provider authority
    public static final String AUTHORITY =
            "com.example.android.datasync.provider";
    // Account type
    public static final String ACCOUNT_TYPE = "com.example.android.datasync";
    // Account
    public static final String ACCOUNT = "default_account";
    // Instance fields
    Account mAccount;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        /*
         * Create the placeholder account. The code for CreateSyncAccount
         * is listed in the lesson Creating a Sync Adapter
         */

        mAccount = CreateSyncAccount(this);
        ...
    }
    /**
     * Respond to a button click by calling requestSync(). This is an
     * asynchronous operation.
     *
     * This method is attached to the refresh button in the layout
     * XML file
     *
     * @param v The View associated with the method call,
     * in this case a Button
     */
    public void onRefreshButtonClick(View v) {
        // Pass the settings flags by inserting them in a bundle
        Bundle settingsBundle = new Bundle();
        settingsBundle.putBoolean(
                ContentResolver.SYNC_EXTRAS_MANUAL, true);
        settingsBundle.putBoolean(
                ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        /*
         * Request the sync for the default account, authority, and
         * manual sync settings
         */
        ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle);
    }