【问题标题】:SyncAdapter - How to Periodic sync every secondSyncAdapter - 如何每秒定期同步
【发布时间】:2016-05-22 22:18:37
【问题描述】:

我试图强制我的应用程序每秒执行一次同步,或者每 4 或 5 秒执行一次。但是,我可以使同步适配器同步的最短时间是 30-60 秒。如何归档这样的行为?

无论我在 addPeriodicSync() 中设置的第二个参数是什么,它都不会低于 30 秒。 ContentResolver.setMasterSyncAutomatically(true); ContentResolver.setIsSyncable(mAccount, AUTHORITY, 1);

    ContentResolver.setSyncAutomatically(mAccount, AUTHORITY, true);
    ContentResolver.addPeriodicSync(
            mAccount,
            AUTHORITY,
            Bundle.EMPTY,
            4);

我知道这对应用程序来说是一种不良行为,因为它会耗尽电池电量,并且应该使用 GCM 从服务器创建推送。 该应用程序是针对大学项目演示的,因此我需要反应灵敏且表现出色。

编辑:

我知道手动同步的可能性:):

提前致谢。

【问题讨论】:

标签: android long-polling polling android-syncadapter


【解决方案1】:

您不能以低于 60 秒的间隔安排同步。

您可以通过阅读代码或查看此SO answer 来确认这一点

【讨论】:

    【解决方案2】:

    我是这样做的:

    在 Manifest 文件中添加权限 <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

    为同步添加以下方法:

    public void syncAllAccountsPeriodically(Context contextAct, long seconds) throws Exception {
        AccountManager manager = AccountManager.get(contextAct);
        Account[] accounts = manager.getAccountsByType("com.google");
        String accountName = "";
        String accountType = "";
        for (Account account : accounts) {
            accountName = account.name;
            accountType = account.type;
            break;
        }
    
        Account a = new Account(accountName, accountType);
        ContentResolver.addPeriodicSync(a, AUTHORITY,
            Bundle.EMPTY, seconds*1000);
    }
    

    希望这对你有帮助。

    【讨论】:

      【解决方案3】:

      我只是假设不能以这种方式使用 SyncAdapter,因为它是专门设计为不能以这种方式工作的。

      如果您需要一个应用每秒执行一次同步,您可能应该只实现一个 IntentService 以延迟意图(您可以将延迟设置为 1 秒)重新启动自身,然后在那里进行同步,而不是在一个 SyncAdapter。

      编辑:这是一个示例实现,请不要将其用于演示,但我觉得写它有点脏。

      public class ContinuousSyncService extends IntentService {
      
          private static final int DELAY = 1000; // ms
      
          public ContinuousSyncService() {
              super(ContinuousSyncService.class.getName());
          }
      
          public static PendingIntent getPendingIntent(@NonNull Context context) {
              Intent intent = new Intent(context, ContinuousSyncService.class);
              return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          }
      
          private void scheduleNextStart(long delay) {
              ((AlarmManager) getSystemService(Context.ALARM_SERVICE)).set(
                      AlarmManager.ELAPSED_REALTIME,
                      SystemClock.elapsedRealtime() + delay,
                      getPendingIntent(this));
          }
      
          @Override
          protected void onHandleIntent(final Intent intent) {
              sync();
              scheduleNextStart(DELAY);
          }
      
          private void sync() {
              // Either use ContentResolver.requestSync()
              // Or just put the code from your SyncAdapter.onPerformSync() here
          }
      
      }
      

      【讨论】:

      • 你能举一个存档的例子吗? :)
      • @user1868569 添加了示例实现。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多