我在我的项目中使用同步适配器,它将定期同步.要为同步适配器创建帐户,我使用以下代码.
我面临的问题是此代码触发了初始同步.文档中没有提到此代码将使同步最初运行.
事实上即使在谷歌示例项目中也有额外的代码用于触发我已删除的初始同步.
我使用了此示例中的代码:http: //developer.android.com/samples/BasicSyncAdapter/index.html
即使我添加命令ContentResolver.cancelSync(account,null); 同步适配器仍然运行.
如何阻止同步适配器最初同步.它应该在同步间隔周期过去后第一次同步.
Account account = new Account(context.getPackageName(), context.getPackageName());
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
if (accountManager.addAccountExplicitly(account, null, null)) {
// Inform the system that this account supports sync
ContentResolver.setIsSyncable(account, context.getPackageName(), 1);
// Inform the system that this account is eligible for auto sync when the network is up
ContentResolver.setSyncAutomatically(account, context.getPackageName(), true);
// Recommend a schedule for automatic synchronization.
// The system may modify this based
// on other scheduled syncs and network utilization. …
Run Code Online (Sandbox Code Playgroud) 我在活动中使用下载管理器来下载文件。我想使用广播接收器接收ACTION_DOWNLOAD_COMPLETE意图,并将下载请求的ID与下载管理器设置的意图中的ID匹配,以检查是否相同。
有两种选择:
1)通过创建意图过滤器并注册广播接收器来接收活动中的意图。这里的问题是广播接收机存在于活动中,并随活动而被销毁。因此,如果在销毁活动之前未完成下载,则无法接收意图。
this.downloadId = manager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
在onReceive方法中,将downloadID与意图的EXTRA_DOWNLOAD_ID进行比较,以检查下载是否相同。在onReceive方法中可以访问downloadId变量。
2)通过清单接收广播。这里的问题是onReceive方法无法访问活动内设置的downloadId变量。因此,我无法检查下载是否是我需要的下载。
我做错了吗?基本上,我想知道下载是否成功完成,即使下载开始的活动/服务被破坏了。
我希望我的问题很清楚。
android ×2