目前我正在处理具有多个切换功能的小部件,其中之一是自动同步切换. 我设法使用流动代码实现自动同步的启用和禁用.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
除此之外,我需要注册广播接收器,它将检测从其他应用程序(从设置或通过其他小部件)更改的自动同步状态. 是否有一些操作可以添加到清单中的intent过滤器?
可能解决方案的其他解决方案是implement SyncStatusObserver interface使用
public void onStatusChanged(int which); …Run Code Online (Sandbox Code Playgroud)