5

現在、いくつかのトグル機能を持​​つウィジェットに取り組んでおり、そのうちの 1 つが自動同期トグルです。 フローコードを使用して、自動同期の有効化と無効化を実装することができました。

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

}

それに加えて、他のアプリ (設定または他のウィジェットを介して) から変更された自動同期状態を検出するブロードキャスト レシーバーを登録する必要があります。 マニフェストのインテント フィルターに追加できるアクションはありますか?

可能な解決策である他の解決策はimplement SyncStatusObserver interface、使用することです

public void onStatusChanged(int which);

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

しかし、このソリューションの問題は、他のアプリを起動したり、画面をオフ/オンにしたりするたびに、リスナーがそれ自体を登録解除し、ハンドル オブジェクトが null になることです。

回避策はありますか?

**====== 最新のアップデート(回避策) ====== **

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) {    
        }        
    }
}
4

0 に答える 0