android.content.Context.startForegroundService()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(263)

本文整理了Java中android.content.Context.startForegroundService()方法的一些代码示例,展示了Context.startForegroundService()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.startForegroundService()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:startForegroundService

Context.startForegroundService介绍

暂无

代码示例

代码示例来源:origin: google/ExoPlayer

/**
 * 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);
 }
}

代码示例来源:origin: oasisfeng/condom

@RequiresApi(O) @Override public ComponentName startForegroundService(final Intent service) {
  return mBase.startForegroundService(service);
}

代码示例来源:origin: TeamNewPipe/NewPipe

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);
  }
}

代码示例来源:origin: commonsguy/cw-omnibus

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);
 }
}

代码示例来源:origin: lingochamp/FileDownloader

@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);
  }
}

代码示例来源:origin: lingochamp/FileDownloader

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);

代码示例来源:origin: gotev/android-upload-service

/**
 * 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;
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = VERSION_CODES.O)
public void startForegroundService() {
 Intent intent = new Intent();
 context.startForegroundService(intent);
 assertThat(ShadowApplication.getInstance().getNextStartedService()).isEqualTo(intent);
}

代码示例来源:origin: robolectric/robolectric

context.startForegroundService(intentToSend);

代码示例来源:origin: AltBeacon/android-beacon-library

this.getForegroundServiceNotification() != null) {
LogManager.i(TAG, "Starting foreground beacon scanning service.");
mContext.startForegroundService(intent);

代码示例来源:origin: Trumeet/MiPushFramework

@RequiresApi(O) @Override public ComponentName startForegroundService(final Intent service) {
  return mBase.startForegroundService(service);
}

代码示例来源:origin: Genymobile/gnirehtet

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);
  }
}

代码示例来源:origin: ukanth/afwall

@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));
      }
    }
  }
}

代码示例来源:origin: syncthing/syncthing-android

/**
 * 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);
  }
}

代码示例来源:origin: Genymobile/gnirehtet

public static void stop(Context context) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(createStopIntent(context));
  } else {
    context.startService(createStopIntent(context));
  }
}

代码示例来源:origin: julian-klode/dns66

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);
  }
}

代码示例来源:origin: julian-klode/dns66

@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);
      }

    }
  }
}

代码示例来源:origin: fython/Blackbulb

/**
 * 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);
  }
}

代码示例来源:origin: termux/termux-widget

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);
  }
}

代码示例来源:origin: proninyaroslav/libretorrent

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);
}

相关文章

Context类方法