Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void stopForegroundService() {
- // Stop foreground service and remove the notification.
- stopForeground(true);
- // Stop the foreground service.
- stopSelf();
- }
- Intent intent = new Intent(this, MyForeGroundService.class);
- intent.setAction(MyForeGroundService.ACTION_STOP_FOREGROUND_SERVICE);
- startService(intent);
- private static final String TAG_FOREGROUND_SERVICE = "FOREGROUND_SERVICE";
- public static final String ACTION_START_FOREGROUND_SERVICE = "ACTION_START_FOREGROUND_SERVICE";
- public static final String ACTION_STOP_FOREGROUND_SERVICE = "ACTION_STOP_FOREGROUND_SERVICE";
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- if (intent != null) {
- String action = intent.getAction();
- switch (action) {
- case ACTION_START_FOREGROUND_SERVICE:
- startForegroundService();
- break;
- case ACTION_STOP_FOREGROUND_SERVICE:
- stopForegroundService();
- break;
- }
- }
- return START_STICKY;
- }
- private void stopForegroundService() {
- Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");
- // Stop foreground service and remove the notification.
- stopForeground(true);
- // Stop the foreground service.
- stopSelf();
- }
- <service
- android:enabled="true"
- android:name=".ExampleService"
- android:exported="false"
- android:stopWithTask="true" />
- @Override
- public void onTaskRemoved(Intent rootIntent) {
- System.out.println("onTaskRemoved called");
- super.onTaskRemoved(rootIntent);
- //do something you want
- //stop service
- this.stopSelf();
- }
Add Comment
Please, Sign In to add comment