Android的SyncAdapter自动初始化正在同步 [英] Android SyncAdapter Automatically Initialize Syncing

查看:266
本文介绍了Android的SyncAdapter自动初始化正在同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SyncAdapter 我的应用程序,以及的AccountManager 来加我Apps帐户到Android客户经理。我的$ C $下时,我的帐户添加到客户经理的样子:

 捆绑数据=新的软件包(5);
data.putString(_PEOPLE_ID,people_id);
data.putString(_FIRST_NAME,FIRST_NAME);
data.putString(_LAST_NAME,姓氏);
data.putString(_PLAN,计划);
data.putString(_BIRTHDAY,生日);
账户账户=新帐号(用户名,_ACCOUNT_TYPE);
尝试 {
    布尔创建的;
    创建= _account_manager.addAccountExplicitly(帐户,
                                   _cryptography.encrypt(_SEED,密码),数据);
    response.accountCreated(创建);
    _account_manager.setAuthToken(帐户,_TOKEN_TYPE,session_token);
    _model.updateActiveAccount(people_id,用户名,密码);
    共享preferences.Editor设置= _settings.edit();
    settings.putString(_ACCOUNT_TYPE,account.name);
    settings.putString(_TOKEN_TYPE,session_token);
    settings.commit();
    //告诉内容提供商,它可以同步此帐户
    ContentResolver.setIsSyncable(账号,权限,1);
    最终捆绑额外=新软件包(1);
    extras.putBoolean(SYNC_EXTRAS_INITIALIZE,真正的);
    ContentResolver.addPeriodicSync(账号,权限,群众演员,900);
}赶上(例外五){
    Ln.e(e.getCause());
}
 

我可以成功地通过设置添加帐户的客户经理,但我必须手动启用同步在设置也账户,即使后台数据和自动同步设置仿真器上启用。如果我手动启用同步,则同步进行罚款,它只是默认情况下不启动。

解决方案

  ContentResolver.setIsSyncable(账号,权限,1);
ContentResolver.setSyncAutomatically(账号,权限,真正的);
 

由于 Blehi 表示,将启动自动同步指定的帐户,鉴于全球设置背景数据和自动同步被启用。

要prevent背到后端同步(从jcwenger )确保,如果在任何方法该 SyncAdapter.onPerformSync(...)电话 ContentResolver.notifyChange(...)它使用 ContentResolver.notifyChange(URI,观察者,FALSE)标记该通知不触发同步调用(第三个参数是 syncToNetwork )。

如果您使用的是的ContentProvider 来进行插入/删除/为您更新 SyncAdapter 这是有道理的叫 ContentResolver.notifyChange(...)所以,当应用程序是可见的,用户可以接收来自 SyncAdapter ,这意味着你将有的ContentProvider 制作 ContentResolver.notifyChange(...)调用。为了得到这个设置工作,我已经添加(下面的开发指南 CALLER_IS_SYNC_ADAPTER 查询参数的每一个URI被用于 SyncAdapter 。添加这个方法到的ContentProvider 考进来的URI

  / **
 *确定给定URI指定该请求是从同步适配器到来。
 *参数URI将给定的URI
 * @返回true,如果URI指定请求从同步适配器来了
 * /
私人布尔callerIsSyncAdapter(URI URI){
    最后弦乐is_sync_adapter = uri.getQueryParameter(CALLER_IS_SYNC_ADAPTER);
    返回is_sync_adapter = NULL和放大器;!&安培; !is_sync_adapter.equals(0);
}
 

那么你可以做

 的getContext()getContentResolver()有NotifyChange(URI,观察者,callerIsSyncAdapter(URI)!)。
 

当你需要发送更改通知。

如果要计划同步进行周期性的频率(轮询服务器)与 ContentResolver.setSyncAutomatically(...)通话添加此执行。

  ContentResolver.addPeriodicSync(账号,权限,新的捆绑(),frequency_in_seconds)
 

I have a SyncAdapter for my app, and an AccountManager to add my apps accounts to the Android Account Manager. My code for when I add an account to the Account Manager looks like:

Bundle data = new Bundle(5);
data.putString(_PEOPLE_ID, people_id);
data.putString(_FIRST_NAME, first_name);
data.putString(_LAST_NAME, last_name);
data.putString(_PLAN, plan);
data.putString(_BIRTHDAY, birthday);
Account account = new Account(username, _ACCOUNT_TYPE);
try {
    boolean created;
    created = _account_manager.addAccountExplicitly(account,
                                   _cryptography.encrypt(_SEED, password), data);
    response.accountCreated(created);
    _account_manager.setAuthToken(account, _TOKEN_TYPE, session_token);
    _model.updateActiveAccount(people_id, username, password);
    SharedPreferences.Editor settings = _settings.edit();
    settings.putString(_ACCOUNT_TYPE, account.name);
    settings.putString(_TOKEN_TYPE, session_token);
    settings.commit();
    // Tells the content provider that it can sync this account
    ContentResolver.setIsSyncable(account, AUTHORITY, 1);
    final Bundle extras = new Bundle(1);
    extras.putBoolean(SYNC_EXTRAS_INITIALIZE, true);
    ContentResolver.addPeriodicSync(account, AUTHORITY, extras, 900);
} catch (Exception e) {
    Ln.e(e.getCause());
}

I can add the account to the Account Manager successfully through Settings, but I have to manually enable syncing for the account in Settings also, even though background data and sync automatically settings are enabled on the emulator. If I manually enable syncing, then the sync is performed fine, it just isn't started by default.

解决方案

ContentResolver.setIsSyncable(account, AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);

As Blehi said will initiate the automatic syncing of the given account, given the global Settings, "Background data" and "Auto-sync" are enabled.

To prevent the back-to-back syncing (from jcwenger) make sure that if any method in the SyncAdapter.onPerformSync(...) calls ContentResolver.notifyChange(...) it uses ContentResolver.notifyChange(uri, observer, false) to flag the notify not to trigger a sync call (the third parameter is syncToNetwork).

If you're using the ContentProvider to perform the inserts/deletes/updates for you SyncAdapter it makes sense to call ContentResolver.notifyChange(...) so that while the app is visible the user can receive updates from the SyncAdapter, which means you will have the ContentProvider making ContentResolver.notifyChange(...) calls. To get this set up to work, I've added (following the dev guide) CALLER_IS_SYNC_ADAPTER query parameter to every URI that is used for the SyncAdapter. Add this method to the ContentProvider to test the incoming URIs

/**
 * Determines if the given URI specifies that the request is coming from the sync adapter.
 * @param uri the given URI
 * @return true if the uri specifies that the request is coming from the sync adapter
 */
private boolean callerIsSyncAdapter(Uri uri) {
    final String is_sync_adapter = uri.getQueryParameter(CALLER_IS_SYNC_ADAPTER);
    return is_sync_adapter != null && !is_sync_adapter.equals("0");
}  

then you can do

getContext().getContentResolver().notifyChange(uri, observer, !callerIsSyncAdapter(uri));

whenever you need send a change notification.

And if you want to schedule the sync to be executed periodically at a frequency (polling the server) add this with the ContentResolver.setSyncAutomatically(...) call.

ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), frequency_in_seconds)

这篇关于Android的SyncAdapter自动初始化正在同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆