Search code examples
javaandroidandroid-syncadapter

How to trigger onPerformSync when a certain variable value changes


I'm developing an app which contains updating profile picture. What I'm doing is the following: I'm storing the user's profile URI using sharedPreferences, and when the user updates their profile image, I want to run syncAdapter to sync the profile pic to the server. However, syncing works fine when using refresh button in the settings, but it is never triggered when using requestSync.

I've tried using requestSync and using the flags SYNC_EXTRAS_MANUAL and SYNC_EXTRAS_EXPEDITED, but id didn't work. Here's the code I tried:

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;
    }
}

Here is the function that should triggers 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);
}

I really appreciate your help, thanks in advance


Solution

  • I figured out the problem, I was wrong about the Auothority, it wasn't the same as the one declared in the sync adapter xml.