Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Account.setPassword导致SyncAdapter无限循环_Android_Android Syncadapter - Fatal编程技术网

Android Account.setPassword导致SyncAdapter无限循环

Android Account.setPassword导致SyncAdapter无限循环,android,android-syncadapter,Android,Android Syncadapter,考虑到android SyncAdapter的无限循环,有很多问题: ,但没有一个描述我遇到的问题 我正在将我的同步设置为: ContentResolver.setIsSyncable(account, AppConstants.AUTHORITY, 1); ContentResolver.setSyncAutomatically(account, AppConstants.AUTHORITY, true); ContentResolver.addPeriodicSync(account, Ap

考虑到android SyncAdapter的无限循环,有很多问题: ,但没有一个描述我遇到的问题

我正在将我的同步设置为:

ContentResolver.setIsSyncable(account, AppConstants.AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, AppConstants.AUTHORITY, true);
ContentResolver.addPeriodicSync(account, AppConstants.AUTHORITY, Bundle.EMPTY, 60);
我的同步适配器支持上载android:supportsUploading=true,这意味着在我的ContentProvider中,我必须检查数据更改是否来自我的同步适配器,如果来自我的同步适配器,则我会通知更改,而不请求同步到网络

boolean syncToNetwork = false;
getContext().getContentResolver().notifyChange(uri, null, syncToNetwork);

我的同步适配器仍然在一个恒定的循环中运行,触发另一个同步还有什么其他原因呢?

在每次同步中,我都向服务器请求数据。对于每个请求,我都从我的客户机获得一个访问令牌。我没有在我的帐户中保存密码,而是决定保存Oauth2刷新令牌,然后可以使用该令牌刷新访问令牌。对于每个刷新的访问令牌,服务器还发送一个新的刷新令牌,然后我将其更新到我的帐户:

accountManager.setPassword(account, refreshToken);
这就是问题所在。通过查看AOSP代码,我在中发现了以下BroadcastReceiver:

所以,它所做的是,在每个帐户上更改添加、删除、设置密码发送中的广播,以触发所有SyncAdapter的同步,而不仅仅是您自己的同步

老实说,我不知道原因是什么,但我可以把它看作是可利用的——我让我的手机和我的应用程序在无限循环中运行了一夜,第二天早上电池耗尽了,还有我的FUP——只有谷歌的文档、幻灯片和工作表应用程序消耗了143MB

private BroadcastReceiver mAccountsUpdatedReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
          updateRunningAccounts();

          // Kick off sync for everyone, since this was a radical account change
          scheduleSync(null, UserHandle.USER_ALL, null, null, 0 /* no delay */, false);
     }
};