Java Code Examples for android.content.Context#startForegroundService()

The following examples show how to use android.content.Context#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: AlarmService.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 2
Source File: AutoUpdateReceiver.java    From Focus with GNU General Public License v3.0 6 votes vote down vote up
@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 {
    }
}
 
Example 3
Source File: MediaButtonIntentReceiver.java    From VinylMusicPlayer with GNU General Public License v3.0 6 votes vote down vote up
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);
        }
    }
}
 
Example 4
Source File: ServiceRecorder.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * コンストラクタ
 * @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();
}
 
Example 5
Source File: MediaButtonIntentReceiver.java    From AudioAnchor with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: StartOpenFitAtBootReceiver.java    From OpenFit with MIT License 5 votes vote down vote up
@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);
        }
    }
}
 
Example 7
Source File: StunnelIntentService.java    From SSLSocks with MIT License 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 8
Source File: AccountTransferBroadcastReceiver.java    From account-transfer-api with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 9
Source File: ModulesAux.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: U.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: ToggleActivity.java    From hyperion-android-grabber with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 12
Source File: CIMPushManager.java    From cim with Apache License 2.0 5 votes vote down vote up
public static void startService(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
Example 13
Source File: BaseFileServiceUIGuard.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 14
Source File: CalendarIntegrationService.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 15
Source File: MediaButtonIntentReceiver.java    From RetroMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 16
Source File: BootActivity.java    From hyperion-android-grabber with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 17
Source File: Utils.java    From SmartPack-Kernel-Manager with GNU General Public License v3.0 5 votes vote down vote up
public static void startService(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    } else {
        context.startService(intent);
    }
}
 
Example 18
Source File: WipeMemoryService.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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);
  }
}
 
Example 19
Source File: IslandProvisioning.java    From island with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 20
Source File: Contexts.java    From deagle with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}