私のアプリケーションには 2 つの自動同期モードがあります (すべてのネットワーク/WiFi のみ)。
ユーザーが Android の設定 (アプリケーションの設定ではなく) で自動同期をオンにしたときにリッスンし、それに応じて自動モードを設定したいと考えています。
ユーザーが Android 設定で同期をオンにしたことをリッスンするにはどうすればよいですか?
そのチェック ボックスの値が格納されるコンテンツ プロバイダは次のとおりです。
@オーバーライド
public boolean [More ...] onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) {
if (preference instanceof SyncStateCheckBoxPreference) {
SyncStateCheckBoxPreference syncPref = (SyncStateCheckBoxPreference) preference;
String authority = syncPref.getAuthority();
Account account = syncPref.getAccount();
boolean syncAutomatically = ContentResolver.getSyncAutomatically(account, authority);
if (syncPref.isOneTimeSyncMode()) {
requestOrCancelSync(account, authority, true);
} else {
boolean syncOn = syncPref.isChecked();
boolean oldSyncState = syncAutomatically;
if (syncOn != oldSyncState) {
// if we're enabling sync, this will request a sync as well
ContentResolver.setSyncAutomatically(account, authority, syncOn);
// if the master sync switch is off, the request above will
// get dropped. when the user clicks on this toggle,
// we want to force the sync, however.
if (!ContentResolver.getMasterSyncAutomatically() || !syncOn) {
requestOrCancelSync(account, authority, syncOn);
}
}
}
これは、追加の作業を行う必要がある場所です。設定は常にすべての値を共有設定に保存します。共有設定からその値を見つけ、その共有設定変更リスナーに登録する必要があります。その同期チェックボックスをリッスンできるかどうかはわかりません。
これは、値が保存されているチェックボックスまたはスイッチ設定を見つけるリンクです。First Secondリンクを分析します。
もう 1 つ注意すべき点は、設定コードがこのテーブルに値を保存することです。