How to detect auto-sync status changed through a broadcast receiver

I am currently working on widgets that have several features , and one of them is the Auto-Sync switch. I was able to implement enabling and disabling auto-sync using the current code.

public class AutoSyncUtility {

    private static final String TAG = "Toggler_AutoSyncUtility";

    public static void setEnabled(Context context, boolean isEnabled) {
        Log.d(TAG, "Called setEnabled");

        try {
            ContentResolver.setMasterSyncAutomatically(isEnabled);
        } catch (Exception exception) {
            Log.e(TAG, Log.getStackTraceString(exception));
        }
    }

    public static boolean isEnabled(Context context) {
        Log.d(TAG, "Called isEnabled");
        boolean isEnabled = false;
        try {
                isEnabled = ContentResolver.getMasterSyncAutomatically();
        } catch (Exception exception) {
            Log.e(TAG, Log.getStackTraceString(exception));
        }

        return isEnabled;
    }

}

In addition, I need to register a broadcast receiver that will determine the auto-synchronization state changed from other applications (either from the settings or through another widget). Is there any action that can be added to the intent filter in the manifest?

Another solution, which is a possible solution, is to implement SyncStatusObserver interfaceuse

public void onStatusChanged(int which);

Object handleObject = ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, 
    new SyncStatusObserver() {
        @Override
        public void onStatusChanged(int which) {}
    );
}

, , - / , , .

- ?

** ====== ( ) ====== **

public class AutoRotateContentObserver extends ContentObserver {


    public static interface AutoRotateStateChangedListener {
        void autoRotateStateChanged();
    }

    private AutoRotateStateChangedListener mListener;

    public AutoRotateContentObserver(Handler handler,
            AutoRotateStateChangedListener listener) {
        super(handler);
        mListener = listener;
    }

    @Override
    public void onChange(boolean selfChange) {
        mListener.autoRotateStateChanged();
        super.onChange(selfChange);

    }
}


public class MyService extends Service implements
        AutoRotateStateChangedListener {
    private AutoRotateContentObserver mAutoRotateContentObserver;

    @Override
    public void onCreate() {
        super.onCreate();

        mAutoRotateContentObserver = new AutoRotateContentObserver(new Handler(), this);

        getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
                false, mAutoRotateContentObserver);
    }

    @Override
    public void onDestroy() {
        getContentResolver().unregisterContentObserver(mAutoRotateContentObserver);
        super.onDestroy();
    }

    @Override
    public void autoRotateStateChanged() {         
        // doSomething();
    }
}


public class AutoRotateUtility {

    public static boolean isEnabled(Context context) {        
        try {
            return (Settings.System.getInt(context.getContentResolver(),
                    Settings.System.ACCELEROMETER_ROTATION) != 0) ? true : false;            
        } catch (SettingNotFoundException e) {
        }        
        return false;
    }

    public static void setEnabled(Context context, boolean isEnabled) {
        try {
            Settings.System.putInt(context.getContentResolver(),
                    Settings.System.ACCELEROMETER_ROTATION, isEnabled ? 1 : 0);                        
        } catch (Exception e) {    
        }        
    }
}
+5

All Articles