Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PeriodicSync is not working in android kitkat and lollipop

I am performing sync in my application for every 12 hours ,earlier i tried in android 4.4 below versions sync adapter is working fine, but kitkat and above periodicsync is not even triggering please help me.

public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
    Account account = getSyncAccount(context);
    String authority = context.getString(R.string.content_authority);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // we can enable inexact timers in our periodic sync
        SyncRequest request = new SyncRequest.Builder().
                syncPeriodic(syncInterval, flexTime).
                setSyncAdapter(account, authority).build();
        ContentResolver.requestSync(request);
    } else {
        ContentResolver.addPeriodicSync(account,
                authority, new Bundle(), syncInterval);
    }
}
like image 594
skyshine Avatar asked Dec 03 '25 14:12

skyshine


1 Answers

I have solved this question,for kitkat and above we need to write separate code

code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            SyncRequest.Builder b = (new SyncRequest.Builder()).syncPeriodic(syncInterval, flexTime);
            b.setSyncAdapter(account, authority);
            b.setExtras(new Bundle());
            ContentResolver.requestSync(b.build());
        } else {
            ContentResolver.addPeriodicSync(account, authority, new Bundle(),
                    syncInterval);
like image 143
skyshine Avatar answered Dec 05 '25 03:12

skyshine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!