【问题标题】:Android how to listen to sync turned on in accounts and settingsAndroid如何收听同步在帐户和设置中打开
【发布时间】:2013-09-05 11:03:18
【问题描述】:

我的应用程序有 2 种自动同步模式(所有网络/仅 WiFi)。

我想监听用户何时在 Android 设置(不是我的应用程序设置)中打开自动同步,然后相应地设置我的自动模式。

当用户在 Android 设置中打开同步时,我如何收听?

【问题讨论】:

    标签: android settings sync


    【解决方案1】:

    这是存储该复选框值的内容提供程序。

    @Override

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

    这是你需要做一些额外工作的地方。设置总是将每个值保存到共享首选项。您需要从共享偏好中找到该值并注册该共享偏好更改侦听器。我不知道是否可以收听该同步复选框。

    这是您找到保存值的复选框或切换首选项的链接。 分析FirstSecond链接。

    还有一点需要注意的是,设置代码在这个表中保存了一些值。

    First table

    Second table

    Third table

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多