gpt4 book ai didi

java - 如何在某个变量值变化时触发onPerformSync

转载 作者:行者123 更新时间:2023-11-30 04:58:22 24 4
gpt4 key购买 nike

我正在开发一个包含更新个人资料图片的应用程序。我正在做的是:我正在使用 sharedPreferences 存储用户的个人资料 URI,当用户更新他们的个人资料图片时,我想运行 syncAdapter 来同步服务器的个人资料图片。但是,在设置中使用刷新按钮时同步工作正常,但在使用 requestSync 时永远不会触发。

我已尝试使用 requestSync 并使用标志 SYNC_EXTRAS_MANUALSYNC_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);
}

非常感谢您的帮助,提前致谢

最佳答案

我弄清楚了问题所在,我对 Auothority 的理解是错误的,它与同步适配器 xml 中声明的不一样。

关于java - 如何在某个变量值变化时触发onPerformSync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58732141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com