下面列出了怎么用android.content.SyncRequest的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Based on https://gist.github.com/udacityandroid/7230489fb8cb3f46afee
*/
private static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
Account account = getSyncAccount(context, syncInterval, flexTime);
String authority = context.getString(R.string.rnsb_content_authority);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// We can enable inexact timers in our periodic sync (better for batter life)
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);
}
}
/**
* Helper method to schedule the sync adapter periodic execution
*/
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).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account,
authority, new Bundle(), syncInterval);
}
}
/**
* Helper method to schedule the sync adapter periodic execution
*/
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).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account,
authority, new Bundle(), syncInterval);
}
}
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)
.setExtras(new Bundle())
.build();
ContentResolver.requestSync(request);
ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
} else {
ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
}
}
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)
.setExtras(new Bundle())
.build();
ContentResolver.requestSync(request);
ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
} else {
ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
}
}
/**
* If the user id supplied is different to the calling user, the caller must hold the
* INTERACT_ACROSS_USERS_FULL permission.
*/
@Override
public void syncAsUser(SyncRequest request, int userId) {
enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
int callerUid = Binder.getCallingUid();
// This makes it so that future permission checks will be in the context of this
// process rather than the caller's process. We will restore this before returning.
long identityToken = clearCallingIdentity();
try {
SyncManager syncManager = getSyncManager();
if (syncManager == null) {
return;
}
Bundle extras = request.getBundle();
long flextime = request.getSyncFlexTime();
long runAtTime = request.getSyncRunTime();
if (request.isPeriodic()) {
mContext.enforceCallingOrSelfPermission(
Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
SyncStorageEngine.EndPoint info;
info = new SyncStorageEngine.EndPoint(
request.getAccount(), request.getProvider(), userId);
runAtTime = clampPeriod(runAtTime);
// Schedule periodic sync.
getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
flextime, extras);
} else {
syncManager.scheduleSync(
request.getAccount(), userId, callerUid, request.getProvider(), extras,
SyncStorageEngine.AuthorityInfo.UNDEFINED);
}
} finally {
restoreCallingIdentity(identityToken);
}
}
@Override
public void cancelRequest(SyncRequest request) {
SyncManager syncManager = getSyncManager();
if (syncManager == null) return;
int userId = UserHandle.getCallingUserId();
final int callingUid = Binder.getCallingUid();
long identityToken = clearCallingIdentity();
try {
SyncStorageEngine.EndPoint info;
Bundle extras = new Bundle(request.getBundle());
Account account = request.getAccount();
String provider = request.getProvider();
info = new SyncStorageEngine.EndPoint(account, provider, userId);
if (request.isPeriodic()) {
// Remove periodic sync.
mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
getSyncManager().removePeriodicSync(info, extras,
"cancelRequest() by uid=" + callingUid);
}
// Cancel active syncs and clear pending syncs from the queue.
syncManager.cancelScheduledSyncOperation(info, extras);
syncManager.cancelActiveSync(info, extras, "API");
} finally {
restoreCallingIdentity(identityToken);
}
}
@Override
public void cancelRequest(SyncRequest request) {
SyncManager syncManager = getSyncManager();
if (syncManager == null) return;
int userId = UserHandle.getCallingUserId();
final int callingUid = Binder.getCallingUid();
if (request.isPeriodic()) {
mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
}
Bundle extras = new Bundle(request.getBundle());
validateExtras(callingUid, extras);
long identityToken = clearCallingIdentity();
try {
SyncStorageEngine.EndPoint info;
Account account = request.getAccount();
String provider = request.getProvider();
info = new SyncStorageEngine.EndPoint(account, provider, userId);
if (request.isPeriodic()) {
// Remove periodic sync.
getSyncManager().removePeriodicSync(info, extras,
"cancelRequest() by uid=" + callingUid);
}
// Cancel active syncs and clear pending syncs from the queue.
syncManager.cancelScheduledSyncOperation(info, extras);
syncManager.cancelActiveSync(info, extras, "API");
} finally {
restoreCallingIdentity(identityToken);
}
}
@Override
public void cancelRequest(SyncRequest request) {
SyncManager syncManager = getSyncManager();
if (syncManager == null) return;
int userId = UserHandle.getCallingUserId();
final int callingUid = Binder.getCallingUid();
if (request.isPeriodic()) {
mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
}
Bundle extras = new Bundle(request.getBundle());
validateExtras(callingUid, extras);
long identityToken = clearCallingIdentity();
try {
SyncStorageEngine.EndPoint info;
Account account = request.getAccount();
String provider = request.getProvider();
info = new SyncStorageEngine.EndPoint(account, provider, userId);
if (request.isPeriodic()) {
// Remove periodic sync.
getSyncManager().removePeriodicSync(info, extras,
"cancelRequest() by uid=" + callingUid);
}
// Cancel active syncs and clear pending syncs from the queue.
syncManager.cancelScheduledSyncOperation(info, extras);
syncManager.cancelActiveSync(info, extras, "API");
} finally {
restoreCallingIdentity(identityToken);
}
}
private void requestSync(AuthorityInfo authorityInfo, int reason, Bundle extras,
@SyncExemption int syncExemptionFlag) {
if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
&& mSyncRequestListener != null) {
mSyncRequestListener.onSyncRequest(authorityInfo.target, reason, extras,
syncExemptionFlag);
} else {
SyncRequest.Builder req =
new SyncRequest.Builder()
.syncOnce()
.setExtras(extras);
req.setSyncAdapter(authorityInfo.target.account, authorityInfo.target.provider);
ContentResolver.requestSync(req.build());
}
}
/**
* Helper method to schedule the sync adapter periodic execution
*/
private static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
SyncRequest request = new SyncRequest.Builder().
syncPeriodic(syncInterval, flexTime).
setSyncAdapter(mAccount, context.getString(R.string.sync_authority)).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(mAccount,
context.getString(R.string.sync_authority), new Bundle(), syncInterval);
}
}
protected void startSynchronization() {
Log_OC.d(TAG, "Got to start sync");
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
Log_OC.d(TAG, "Canceling all syncs for " + MainApp.getAuthority());
ContentResolver.cancelSync(null, MainApp.getAuthority());
// cancel the current synchronizations of any ownCloud account
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
MainApp.getAuthority());
ContentResolver.requestSync(
getAccount(),
MainApp.getAuthority(), bundle);
} else {
Log_OC.d(TAG, "Requesting sync for " + getAccount().name + " at " +
MainApp.getAuthority() + " with new API");
SyncRequest.Builder builder = new SyncRequest.Builder();
builder.setSyncAdapter(getAccount(), MainApp.getAuthority());
builder.setExpedited(true);
builder.setManual(true);
builder.syncOnce();
// Fix bug in Android Lollipop when you click on refresh the whole account
Bundle extras = new Bundle();
builder.setExtras(extras);
SyncRequest request = builder.build();
ContentResolver.requestSync(request);
}
}
/**
* If the user id supplied is different to the calling user, the caller must hold the
* INTERACT_ACROSS_USERS_FULL permission.
*/
@Override
public void syncAsUser(SyncRequest request, int userId, String callingPackage) {
enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
final int callingUid = Binder.getCallingUid();
final int callingPid = Binder.getCallingPid();
final Bundle extras = request.getBundle();
validateExtras(callingUid, extras);
final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callingUid, extras);
// This makes it so that future permission checks will be in the context of this
// process rather than the caller's process. We will restore this before returning.
long identityToken = clearCallingIdentity();
try {
SyncManager syncManager = getSyncManager();
if (syncManager == null) {
return;
}
long flextime = request.getSyncFlexTime();
long runAtTime = request.getSyncRunTime();
if (request.isPeriodic()) {
mContext.enforceCallingOrSelfPermission(
Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
SyncStorageEngine.EndPoint info;
info = new SyncStorageEngine.EndPoint(
request.getAccount(), request.getProvider(), userId);
runAtTime = clampPeriod(runAtTime);
// Schedule periodic sync.
getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
flextime, extras);
} else {
syncManager.scheduleSync(
request.getAccount(), userId, callingUid, request.getProvider(), extras,
SyncStorageEngine.AuthorityInfo.UNDEFINED,
syncExemption, callingUid, callingPid, callingPackage);
}
} finally {
restoreCallingIdentity(identityToken);
}
}
/**
* If the user id supplied is different to the calling user, the caller must hold the
* INTERACT_ACROSS_USERS_FULL permission.
*/
@Override
public void syncAsUser(SyncRequest request, int userId) {
enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
int callerUid = Binder.getCallingUid();
final Bundle extras = request.getBundle();
validateExtras(callerUid, extras);
final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callerUid, extras);
// This makes it so that future permission checks will be in the context of this
// process rather than the caller's process. We will restore this before returning.
long identityToken = clearCallingIdentity();
try {
SyncManager syncManager = getSyncManager();
if (syncManager == null) {
return;
}
long flextime = request.getSyncFlexTime();
long runAtTime = request.getSyncRunTime();
if (request.isPeriodic()) {
mContext.enforceCallingOrSelfPermission(
Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
SyncStorageEngine.EndPoint info;
info = new SyncStorageEngine.EndPoint(
request.getAccount(), request.getProvider(), userId);
runAtTime = clampPeriod(runAtTime);
// Schedule periodic sync.
getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
flextime, extras);
} else {
syncManager.scheduleSync(
request.getAccount(), userId, callerUid, request.getProvider(), extras,
SyncStorageEngine.AuthorityInfo.UNDEFINED,
syncExemption);
}
} finally {
restoreCallingIdentity(identityToken);
}
}
/**
* Request a sync with a generic {@link android.content.SyncRequest} object. This will be
* either:
* periodic OR one-off sync.
* and
* anonymous OR provider sync.
* Depending on the request, we enqueue to suit in the SyncManager.
* @param request The request object. Validation of this object is done by its builder.
*/
@Override
public void sync(SyncRequest request) {
syncAsUser(request, UserHandle.getCallingUserId());
}
/**
* Request a sync with a generic {@link android.content.SyncRequest} object. This will be
* either:
* periodic OR one-off sync.
* and
* anonymous OR provider sync.
* Depending on the request, we enqueue to suit in the SyncManager.
* @param request The request object. Validation of this object is done by its builder.
*/
@Override
public void sync(SyncRequest request, String callingPackage) {
syncAsUser(request, UserHandle.getCallingUserId(), callingPackage);
}
/**
* Request a sync with a generic {@link android.content.SyncRequest} object. This will be
* either:
* periodic OR one-off sync.
* and
* anonymous OR provider sync.
* Depending on the request, we enqueue to suit in the SyncManager.
* @param request The request object. Validation of this object is done by its builder.
*/
@Override
public void sync(SyncRequest request) {
syncAsUser(request, UserHandle.getCallingUserId());
}