SyncAdapter没有得到号召"网络挠痒痒" [英] SyncAdapter not getting called on "Network tickle"
问题描述
概述
我follwed 谷歌的教程使用SyncAdapter不使用的ContentProvider,Authenticator..etc 。它完美的作品时,我称之为 onPerformSync(...)
,当我需要做一个上载经德SyncAdapter服务器。
I follwed Google's tutorial on using SyncAdapter without using ContentProvider, Authenticator..etc. It works perfectly when I call onPerformSync(...)
when I need to do an "upload" to the server via de SyncAdapter.
现在,你可以想像,我需要从服务器上做下载,以及(是的,我明白,这将是更好地使用谷歌的云端通讯系统,但这是建立我得到了,我可以' T改变这一点)。对于这一点,而不是做定期同步,我想利用网络挠痒痒Android系统执行时有可用的网络。对于我声明如下:
Now, as you can imagine, I need to do downloads from the server as well (yes I understand that it would be better to use Google's Cloud Messaing system, but this is the set up I was given, and I can't change that). For that, instead of doing periodical syncs, I want to make use of the "Network tickle" Android system carries out when there is a network available. For that I state the following:
ContentResolver.setIsSyncable(accounts[0], AUTHORITY, 1);
ContentResolver.setSyncAutomatically(accounts[0], AUTHORITY, true);
但我SyncAdapter 只是没有得到所谓的。寻找其他StackOverflow的问题,似乎有一个问题,如果目标定位API 10或以下,SyncAdapter而且你必须在添加帐户明确的方法之前调用。所以,我结束了这一点:
But my SyncAdapter is just not getting called. Looking into other stackOverFlow questions, there seem to be a problem if targetting API 10 or below with SyncAdapter and that you must add an account explicitly before calling the before methods. So I ended up with this:
AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
Account[] accounts = accountManager.getAccounts();
if(accounts.length == 0){ //ADD DUMMY ACCOUNT
Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
ContentResolver.setIsSyncable(accounts[0], AUTHORITY, 1);
ContentResolver.setSyncAutomatically(accounts[0], AUTHORITY, true);
accountManager.addAccountExplicitly(newAccount, null, null);
}else{
accounts = accountManager.getAccounts();
ContentResolver.setIsSyncable(accounts[0], AUTHORITY, 1);
ContentResolver.setSyncAutomatically(accounts[0], AUTHORITY, true);
}
现在这个code被执行时,在用户登录,或者如果应用程序被打死,再次启动。我想知道,我应该叫 setIsSyncable
和 setSyncAutomatically
只有当我加入dummyAccount的第一次?
Now this code gets executed when the user signs in, or if the application was killed and is started up again. I am wondering, should I call setIsSyncable
and setSyncAutomatically
only when I add the dummyAccount the very first time?
此外,在SyncAdapter的goodiness的部分是,它会保持在一个异常的情况下,使得调用。但我不明白这是如何去一下,所以不是我有这样的:
Also, part of the "goodiness" of the SyncAdapter is that it will keep on making the calls in case of an exception. But I don't quite understand how this goes about, so instead I have this:
private void profileUpdate(){
TableAccounts db = TableAccounts.getInstance(getContext());
boolean isRecordDirty = db.isRecordDirty(signedInUser);
if(isRecordDirty){
if(server.upDateUserProfile(signedInUser)){
db.cleanDirtyRecord(signedInUser);
turnOffPeriodicSync();
}else{
this.turnOnPeriodicSync(this.sync_bundle);
}
}else
turnOffPeriodicSync();
}
正如你所看到的,这取决于我上传到服务器的结果,我打开或关闭周期同步。
As you can see, depending on the result of my upload to the server, I turn on or off a periodic sync.
推荐答案
由于accountManager.getAccounts []返回设备上的每个帐户,我觉得没有什么保证该帐户[0]为您的应用程序的帐户(即,有你的包名ACCOUNT_TYPE)。
- 你可以在任何情况下调用addAccountExplicitly(),如果存在,则什么也不会发生。
Since the accountManager.getAccounts[] return every account on the device, I think nothing guarantee that the account[0] is your app's account (aka, has the ACCOUNT_TYPE of your package name). -- You could call addAccountExplicitly() in any case, if it is existed, then nothing happens.
Account account = new Account(ACCOUNT, ACCOUNT_TYPE);
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
accountManager.addAccountExplicitly(account, null, null)
context.getContentResolver().setSyncAutomatically(account, AUTHORITY, true);
这篇关于SyncAdapter没有得到号召"网络挠痒痒"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!