我给出的更新间隔时间是30秒但是适配器在30秒内没有同步,第二个奇怪的行为是它在启动时同步了两次,我已经注册了所有的xml并且还在清单中注册了服务不会在后台同步
public class WeatherSyncAdapter extends AbstractThreadedSyncAdapter {
private static final int WEATHER_NOTIFICATION_ID = 5004;
private static final int SYNC_INTERVAL = 30;
private static final int SYNC_FLEXTIME = SYNC_INTERVAL/3;
private static final String[] NOTIFY_WEATHER_PROJECTION = new String[] {
weathertablecoloum.mWeatherIconId,
weathertablecoloum.mMaxTemp,
weathertablecoloum.mMinTemp,
weathertablecoloum.mCondition
};
public final String LOG_TAG = WeatherSyncAdapter.class.getSimpleName();
public WeatherSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.d(LOG_TAG,"Sync Started");
try {
boolean t = GetList.makelist(getContext());
} catch (JSONException e) {
e.printStackTrace();
}
notifyWeather();
}
public static Account getSyncAccount(Context context) {
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Account newAccount = new Account(context.getString(R.string.app_name), context.getString(R.string.sync_account_type));
if ( null == accountManager.getPassword(newAccount) ) {
if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
return null;
}
onAccountCreated(newAccount, context);
}
return newAccount;
}
public static void syncImmediately(Context context) {
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context), context.getString(R.string.content_authority), bundle);
}
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) {
SyncRequest request = new SyncRequest.Builder().
syncPeriodic(syncInterval, flexTime).
setSyncAdapter(account, authority).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account,
authority, new Bundle(), syncInterval);
}
}
private static void onAccountCreated(Account newAccount, Context context) {
WeatherSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true);
//syncImmediately(context);
}
public static void initializeSyncAdapter(Context context) {
getSyncAccount(context);
}
private void notifyWeather() {
Context context = getContext();
Uri weatherUri = Uri.withAppendedPath(weathertable.mContentUri,"");
Cursor cursor = context.getContentResolver().query(weatherUri, null, null, null, null);
if (cursor.moveToFirst()) {
int weatherId = cursor.getInt(cursor.getColumnIndex(weathertablecoloum.mWeatherIconId));
double high = cursor.getDouble(cursor.getColumnIndex(weathertablecoloum.mMaxTemp));
double low = cursor.getDouble(cursor.getColumnIndex(weathertablecoloum.mMinTemp));
String desc = cursor.getString(cursor.getColumnIndex(weathertablecoloum.mCondition));
int iconId = R.mipmap.ic_launcher;
String title = context.getString(R.string.app_name);
String contentText = String.format(context.getString(R.string.format_notification), desc, String.valueOf(high), String.valueOf(low));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext()).setSmallIcon(iconId).setContentTitle(title).setContentText(contentText);
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(WEATHER_NOTIFICATION_ID, mBuilder.build());
}
}
}