其他分享
首页 > 其他分享> > PeriodicSync在android kitkat和棒棒糖中不起作用

PeriodicSync在android kitkat和棒棒糖中不起作用

作者:互联网

我每12小时在应用程序中执行一次同步,之前我在android 4.4以下版本中尝试过,同步适配器可以正常工作,但是kitkat和periodsyncsync甚至都无法触发,请帮帮我.

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

解决方法:

我已经解决了这个问题,对于kitkat及以上,我们需要编写单独的代码

码:

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

标签:android-syncadapter,android
来源: https://codeday.me/bug/20191028/1955801.html