Example usage for android.content Context startForegroundService

List of usage examples for android.content Context startForegroundService

Introduction

In this page you can find the example usage for android.content Context startForegroundService.

Prototype

@Nullable
public abstract ComponentName startForegroundService(Intent service);

Source Link

Document

Similar to #startService(Intent) , but with an implicit promise that the Service will call android.app.Service#startForeground(int,android.app.Notification) startForeground(int, android.app.Notification) once it begins running.

Usage

From source file:org.chromium.base.ContextUtils.java

/**
 * Starts a service with {@code Intent}.  This will be started with the foreground expectation
 * on Android O and higher.//  w  ww. j  a  v  a  2  s. c o m
 * TODO(crbug.com/769683): Temporary measure until we roll the support library to 26.1.0.
 *
 * @param context Context to start Service from.
 * @param intent The description of the Service to start.
 *
 * @see {@link android.support.v4.content.ContextCompat#startForegroundService(Context,Intent)}
 * @see Context#startForegroundService(Intent)
 * @see Context#startService(Intent)
 */
public static void startForegroundService(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}

From source file:org.mozilla.focus.session.SessionNotificationService.java

static void start(Context context) {
    final Intent intent = new Intent(context, SessionNotificationService.class);
    intent.setAction(ACTION_START);//from www .  j  a v a 2 s  . c o  m

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}

From source file:androidx.media.session.MediaButtonReceiver.java

private static void startForegroundService(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(intent);
    } else {/*  w w w . j  a v  a2 s .c o  m*/
        context.startService(intent);
    }
}

From source file:com.commonsware.android.service.lifecycle.DemoService.java

static void startMeUp(Context ctxt, boolean foreground, boolean importantish) {
    Intent i = new Intent(ctxt, DemoService.class).putExtra(EXTRA_FOREGROUND, foreground)
            .putExtra(EXTRA_IMPORTANTISH, importantish);

    if (foreground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ctxt.startForegroundService(i);
    } else {/*from w  ww. j  av  a 2s .  c  o m*/
        ctxt.startService(i);
    }
}

From source file:nu.yona.app.utils.AppUtils.java

@TargetApi(Build.VERSION_CODES.O)
public static void startForegroundService(Context context, Intent activityMonitorIntent) {
    if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) {
        return; // Notification permission is required for starting a ForegroundService
    }//w  w  w  .j a va 2 s  .c  o m
    context.startForegroundService(activityMonitorIntent);
}

From source file:com.owncloud.android.files.services.TransferRequester.java

/**
 * Call to update multiple files already uploaded
 *//*from  www. j ava2s  . c om*/
private void uploadsUpdate(Context context, Account account, OCFile[] existingFiles, Integer behaviour,
        Boolean forceOverwrite, boolean requestedFromAvOfflineJobService) {
    Intent intent = new Intent(context, FileUploader.class);

    intent.putExtra(FileUploader.KEY_ACCOUNT, account);
    intent.putExtra(FileUploader.KEY_FILE, existingFiles);
    intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
    intent.putExtra(FileUploader.KEY_FORCE_OVERWRITE, forceOverwrite);

    // Since in Android O and above the apps in background are not allowed to start background
    // services and available offline feature may try to do it, this is the way to proceed
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && requestedFromAvOfflineJobService) {
        intent.putExtra(FileUploader.KEY_IS_AVAILABLE_OFFLINE_FILE, true);
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}

From source file:com.owncloud.android.files.services.TransferRequester.java

/**
 * Private implementation of retry.//w  ww . jav a  2  s.  c om
 *
 * @param context           Caller {@link Context}
 * @param account           OC account where the upload will be retried.
 * @param upload            Persisted upload to retry.
 * @param requestedFromWifiBackEvent true if the retry was requested because wifi connection was back,
 *                                   false otherwise
 */
private void retry(Context context, Account account, OCUpload upload, boolean requestedFromWifiBackEvent) {
    if (upload != null) {
        Intent intent = new Intent(context, FileUploader.class);
        intent.putExtra(FileUploader.KEY_RETRY, true);
        intent.putExtra(FileUploader.KEY_ACCOUNT, account);
        intent.putExtra(FileUploader.KEY_RETRY_UPLOAD, upload);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
                && (upload.getCreatedBy() == CREATED_AS_CAMERA_UPLOAD_PICTURE
                        || upload.getCreatedBy() == CREATED_AS_CAMERA_UPLOAD_VIDEO
                        || requestedFromWifiBackEvent)) {
            // Since in Android O the apps in background are not allowed to start background
            // services and camera uploads feature may try to do it, this is the way to proceed
            if (requestedFromWifiBackEvent) {
                intent.putExtra(FileUploader.KEY_REQUESTED_FROM_WIFI_BACK_EVENT, true);
            }
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }
}

From source file:com.owncloud.android.files.services.TransferRequester.java

/**
 * Call to upload several new files//from ww  w. jav a2s  . com
 */
public void uploadNewFiles(Context context, Account account, String[] localPaths, String[] remotePaths,
        String[] mimeTypes, Integer behaviour, Boolean createRemoteFolder, int createdBy) {
    Intent intent = new Intent(context, FileUploader.class);

    intent.putExtra(FileUploader.KEY_ACCOUNT, account);
    intent.putExtra(FileUploader.KEY_LOCAL_FILE, localPaths);
    intent.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
    intent.putExtra(FileUploader.KEY_MIME_TYPE, mimeTypes);
    intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
    intent.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, createRemoteFolder);
    intent.putExtra(FileUploader.KEY_CREATED_BY, createdBy);

    if ((createdBy == CREATED_AS_CAMERA_UPLOAD_PICTURE || createdBy == CREATED_AS_CAMERA_UPLOAD_VIDEO)
            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Since in Android O the apps in background are not allowed to start background
        // services and camera uploads feature may try to do it, this is the way to proceed
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}

From source file:github.popeen.dsub.service.DownloadService.java

public static void startService(Context context, Intent intent) {
    PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
    if (Build.VERSION.SDK_INT < 26
            || (powerManager != null && powerManager.isIgnoringBatteryOptimizations(intent.getPackage()))) {
        context.startService(intent);/* w  w  w.j  a  v a2s  .c o m*/
    } else {
        context.startForegroundService(intent);
    }
}