android - 应用进程终止时 SyncAdapter 进程终止

标签 android android-syncadapter

为什么当应用程序从应用程序切换器列表中滑动时,SyncAdapter 进程 (:sync) 被终止?我认为这里的全部目的是让它们分离。

编辑:

以下是使用的代码。 mUploadTask 是一个 AsyncTask 即时执行,它从 sqlite 表中读取信息(使用 getContext().getContentResolver())并将相关数据上传到后端(使用 HttpPost)。非常直接。

此外,我只实现了一个onSyncCanceled(),因为我的SyncAdapter 不支持并行同步多个帐户。

public class SyncAdapter extends AbstractThreadedSyncAdapter implements UploadTaskListener {

private static final String TAG = SyncAdapter.class.getSimpleName();

private static volatile UploadTask mUploadTask;

/**
 * Set up the sync adapter
 */
public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
}

/**
 * Set up the sync adapter. This form of the
 * constructor maintains compatibility with Android 3.0
 * and later platform versions
 */
public SyncAdapter(
        Context context,
        boolean autoInitialize,
        boolean allowParallelSyncs) {
    super(context, autoInitialize, allowParallelSyncs);
}

@Override
public void onPerformSync(Account account, Bundle extras, String authority,
        ContentProviderClient provider, SyncResult syncResult) {

    MHLog.logI(TAG, "onPerformSync");

    ContentResolver.setSyncAutomatically(account, authority, true);

    if (mUploadTask == null) {
        synchronized (SyncAdapter.class) {
            if (mUploadTask == null) {
                mUploadTask = new UploadTask(getContext(), this).executeOnSettingsExecutor();
                MHLog.logI(TAG, "onPerformSync - running");
            }
        }
    }
}

@Override
public void onSyncCanceled() {
    MHLog.logI(TAG, "onSyncCanceled");
    if(mUploadTask != null){
        mUploadTask.cancel(true);
        mUploadTask = null;
    }
}

最佳答案

来自文档:

Syncs can be cancelled at any time by the framework. For example a sync that was not user-initiated and lasts longer than 30 minutes will be considered timed-out and cancelled. Similarly the framework will attempt to determine whether or not an adapter is making progress by monitoring its network activity over the course of a minute. If the network traffic over this window is close enough to zero the sync will be cancelled. You can also request the sync be cancelled via cancelSync(Account, String) or cancelSync(SyncRequest).

A sync is cancelled by issuing a interrupt() on the syncing thread. Either your code in onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult) must check interrupted(), or you you must override one of onSyncCanceled(Thread)/onSyncCanceled() (depending on whether or not your adapter supports syncing of multiple accounts in parallel). If your adapter does not respect the cancel issued by the framework you run the risk of your app's entire process being killed.

您是否确保遵守 SyncAdapter 框架的规则?

此外,很高兴看到您的一些代码可以深入了解框架取消同步的原因...

关于android - 应用进程终止时 SyncAdapter 进程终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35042274/

相关文章:

Android: 如何在 ViewPager 中创建不同的 View ?

android - 如何获取android TextView的默认文本颜色?

带有 appengine Google 用户帐户的 Android 同步适配器

java - 在 Android ContentProvider 中从网络同步时防止网络同步循环

android - 单个应用程序中的多个同步适配器或仅 1 个

android - 如何从自定义帐户检索联系方式?

android - Eclipse 不会自动切换运行配置

java - Android 10 (Q) ACCESS_BACKGROUND_LOCATION 权限

安卓电视盒体验。

android - SampleSyncAdapter 断点不起作用