/** * Calls {@link Context#startForegroundService(Intent)} if {@link #SDK_INT} is 26 or higher, or * {@link Context#startService(Intent)} otherwise. * * @param context The context to call. * @param intent The intent to pass to the called method. * @return The result of the called method. */ public static ComponentName startForegroundService(Context context, Intent intent) { if (Util.SDK_INT >= 26) { return context.startForegroundService(intent); } else { return context.startService(intent); } }
@RequiresApi(O) @Override public ComponentName startForegroundService(final Intent service) { return mBase.startForegroundService(service); }
public static void startService(@NonNull final Context context, @NonNull final Intent intent) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } }
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 { ctxt.startService(i); } }
@Override public void bindStartByContext(Context context, Runnable connectedRunnable) { if (connectedRunnable != null) { if (!connectedRunnableList.contains(connectedRunnable)) { connectedRunnableList.add(connectedRunnable); } } Intent i = new Intent(context, SERVICE_CLASS); runServiceForeground = FileDownloadUtils.needMakeServiceForeground(context); i.putExtra(ExtraKeys.IS_FOREGROUND, runServiceForeground); if (runServiceForeground) { if (FileDownloadLog.NEED_LOG) FileDownloadLog.d(this, "start foreground service"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(i); } else { context.startService(i); } }
if (runServiceForeground) { if (FileDownloadLog.NEED_LOG) FileDownloadLog.d(this, "start foreground service"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(i); } else { context.startService(i);
/** * Start the background file upload service. * @return the uploadId string. If you have passed your own uploadId in the constructor, this * method will return that same uploadId, otherwise it will return the automatically * generated uploadId */ public String startUpload() { UploadService.setUploadStatusDelegate(params.id, delegate); final Intent intent = new Intent(context, UploadService.class); this.initializeIntent(intent); intent.setAction(UploadService.getActionUpload()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (params.notificationConfig == null) { throw new IllegalArgumentException("Android Oreo requires a notification configuration for the service to run. https://developer.android.com/reference/android/content/Context.html#startForegroundService(android.content.Intent)"); } context.startForegroundService(intent); } else { context.startService(intent); } return params.id; }
@Test @Config(minSdk = VERSION_CODES.O) public void startForegroundService() { Intent intent = new Intent(); context.startForegroundService(intent); assertThat(ShadowApplication.getInstance().getNextStartedService()).isEqualTo(intent); }
context.startForegroundService(intentToSend);
this.getForegroundServiceNotification() != null) { LogManager.i(TAG, "Starting foreground beacon scanning service."); mContext.startForegroundService(intent);
@RequiresApi(O) @Override public ComponentName startForegroundService(final Intent service) { return mBase.startForegroundService(service); }
public static void start(Context context, VpnConfiguration config) { Intent intent = new Intent(context, GnirehtetService.class); intent.setAction(ACTION_START_VPN); intent.putExtra(GnirehtetService.EXTRA_VPN_CONFIGURATION, config); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } }
@Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(new Intent(context, ApplyOnBootService.class)); } else { context.startService(new Intent(context, ApplyOnBootService.class)); } } } }
/** * Workaround for starting service from background on Android 8+. * * https://stackoverflow.com/a/44505719/1837158 */ static void startServiceCompat(Context context) { Intent intent = new Intent(context, SyncthingService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } }
public static void stop(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(createStopIntent(context)); } else { context.startService(createStopIntent(context)); } }
public static void checkStartVpnOnBoot(Context context) { Log.i("BOOT", "Checking whether to start ad buster on boot"); Configuration config = FileHelper.loadCurrentSettings(context); if (config == null || !config.autoStart) { return; } if (!context.getSharedPreferences("state", MODE_PRIVATE).getBoolean("isActive", false)) { return; } if (VpnService.prepare(context) != null) { Log.i("BOOT", "VPN preparation not confirmed by user, changing enabled to false"); } Log.i("BOOT", "Starting ad buster from boot"); NotificationChannels.onCreate(context); Intent intent = getStartIntent(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && config.showNotification) { context.startForegroundService(intent); } else { context.startService(intent); } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "onActivityResult: Received result=" + resultCode + " for request=" + requestCode); super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_START_VPN && resultCode == RESULT_CANCELED) { Toast.makeText(getContext(), R.string.could_not_configure_vpn_service, Toast.LENGTH_LONG).show(); } if (requestCode == REQUEST_START_VPN && resultCode == RESULT_OK) { Log.d("MainActivity", "onActivityResult: Starting service"); Intent intent = new Intent(getContext(), AdVpnService.class); intent.putExtra("COMMAND", Command.START.ordinal()); intent.putExtra("NOTIFICATION_INTENT", PendingIntent.getActivity(getContext(), 0, new Intent(getContext(), MainActivity.class), 0)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && MainActivity.config.showNotification) { getContext().startForegroundService(intent); } else { getContext().startService(intent); } } } }
/** * Start foreground service (Must call Service.startForeground after starting) * @param context Context * @param intent Service Intent */ public static void startForegroundService(Context context, Intent intent) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } }
static void startTermuxService(Context context, Intent executeIntent) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // https://developer.android.com/about/versions/oreo/background.html context.startForegroundService(executeIntent); } else { context.startService(executeIntent); } }
public static void startTorrentServiceBackground(Context context, String action) { Intent i = new Intent(context, TorrentTaskService.class); if (action != null) i.setAction(action); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(i); else context.startService(i); }