下面列出了android.content.Context#startForegroundService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onReceive(Context context, Intent intent) {
//当前定时器已开启
UserPreference.updateOrSaveValueByKey(UserPreference.tim_is_open,"1");
String interval = UserPreference.queryValueByKey(UserPreference.tim_interval,"-1");
if (!interval.equals("-1")){//这个地方判断是因为有可能在定时器运行期间,用户取消了定时器,这样
Intent i = new Intent(context, TimingService.class);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i);
} else {
context.startService(i);
}
}catch (Exception e){
UserPreference.updateOrSaveValueByKey(UserPreference.back_error,e.getMessage());
}
}else {
}
}
/**
* コンストラクタ
* @param context
* @param callback
*/
@SuppressLint("NewApi")
public ServiceRecorder(@NonNull final Context context,
@NonNull final Callback callback) {
if (DEBUG) Log.v(TAG, "コンストラクタ:");
mWeakContext = new WeakReference<>(context);
mCallback = callback;
final Intent serviceIntent = createServiceIntent(context);
if (BuildCheck.isOreo()) {
context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
doBindService();
}
private static void startService(Context context, String command) {
final Intent intent = new Intent(context, MusicService.class);
intent.setAction(command);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
try {
// IMPORTANT NOTE: (kind of a hack)
// on Android O and above the following crashes when the app is not running
// there is no good way to check whether the app is running so we catch the exception
// we do not always want to use startForegroundService() because then one gets an ANR
// if no notification is displayed via startForeground()
// according to Play analytics this happens a lot, I suppose for example if command = PAUSE
context.startService(intent);
} catch (IllegalStateException ignored) {
ContextCompat.startForegroundService(context, intent);
}
}
}
@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
int alarmId = intent.getIntExtra(EXTRA_ALARMID, -1);
long time = intent.getLongExtra(EXTRA_TIME, -1);
if (alarmId > 0 && time > 0) {
Intent service = new Intent(context, AlarmService.class);
service.putExtra(EXTRA_ALARMID, alarmId);
service.putExtra(EXTRA_TIME, time);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(service);
} else {
context.startService(service);
}
} else {
Times.setAlarms();
}
}
public static void startService(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
private static void startForegroundService(Context context) {
Intent intent = new Intent(context, WipeMemoryService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
public static void startService(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
public static void startScreenRecorder(Context context, int resultCode, Intent data) {
Intent intent = new Intent(context, HyperionScreenService.class);
intent.setAction(HyperionScreenService.ACTION_START);
intent.putExtra(HyperionScreenService.EXTRA_RESULT_CODE, resultCode);
intent.putExtras(data);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
private static void startService(Context context, String command) {
final Intent intent = new Intent(context, MusicService.class);
intent.setAction(command);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
public static void startCalendarIntegration(@NonNull Context context) {
Intent intent = new Intent(context, CalendarIntegrationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
@Override
public void bindStartByContext(final Context context, final Runnable connectedRunnable) {
if (FileDownloadUtils.isDownloaderProcess(context)) {
throw new IllegalStateException("Fatal-Exception: You can't bind the "
+ "FileDownloadService in :filedownloader process.\n It's the invalid operation"
+ " and is likely to cause unexpected problems.\n Maybe you want to use"
+ " non-separate process mode for FileDownloader, More detail about "
+ "non-separate mode, please move to wiki manually:"
+ " https://github.com/lingochamp/FileDownloader/wiki/filedownloader.properties"
);
}
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "bindStartByContext %s", context.getClass().getSimpleName());
}
Intent i = new Intent(context, serviceClass);
if (connectedRunnable != null) {
if (!connectedRunnableList.contains(connectedRunnable)) {
connectedRunnableList.add(connectedRunnable);
}
}
if (!bindContexts.contains(context)) {
// 对称,只有一次remove,防止内存泄漏
bindContexts.add(context);
}
runServiceForeground = FileDownloadUtils.needMakeServiceForeground(context);
i.putExtra(ExtraKeys.IS_FOREGROUND, runServiceForeground);
context.bindService(i, this, Context.BIND_AUTO_CREATE);
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);
}
}
private static void startService(Context context, String command) {
final Intent intent = new Intent(context, MediaPlayerService.class);
intent.setAction(command);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
private static void startScreenRecorder(Context context, int resultCode, Intent data) {
Intent intent = new Intent(context, HyperionScreenService.class);
intent.setAction(HyperionScreenService.ACTION_START);
intent.putExtra(HyperionScreenService.EXTRA_RESULT_CODE, resultCode);
intent.putExtras(data);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
public static void startForegroundService(Context context, Intent intent) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if(Settings.canDrawOverlays(context))
context.startForegroundService(intent);
} else
context.startService(intent);
}
private static void sendIntent(Context context, String action) {
Intent intent = new Intent(context, ModulesService.class);
intent.setAction(action);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.putExtra("showNotification", true);
context.startForegroundService(intent);
} else {
intent.putExtra("showNotification", isShowNotification(context));
context.startService(intent);
}
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received intent:" + intent);
// Long running tasks, like calling Account Transfer API, shouldn't happen here. Start a
// foreground service to perform long running tasks.
Intent serviceIntent = AccountTransferService.getIntent(context, intent.getAction());
if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void start(Context context) {
Intent intent = new Intent(context, StunnelIntentService.class);
intent.setAction(ACTION_STARTNOVPN);
if (android.os.Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, OpenFitService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
public static void start(final Context context, final @Nullable String action) {
final Intent intent = new Intent(action).setComponent(new ComponentName(context, IslandProvisioning.class));
if (SDK_INT >= O) context.startForegroundService(intent);
else context.startService(intent);
}
/**
* startForegroundService() was introduced in O, just call startService() before O.
*
* @param context Context to start Service from.
* @param intent The description of the Service to start.
*
* @see Context#startForegroundService(Intent)
*/
public static ComponentName startForegroundService(final Context context, final Intent intent) {
return SDK_INT >= O ? context.startForegroundService(intent) : context.startService(intent);
}