类android.content.SyncRequest源码实例Demo

下面列出了怎么用android.content.SyncRequest的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: react-native-sync-adapter   文件: SyncAdapter.java
/**
 * 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);
    }
}
 
源代码2 项目: Krishi-Seva   文件: SunshineSyncAdapter.java
/**
 * 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);
    }
}
 
源代码4 项目: Woodmin   文件: WoodminSyncAdapter.java
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);
    }
}
 
源代码5 项目: Woodmin   文件: WoodminSyncAdapter.java
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);
    }
}
 
源代码6 项目: AndroidComponentPlugin   文件: ContentService.java
/**
 * 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);
    }
}
 
源代码7 项目: AndroidComponentPlugin   文件: ContentService.java
@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);
    }
}
 
源代码8 项目: AndroidComponentPlugin   文件: ContentService.java
@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);
    }
}
 
源代码9 项目: android_9.0.0_r45   文件: ContentService.java
@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);
    }
}
 
源代码10 项目: android_9.0.0_r45   文件: SyncStorageEngine.java
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());
    }
}
 
源代码11 项目: gito-github-client   文件: SyncAdapter.java
/**
 * 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);
    }
}
 
源代码12 项目: Cirrus_depricated   文件: FileActivity.java
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);
    }
}
 
源代码13 项目: AndroidComponentPlugin   文件: ContentService.java
/**
 * 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);
    }
}
 
源代码14 项目: android_9.0.0_r45   文件: ContentService.java
/**
 * 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);
    }
}
 
源代码15 项目: AndroidComponentPlugin   文件: ContentService.java
/**
 * 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());
}
 
源代码16 项目: AndroidComponentPlugin   文件: ContentService.java
/**
 * 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);
}
 
源代码17 项目: android_9.0.0_r45   文件: ContentService.java
/**
 * 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());
}
 
 类所在包
 同包方法