content: package io.agora.ktv.service;
  
  import android.app.Notification;
  import android.app.NotificationChannel;
  import android.app.NotificationManager;
  import android.app.Service;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.Color;
  import android.os.Build;
  import android.os.IBinder;
  import android.util.Log;
  
  import androidx.core.app.NotificationCompat;
  
  public class MyForegroundService extends Service {
      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";
  
      public MyForegroundService() {
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          throw new UnsupportedOperationException("Not yet implemented");
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          Log.d(TAG_FOREGROUND_SERVICE, "My foreground service onCreate().");
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          Log.d(TAG_FOREGROUND_SERVICE, "onStartCommand " + intent);
  
          if (intent != null) {
              String action = intent.getAction();
  
              switch (action) {
                  case ACTION_START_FOREGROUND_SERVICE:
                      startForegroundService();
                      break;
                  case ACTION_STOP_FOREGROUND_SERVICE:
                      stopForegroundService();
                      break;
              }
          }
          return super.onStartCommand(intent, flags, startId);
      }
  
      /* Used to build and start foreground service. */
      private void startForegroundService() {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              String NOTIFICATION_CHANNEL_ID = "io.agora.ktv.app";
              String channelName = "My foreground Service";
              NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
              channel.setLightColor(Color.BLUE);
              channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
              NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              assert manager != null;
              manager.createNotificationChannel(channel);
  
              NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
              Notification notification = notificationBuilder.setOngoing(true)
                      .setContentTitle("App is running in background")
                      .setPriority(NotificationManager.IMPORTANCE_MIN)
                      .setCategory(Notification.CATEGORY_SERVICE)
                      .build();
              startForeground(2, notification);
          } else {
              startForeground(1, new Notification());
          }
      }
  
      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();
      }
  }
  