应用程序进程终止时,SyncAdapter进程终止

Asa*_*afK 5 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

kan*_*idj 3

从文档中:

框架可以随时取消同步。例如,非用户启动且持续时间超过 30 分钟的同步将被视为超时并取消。同样,框架将尝试通过在一分钟内监视适配器的网络活动来确定适配器是否正在取得进展。如果此窗口上的网络流量足够接近零,同步将被取消。cancelSync(Account, String)您还可以通过或请求取消同步cancelSync(SyncRequest)

interrupt()通过在同步线程上发出 a 来取消同步。您的代码onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)必须检查interrupted(),或者您必须覆盖onSyncCanceled(Thread)/之一onSyncCanceled()(取决于您的适配器是否支持并行同步多个帐户)。如果您的适配器不尊重框架发出的取消,您将面临应用程序整个进程被终止的风险。

您确定遵守 SyncAdapter 框架的规则吗?

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