0

I'm trying to force my application to perform a sync every second, or just every 4 or 5 second. However, the minimum period i can make the syncadapter sync is 30-60 seconds. How can archive such a behavior?

No matter what i set i the second parameter in addPeriodicSync(), it won't be below 30 seconds. ContentResolver.setMasterSyncAutomatically(true); ContentResolver.setIsSyncable(mAccount, AUTHORITY, 1);

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

I am aware that this is a bad behavior for an application as it will drain the battery, and that GCM should have been used to create pushes from the server. The application is for a university project presentation so i need to responsive and presentable.

Edit:

I am aware of the possibility of manual sync :):

Thanks in advance.

user1868569
  • 23
  • 1
  • 7

3 Answers3

0

I would just assume that a SyncAdapter cannot be used in this way, since it is specifically designed to not work this way.

If you need an app to perform a sync every second, you should probably just implement an IntentService that restarts itself with a delayed intent (where you can set the delay to 1 second), and do the sync there, rather than in a SyncAdapter.

EDIT: Here's a sample implementation, please don't actually use this for anything but a demonstration though, I feel a little dirty for writing it.

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
    }

}
Thorbear
  • 2,303
  • 28
  • 23
0

You cannot schedule a sync at intervals under 60 seconds.

You can confirm this by reading the code, or by reviewing this SO answer

Community
  • 1
  • 1
G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
0

I have done this way:

Add permission <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" /> in Manifest file.

Add below method for Sync:

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

Hope this should help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151