public static void configureSyncFor(Account account) { Log.d(TAG, "Configuring account sync"); ContentResolver.setIsSyncable(account, BuildConfig.PROVIDER_AUTHORITY_SYNC, 1); ContentResolver.setSyncAutomatically(account, BuildConfig.PROVIDER_AUTHORITY_SYNC, true); ContentResolver.addPeriodicSync(account, BuildConfig.PROVIDER_AUTHORITY_SYNC, new Bundle(), SYNC_PERIOD); }
@Test public void shouldSetIsSyncable() { assertThat(ContentResolver.getIsSyncable(a, AUTHORITY)).isEqualTo(-1); assertThat(ContentResolver.getIsSyncable(b, AUTHORITY)).isEqualTo(-1); ContentResolver.setIsSyncable(a, AUTHORITY, 1); ContentResolver.setIsSyncable(b, AUTHORITY, 2); assertThat(ContentResolver.getIsSyncable(a, AUTHORITY)).isEqualTo(1); assertThat(ContentResolver.getIsSyncable(b, AUTHORITY)).isEqualTo(2); }
//Create Account mAccount = createSyncAccount(this); //Turn on periodic syncing ContentResolver resolver = getContentResolver(); resolver.setIsSyncable(mAccount, AUTHORITY, 1); resolver.setSyncAutomatically(mAccount, AUTHORITY, true); resolver.addPeriodicSync( mAccount, AUTHORITY, Bundle.EMPTY, 30);
public void setIsSyncable (Account account, String authority, int syncable) { ContentResolver.setIsSyncable(account, authority, syncable); }
public void setAccountSyncable(MovirtAccount account, boolean syncable) { ContentResolver.setIsSyncable(account.getAccount(), OVirtContract.CONTENT_AUTHORITY, syncable ? 1 : 0); }
public void setSyncPeriodic(String authority, long interval_in_minute, long seconds_per_minute, long milliseconds_per_second) { Account account = mUser.getAccount(); Bundle extras = new Bundle(); this.setAutoSync(authority, true); ContentResolver.setIsSyncable(account, authority, 1); final long sync_interval = interval_in_minute * seconds_per_minute * milliseconds_per_second; ContentResolver.addPeriodicSync(account, authority, extras, sync_interval); }
/** * Schedule syncAdapter by interval * * @param intervalInMinute interval in minute */ protected boolean addPeriodicSync(int intervalInMinute) { boolean added = false; if (!periodicSyncExists(intervalInMinute)) { Bundle bundle = new Bundle(); // remove all others removePeriodicSync(); ContentResolver.setIsSyncable(mAccount, mAuthority, 1); ContentResolver.setSyncAutomatically(mAccount, mAuthority, true); long syncInSecond = intervalInMinute * SECONDS_PER_MINUTE; ContentResolver.addPeriodicSync( mAccount, mAuthority, bundle, syncInSecond); added = true; } return added; }
/** * Add account to Android system */ private static boolean addAccount(Context context) { Log.d(Constants.TAG, "Adding account..."); AccountManager am = AccountManager.get(context); if (am.addAccountExplicitly(CalendarController.ACCOUNT, null, null)) { // explicitly disable sync ContentResolver.setSyncAutomatically(ACCOUNT, CalendarContract.AUTHORITY, false); ContentResolver.setIsSyncable(ACCOUNT, CalendarContract.AUTHORITY, 0); return true; } else { return false; } }
/** * 利用帐号同步机制拉活 * @param context */ public static void startAccountSync(Context context){ String accountType = "com.henrik.keeplive"; AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); Account account = null; Account[] accounts = accountManager.getAccountsByType(accountType); if(accounts.length>0){ account = accounts[0]; }else { account = new Account("test", accountType); } if(accountManager.addAccountExplicitly(account,null,null)){ String authority = accountType; long sync_interval = 15*60; ContentResolver.setIsSyncable(account,authority,1); ContentResolver.setSyncAutomatically(account, authority, true); //自动同步 ContentResolver.addPeriodicSync(account, authority, new Bundle(), sync_interval);//设置同步时间间隔 } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_TIMEZONE, true); if (lastTimezone.length() == 0) retrieveTimezone(); if (Aware.DEBUG) Log.d(TAG, "Timezone service active..."); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), TimeZone_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), TimeZone_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), TimeZone_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_KEYBOARD, true); if (Aware.DEBUG) Log.d(TAG, "Keyboard service active..."); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Keyboard_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Keyboard_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Keyboard_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_NETWORK_EVENTS, true); if (Aware.DEBUG) Log.d(TAG, "Network service active..."); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Network_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Network_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Network_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_INSTALLATIONS, true); if (Aware.DEBUG) Log.d(TAG, "Installations service active..."); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Installations_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Installations_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Installations_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { if (mAccelerometer == null) { if (DEBUG) Log.d(TAG, "This device does not have an accelerometer sensor. Can't detect significant motion"); Aware.setSetting(this, Aware_Preferences.STATUS_SIGNIFICANT_MOTION, false); stopSelf(); } else { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_SIGNIFICANT_MOTION, true); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI, sensorHandler); isSignificantMotionActive = true; if (Aware.DEBUG) Log.d(TAG, "Significant motion service active..."); } if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Significant_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Significant_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Significant_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_BATTERY, true); if (Aware.DEBUG) Log.d(TAG, "Battery service active..."); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Battery_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Battery_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Battery_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_SCREEN, true); if (Aware.DEBUG) Log.d(TAG, "Screen service active..."); //We can only get the touch events if accessibility service is enabled. if (Aware.getSetting(this, Aware_Preferences.STATUS_TOUCH).equals("true")) { Applications.isAccessibilityServiceActive(this); } if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Screen_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Screen_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Screen_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_TELEPHONY, true); telephonyManager.listen(telephonyState, PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); if (Aware.DEBUG) Log.d(TAG, "Telephony service active..."); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Telephony_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Telephony_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Telephony_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); if (Aware.getSetting(getApplicationContext(), Aware_Preferences.STATUS_CALLS).equals("true")) { getContentResolver().registerContentObserver(Calls.CONTENT_URI, true, callsObs); telephonyManager.listen(phoneState, PhoneStateListener.LISTEN_CALL_STATE); } else { getContentResolver().unregisterContentObserver(callsObs); } if (Aware.getSetting(getApplicationContext(), Aware_Preferences.STATUS_MESSAGES).equals("true")) { getContentResolver().registerContentObserver(MESSAGES_CONTENT_URI, true, msgsObs); } else { getContentResolver().unregisterContentObserver(msgsObs); } if (Aware.DEBUG) Log.d(TAG, TAG + " service active..."); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), Communication_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), Communication_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), Communication_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; }
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (PERMISSIONS_OK) { DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true"); Aware.setSetting(this, Aware_Preferences.STATUS_ESM, true); if (Aware.getSetting(getApplicationContext(), Aware_Preferences.STATUS_ESM).equals("true")) { if (isESMWaiting(getApplicationContext()) && !isESMVisible(getApplicationContext())) { notifyESM(getApplicationContext(), true); } } if (DEBUG) Log.d(TAG, "ESM service active... Queue = " + ESM_Queue.getQueueSize(getApplicationContext())); if (Aware.isStudy(this)) { ContentResolver.setIsSyncable(Aware.getAWAREAccount(this), ESM_Provider.getAuthority(this), 1); ContentResolver.setSyncAutomatically(Aware.getAWAREAccount(this), ESM_Provider.getAuthority(this), true); long frequency = Long.parseLong(Aware.getSetting(this, Aware_Preferences.FREQUENCY_WEBSERVICE)) * 60; SyncRequest request = new SyncRequest.Builder() .syncPeriodic(frequency, frequency / 3) .setSyncAdapter(Aware.getAWAREAccount(this), ESM_Provider.getAuthority(this)) .setExtras(new Bundle()).build(); ContentResolver.requestSync(request); } } return START_STICKY; }
ContentResolver.setIsSyncable(androidAccount, MatchedUri.AUTHORITY, isSyncable ? 1 : 0);