如何在某个变量值变化时触发onPerformSync
How to trigger onPerformSync when a certain variable value changes
我正在开发一个包含更新个人资料图片的应用程序。我正在做的是以下内容:我正在使用 sharedPreferences
存储用户的个人资料 URI,当用户更新他们的个人资料图片时,我想 运行 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);
}
非常感谢您的帮助,提前致谢
我找到问题了,我错了 Auothority,它与同步适配器中声明的不一样 xml。
我正在开发一个包含更新个人资料图片的应用程序。我正在做的是以下内容:我正在使用 sharedPreferences
存储用户的个人资料 URI,当用户更新他们的个人资料图片时,我想 运行 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);
}
非常感谢您的帮助,提前致谢
我找到问题了,我错了 Auothority,它与同步适配器中声明的不一样 xml。