PeriodicSync not working in android-kitkat and lollipop

I do sync in my app every 12 hours, before I tried in android 4.4 below version sync adapter works fine but kitkat and above periodicsync don't even start, 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);
    }
}

      

+3


source to share


1 answer


I solved this question, for kitkat and above we need to write a 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);

      

+3


source







All Articles