首页 > 解决方案 > 当某个变量值改变时如何触发onPerformSync

问题描述

我正在开发一个包含更新个人资料图片的应用程序。我正在做的事情如下:我正在使用 存储用户的个人资料 URI sharedPreferences,当用户更新他们的个人资料图片时,我想运行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);
}

非常感谢您的帮助,在此先感谢

标签: javaandroidandroid-syncadapter

解决方案


我发现了问题,我对权威的看法是错误的,它与同步适配器 xml 中声明的不一样。


推荐阅读