下面列出了怎么用android.content.PeriodicSync的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName,
ComponentName cname) {
if (account == null) {
throw new IllegalArgumentException("Account must not be null");
}
if (TextUtils.isEmpty(providerName)) {
throw new IllegalArgumentException("Authority must not be empty");
}
mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS,
"no permission to read the sync settings");
int userId = UserHandle.getCallingUserId();
long identityToken = clearCallingIdentity();
try {
return getSyncManager().getPeriodicSyncs(
new SyncStorageEngine.EndPoint(account, providerName, userId));
} finally {
restoreCallingIdentity(identityToken);
}
}
@Override
public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName,
ComponentName cname) {
if (account == null) {
throw new IllegalArgumentException("Account must not be null");
}
if (TextUtils.isEmpty(providerName)) {
throw new IllegalArgumentException("Authority must not be empty");
}
mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS,
"no permission to read the sync settings");
int userId = UserHandle.getCallingUserId();
long identityToken = clearCallingIdentity();
try {
return getSyncManager().getPeriodicSyncs(
new SyncStorageEngine.EndPoint(account, providerName, userId));
} finally {
restoreCallingIdentity(identityToken);
}
}
@Override
public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName,
ComponentName cname) {
if (account == null) {
throw new IllegalArgumentException("Account must not be null");
}
if (TextUtils.isEmpty(providerName)) {
throw new IllegalArgumentException("Authority must not be empty");
}
mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS,
"no permission to read the sync settings");
int userId = UserHandle.getCallingUserId();
long identityToken = clearCallingIdentity();
try {
return getSyncManager().getPeriodicSyncs(
new SyncStorageEngine.EndPoint(account, providerName, userId));
} finally {
restoreCallingIdentity(identityToken);
}
}
/**
* Restore all periodic syncs read from persisted files. Used to restore periodic syncs
* after an OS update.
*/
boolean restoreAllPeriodicSyncs() {
if (mPeriodicSyncAddedListener == null) {
return false;
}
synchronized (mAuthorities) {
for (int i=0; i<mAuthorities.size(); i++) {
AuthorityInfo authority = mAuthorities.valueAt(i);
for (PeriodicSync periodicSync: authority.periodicSyncs) {
mPeriodicSyncAddedListener.onPeriodicSyncAdded(authority.target,
periodicSync.extras, periodicSync.period, periodicSync.flexTime);
}
authority.periodicSyncs.clear();
}
writeAccountInfoLocked();
}
return true;
}
/**
* Get a list of periodic syncs corresponding to the given target.
*/
public List<PeriodicSync> getPeriodicSyncs(EndPoint target) {
List<SyncOperation> ops = getAllPendingSyncs();
List<PeriodicSync> periodicSyncs = new ArrayList<PeriodicSync>();
for (SyncOperation op: ops) {
if (op.isPeriodic && op.target.matchesSpec(target)) {
periodicSyncs.add(new PeriodicSync(op.target.account, op.target.provider,
op.extras, op.periodMillis / 1000, op.flexMillis / 1000));
}
}
return periodicSyncs;
}
/**
* Copy constructor for making deep-ish copies. Only the bundles stored
* in periodic syncs can make unexpected changes.
*
* @param toCopy AuthorityInfo to be copied.
*/
AuthorityInfo(AuthorityInfo toCopy) {
target = toCopy.target;
ident = toCopy.ident;
enabled = toCopy.enabled;
syncable = toCopy.syncable;
backoffTime = toCopy.backoffTime;
backoffDelay = toCopy.backoffDelay;
delayUntil = toCopy.delayUntil;
periodicSyncs = new ArrayList<PeriodicSync>();
for (PeriodicSync sync : toCopy.periodicSyncs) {
// Still not a perfect copy, because we are just copying the mappings.
periodicSyncs.add(new PeriodicSync(sync));
}
}
AuthorityInfo(EndPoint info, int id) {
target = info;
ident = id;
enabled = SYNC_ENABLED_DEFAULT;
periodicSyncs = new ArrayList<PeriodicSync>();
defaultInitialisation();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
((TextView) this.accountSpinner.getSelectedView()).setBackgroundColor(0xFFB6B6B6);
Account account = Listactivity.accounts[pos];
// Check periodic sync. If set to 86400 (once a day), set it to 900 (15 minutes)
// this is due to bad upgrade to v4 which handles offline mode and syncing
// Remove this code after V4.0 if version no more used
List<PeriodicSync> currentSyncs = ContentResolver.getPeriodicSyncs (account, AUTHORITY);
for (PeriodicSync onesync : currentSyncs) {
if (onesync.period == 86400) {
ContentResolver.setIsSyncable(account, AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), 60);
Toast.makeText(getApplicationContext(), "Recreating this account is recommended to manage sync interval. Set to 15 minutes in the meantime",
Toast.LENGTH_LONG).show();
}
}
// End of code
Listactivity.imapNotes2Account.SetAccountname(account.name);
Listactivity.imapNotes2Account.SetUsername(Listactivity.accountManager.getUserData (account, "username"));
String pwd = Listactivity.accountManager.getPassword(account);
Listactivity.imapNotes2Account.SetPassword(pwd);
Listactivity.imapNotes2Account.SetServer(Listactivity.accountManager.getUserData (account, "server"));
Listactivity.imapNotes2Account.SetPortnum(Listactivity.accountManager.getUserData (account, "portnum"));
Listactivity.imapNotes2Account.SetSecurity(Listactivity.accountManager.getUserData (account, "security"));
Listactivity.imapNotes2Account.SetUsesticky(accountManager.getUserData (account, "usesticky"));
Listactivity.imapNotes2Account.SetSyncinterval(Listactivity.accountManager.getUserData (account, "syncinterval"));
Listactivity.imapNotes2Account.SetaccountHasChanged();
Listactivity.imapNotes2Account.SetAccount(account);
this.RefreshList();
}