content: package com.example.test;
  
  import android.app.Notification;
  import android.app.NotificationChannel;
  import android.app.NotificationManager;
  import android.app.Service;
  import android.content.Intent;
  import android.os.Build;
  import android.os.IBinder;
  import androidx.core.app.NotificationCompat;
  
  public class ForegroundService extends Service {
  
      private static final String CHANNEL_ID = "ForegroundServiceChannel";
  
      @Override
      public void onCreate() {
          super.onCreate();
          createNotificationChannel();
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                  .setContentTitle("App is running")
                  .setContentText("Your app is running in the background")
                  .setSmallIcon(android.R.drawable.ic_dialog_info)
                  .build();
  
          startForeground(1, notification);
  
          // 如果需要，执行其他任务
  
          return START_NOT_STICKY;
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      private void createNotificationChannel() {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              NotificationChannel serviceChannel = new NotificationChannel(
                      CHANNEL_ID,
                      "Foreground Service Channel",
                      NotificationManager.IMPORTANCE_DEFAULT
              );
  
              NotificationManager manager = getSystemService(NotificationManager.class);
              if (manager != null) {
                  manager.createNotificationChannel(serviceChannel);
              }
          }
      }
  } 