为什么当应用程序从应用程序切换器列表中滑动时,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)
orcancelSync(SyncRequest)
.A sync is cancelled by issuing a
interrupt()
on the syncing thread. Either your code inonPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)
must checkinterrupted()
, or you you must override one ofonSyncCanceled(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/