java - 当某个变量值改变时如何触发onPerformSync
问题描述
我正在开发一个包含更新个人资料图片的应用程序。我正在做的事情如下:我正在使用 存储用户的个人资料 URI sharedPreferences
,当用户更新他们的个人资料图片时,我想运行syncAdapter
以将个人资料图片同步到服务器。但是,在设置中使用刷新按钮时同步工作正常,但使用requestSync
.
我尝试使用 requestSync 并使用标志SYNC_EXTRAS_MANUAL
和SYNC_EXTRAS_EXPEDITED
,但 id 不起作用。这是我尝试过的代码:
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
switch(requestCode) {
case PICK_IMAGE_ID:
selectedImage = null;
try {
selectedImage = ImagePicker.getImageFromResult(getContext(), resultCode, imageReturnedIntent);
} catch (IOException e) {
e.printStackTrace();
}
Glide.with( this )
.load( selectedImage )
.into( imageButton );
SharedPreferences login = getContext().getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = login.edit();
String exProfileUri = login.getString( USER_PROFILE_PIC,USER_PROFILE_PIC );
Log.i( "exprofile"," " + exProfileUri );
//If user picks an image for their profile
if(selectedImage != null) {
editor.putString( USER_PROFILE_PIC,
selectedImage.toString());
editor.apply();
//We should then trigger SyncAdapter
syncOnProfileImageChanged();
}
}
else if(selectedImage == null && exProfileUri != null){
editor.putString( USER_PROFILE_PIC, exProfileUri);
editor.apply();
}
break;
default:
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
break;
}
}
这是应该触发 onPerformSync 的函数: public void SyncProfileImage(){
mAccount = new Account(
ACCOUNT, ACCOUNT_TYPE);
Log.i( "exprofile","New profile image should be synced now... " + mAccount );
Bundle settingsBundle = new Bundle( );
settingsBundle.putBoolean( ContentResolver.SYNC_EXTRAS_MANUAL, true ); settingsBundle.putBoolean( ContentResolver.SYNC_EXTRAS_MANUAL, true );
settingsBundle.putBoolean( ContentResolver.SYNC_EXTRAS_EXPEDITED, true );
ContentResolver.setIsSyncable( mAccount, AUTHORITY, 1 );
if(ContentResolver.isSyncPending( mAccount, AUTHORITY )||
ContentResolver.isSyncActive( mAccount, AUTHORITY )){
Log.i( "ContentResolver","SyncPending, calceling" );
ContentResolver.cancelSync( mAccount, AUTHORITY );
}
ContentResolver.setIsSyncable( mAccount, AUTHORITY, 1 );
ContentResolver.setSyncAutomatically( mAccount,AUTHORITY, true );
ContentResolver.requestSync( mAccount, AUTHORITY, settingsBundle);
}
非常感谢您的帮助,在此先感谢
解决方案
我发现了问题,我对权威的看法是错误的,它与同步适配器 xml 中声明的不一样。
推荐阅读
- flutter - 如何正确模拟 Flutter Fire Firebase 函数?
- php - 如何使用日光浴室按日期过滤
- javascript - jQuery:删除动态添加的项目后重新索引顺序
- flutter - 应用程序空闲较长时间后通知丢失 - 一个信号 sdk 在颤动
- inno-setup - Inno 设置:在 .net dll 中添加第三方引用不起作用
- google-play - App Store 和 Google Play 大版本更新如何收费?
- php - 如何使用 Yii2 Query Builder 获取数据
- git - 是否有任何 gitlab 方式来压缩来自功能分支的任何提交但保留提交所有者?
- visual-studio - 为什么使用 Xamarin 的 Mac OS 社区的 Visual Studio 2019(最新)中的错误列表没有清除
- next.js - getStaticPaths 和 getStaticProps 与域调味