android – 当应用程序进程终止时,SyncAdapter进程被终止
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android – 当应用程序进程终止时,SyncAdapter进程被终止,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2670字,纯文字阅读大概需要4分钟。
内容图文
![android – 当应用程序进程终止时,SyncAdapter进程被终止](/upload/InfoBanner/zyjiaocheng/970/c4add59d1e554276a72e6fba55485486.jpg)
从应用程序切换器列表中刷出应用程序时,为什么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进程被终止全部内容,希望文章能够帮你解决android – 当应用程序进程终止时,SyncAdapter进程被终止所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。