Currently I am working on the widget that has several toggle features, and one of them is Auto-Sync toggle. I managed to implement enabling and disabling of the Auto-Sync using flowing 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;
}
}
Beside that, I need to register broadcast receiver that will detect Auto-Sync state changed from other apps (either from settings or via other widget). Is there some action that can be added to intent filter in manifest?
Other solution that is possible solution is to implement SyncStatusObserver interface
and use
public void onStatusChanged(int which);
Object handleObject = ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
new SyncStatusObserver() {
@Override
public void onStatusChanged(int which) {}
);
}
But problem with this solution is that every time I launch some other app or turn off/on screen that listener unregistered it self, and handle object becomes null.
Is there any workarounds?
**====== Latest update (workaround) ====== **
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) {
}
}
}