Java Code Examples for androidx.core.content.ContextCompat#startForegroundService()

The following examples show how to use androidx.core.content.ContextCompat#startForegroundService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FloatingViewControlFragment.java    From FloatingView with Apache License 2.0 6 votes vote down vote up
/**
 * Start floating view service
 *
 * @param activity             {@link Activity}
 * @param isCustomFloatingView If true, it launches CustomFloatingViewService.
 */
private static void startFloatingViewService(Activity activity, boolean isCustomFloatingView) {
    // *** You must follow these rules when obtain the cutout(FloatingViewManager.findCutoutSafeArea) ***
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        // 1. 'windowLayoutInDisplayCutoutMode' do not be set to 'never'
        if (activity.getWindow().getAttributes().layoutInDisplayCutoutMode == WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER) {
            throw new RuntimeException("'windowLayoutInDisplayCutoutMode' do not be set to 'never'");
        }
    }

    // launch service
    final Class<? extends Service> service;
    final String key;
    if (isCustomFloatingView) {
        service = CustomFloatingViewService.class;
        key = CustomFloatingViewService.EXTRA_CUTOUT_SAFE_AREA;
    } else {
        service = ChatHeadService.class;
        key = ChatHeadService.EXTRA_CUTOUT_SAFE_AREA;
    }
    final Intent intent = new Intent(activity, service);
    intent.putExtra(key, FloatingViewManager.findCutoutSafeArea(activity));
    ContextCompat.startForegroundService(activity, intent);
}
 
Example 2
Source File: MainActivity.java    From TowerCollector with Mozilla Public License 2.0 6 votes vote down vote up
private void startCollectorServiceInternal() {
    askAndSetGpsEnabled();
    if (isGpsEnabled) {
        Timber.d("startCollectorService(): Air plane mode off, starting service");
        // create intent
        final Intent intent = new Intent(this, CollectorService.class);
        // pass means of transport inside intent
        boolean gpsOptimizationsEnabled = MyApplication.getPreferencesProvider().getGpsOptimizationsEnabled();
        MeansOfTransport selectedType = (gpsOptimizationsEnabled ? MeansOfTransport.Universal : MeansOfTransport.Fixed);
        intent.putExtra(CollectorService.INTENT_KEY_TRANSPORT_MODE, selectedType);
        // pass screen on mode
        final String keepScreenOnMode = MyApplication.getPreferencesProvider().getCollectorKeepScreenOnMode();
        intent.putExtra(CollectorService.INTENT_KEY_KEEP_SCREEN_ON_MODE, keepScreenOnMode);
        // pass analytics data
        intent.putExtra(CollectorService.INTENT_KEY_START_INTENT_SOURCE, IntentSource.User);
        // start service
        ContextCompat.startForegroundService(this, intent);
        EventBus.getDefault().post(new CollectorStartedEvent(intent));
        ApkUtils.reportShortcutUsage(MyApplication.getApplication(), R.string.shortcut_id_collector_toggle);
    }
}
 
Example 3
Source File: ScreenshotDecorator.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 6 votes vote down vote up
private void startFloatingPreviewAndMuteNotification() {
    // Avoid duplicated preview
    if (mLastPreviewedShotUri == null || !mLastPreviewedShotUri.equals(recentShotUri)) {
        mLastPreviewedShotUri = recentShotUri;
        switch (mPreferences.getPreviewType()) {
            case ScreenshotPreferences.PREVIEW_TYPE_PIP: {
                previewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Log.d(TAG, "PreviewActivity: should be started");
                startActivity(previewIntent);
                break;
            }
            case ScreenshotPreferences.PREVIEW_TYPE_ARISU: {
                Log.d(TAG, "PreviewService: should be started");
                ContextCompat.startForegroundService(context,
                        PreviewService.createStartIntent(recentShotUri, recentShot, evolving.getKey()));
                break;
            }
            case ScreenshotPreferences.PREVIEW_TYPE_NONE:
                throw new IllegalStateException("Cannot start preview for TYPE_NONE");
        }
    }
    n.setChannelId(CHANNEL_ID_PREVIEWED_SCREENSHOT);
}
 
Example 4
Source File: MediaNotificationManager.java    From klingar with Apache License 2.0 6 votes vote down vote up
@Override public void onReceive(Context context, Intent intent) {
  if (intent.getAction() == null) {
    return;
  }
  switch (intent.getAction()) {
    case ACTION_PAUSE:
    case ACTION_PLAY:
      musicController.playPause();
      break;
    case ACTION_NEXT:
      musicController.next();
      break;
    case ACTION_PREVIOUS:
      musicController.previous();
      break;
    case ACTION_STOP_CAST:
      Intent stopCastIntent = new Intent(context, MusicService.class);
      stopCastIntent.setAction(MusicService.ACTION_STOP_CASTING);
      ContextCompat.startForegroundService(service, stopCastIntent);
      break;
    default:
  }
}
 
Example 5
Source File: GenericForegroundService.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static void stopForegroundTask(@NonNull Context context, int id) {
  Intent intent = new Intent(context, GenericForegroundService.class);
  intent.setAction(ACTION_STOP);
  intent.putExtra(EXTRA_ID, id);

  ContextCompat.startForegroundService(context, intent);
}
 
Example 6
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 5 votes vote down vote up
public static void stop(String reason, Context context, boolean vpnonly) {
    Intent intent = new Intent(context, ServiceSinkhole.class);
    intent.putExtra(EXTRA_COMMAND, Command.stop);
    intent.putExtra(EXTRA_REASON, reason);
    intent.putExtra(EXTRA_TEMPORARY, vpnonly);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 7
Source File: OpenScale.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
private void syncInsertMeasurement(ScaleMeasurement scaleMeasurement) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.health.openscale.sync", "com.health.openscale.sync.core.service.SyncService"));
    intent.putExtra("mode", "insert");
    intent.putExtra("userId", scaleMeasurement.getUserId());
    intent.putExtra("weight", scaleMeasurement.getWeight());
    intent.putExtra("date", scaleMeasurement.getDateTime().getTime());
    ContextCompat.startForegroundService(context, intent);
}
 
Example 8
Source File: BaseActivity.java    From UpdogFarmer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Bind Activity to the service
 */
private void doBind() {
    Log.i(TAG, "Binding service...");
    final Intent serviceIntent = SteamService.createIntent(this);
    ContextCompat.startForegroundService(this, serviceIntent);
    bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
    serviceBound = true;
}
 
Example 9
Source File: SmsReceiver.java    From SmsCode with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    XLog.d("SmsReceiver#onReceived() - {}", intent.getAction());
    if (SMS_RECEIVED.equals(intent.getAction())) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus == null)
                return;

            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }

            if (messages.length != 0) {
                String body = SmsMessageUtils.getMessageBody(messages);
                String sender = messages[0].getOriginatingAddress();
                long date = System.currentTimeMillis();

                SmsMsg smsMsg = new SmsMsg();
                smsMsg.setBody(body);
                smsMsg.setSender(sender);
                smsMsg.setDate(date);

                Intent smsCodeHandleSvc = new Intent(context, SmsCodeHandleService.class);
                smsCodeHandleSvc.putExtra(SmsCodeHandleService.EXTRA_KEY_SMS_MESSAGE_DATA, smsMsg);
                ContextCompat.startForegroundService(context, smsCodeHandleSvc);
            }
        }
    }
}
 
Example 10
Source File: HeapAnalyzerService.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
/**
 * 启动当前服务
 *
 * @param context
 * @param heapDump
 * @param listenerServiceClass
 */
public static void runAnalysis(Context context, HeapDump heapDump,
                               Class<? extends AbstractAnalysisResultService> listenerServiceClass) {
    LeakCanaryInternals.setEnabledBlocking(context, HeapAnalyzerService.class, true);
    LeakCanaryInternals.setEnabledBlocking(context, listenerServiceClass, true);
    Intent intent = new Intent(context, HeapAnalyzerService.class);
    intent.putExtra(LISTENER_CLASS_EXTRA, listenerServiceClass.getName());
    intent.putExtra(HEAPDUMP_EXTRA, heapDump);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 11
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 5 votes vote down vote up
public static void reload(String reason, Context context, boolean interactive) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    if (prefs.getBoolean("enabled", false)) {
        Intent intent = new Intent(context, ServiceSinkhole.class);
        intent.putExtra(EXTRA_COMMAND, Command.reload);
        intent.putExtra(EXTRA_REASON, reason);
        intent.putExtra(EXTRA_INTERACTIVE, interactive);
        ContextCompat.startForegroundService(context, intent);
    }
}
 
Example 12
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 5 votes vote down vote up
public static void run(String reason, Context context) {
    Intent intent = new Intent(context, ServiceSinkhole.class);
    intent.putExtra(EXTRA_COMMAND, Command.run);
    intent.putExtra(EXTRA_REASON, reason);

    ContextCompat.startForegroundService(context, intent);
}
 
Example 13
Source File: SettingsActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
private void createBackup(boolean notify) {
    final Intent intent = new Intent(this, ExportBackupService.class);
    intent.putExtra("NOTIFY_ON_BACKUP_COMPLETE", notify);
    ContextCompat.startForegroundService(this, intent);
}
 
Example 14
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
public static void run(String reason, Context context) {
    Intent intent = new Intent(context, ServiceSinkhole.class);
    intent.putExtra(EXTRA_COMMAND, Command.run);
    intent.putExtra(EXTRA_REASON, reason);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 15
Source File: FFMIntentService.java    From FCM-for-Mojo with GNU General Public License v3.0 4 votes vote down vote up
public static void startDownloadQrCode(Context context) {
    ContextCompat.startForegroundService(context, new Intent(context, FFMIntentService.class)
            .setAction(ACTION_DOWNLOAD_QRCODE));
}
 
Example 16
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
public static void reloadStats(String reason, Context context) {
    Intent intent = new Intent(context, ServiceSinkhole.class);
    intent.putExtra(EXTRA_COMMAND, Command.stats);
    intent.putExtra(EXTRA_REASON, reason);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 17
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
public static void reloadStats(String reason, Context context) {
    Intent intent = new Intent(context, ServiceSinkhole.class);
    intent.putExtra(EXTRA_COMMAND, Command.stats);
    intent.putExtra(EXTRA_REASON, reason);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 18
Source File: EHService.java    From Easer with GNU General Public License v3.0 4 votes vote down vote up
public static void start(Context context) {
    Intent intent = new Intent(context, EHService.class);
    ContextCompat.startForegroundService(context, intent);
}
 
Example 19
Source File: FFMIntentService.java    From FCM-for-Mojo with GNU General Public License v3.0 4 votes vote down vote up
public static void startReply(Context context, CharSequence content, Chat chat) {
    ContextCompat.startForegroundService(context, new Intent(context, FFMIntentService.class)
            .setAction(ACTION_REPLY)
            .putExtra(EXTRA_CONTENT, content)
            .putExtra(EXTRA_CHAT, chat));
}
 
Example 20
Source File: LocationModule.java    From rn-background-location with MIT License 4 votes vote down vote up
@ReactMethod
public void startBackgroundLocation() {
    ContextCompat.startForegroundService(mContext, mForegroundServiceIntent);
}