SoFunction
Updated on 2025-03-01

Implementation of Android notification bar front desk service

1. A brief introduction to front desk service

Foreground services are services that are considered to be aware of by the user and are not allowed to kill when the system is insufficient. The foreground service must provide a notification to the status bar, which is placed under the Ongoing title - which means that the notification can only be cancelled after the service is terminated or the notification is actively removed from the foreground.

The most common form of expression is the music playback service. When the application is running in the background, users can use the notification bar to know the current playback content and perform related operations such as pause, continue, and cut songs.

2. Why use front desk service

The priority of the service system running in the background is relatively low. When the system is insufficient, the service running in the background may be recycled. In order to maintain the normal operation and related operations of the background service, you can choose to set the service that needs to be kept running as the foreground service, so that the service can remain working when the APP is in the background for a long time or is closed (the process has not been cleaned up).

3. Detailed use of front desk services

Create service content, as follows (Don’t forget to register the manifest file for the four major components, otherwise the service will not be found when the startup);

public class ForegroundService extends Service {
  
  private static final String TAG = ();

 @Override
  public void onCreate() {
    ();
    (TAG, "onCreate");
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    (TAG, "onBind");
    return null;
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    (TAG, "onStartCommand");
    return (intent, flags, startId);
  }

  @Override
  public void onDestroy() {
    (TAG, "onDestroy");
    ();
  }  
}

Create service notification content, such as music playback, Bluetooth device being connected, etc.:

/**
  * Create a service notification
  */
private Notification createForegroundNotification() {
  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  // The id of the only notification channel.  String notificationChannelId = "notification_channel_id_01";

  // Create a new message channel for Android 8.0 or above systems  if (.SDK_INT >= Build.VERSION_CODES.O) {
    //The user-visible channel name    String channelName = "Foreground Service Notification";
    //The importance of the channel    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
    ("Channel description");
    //LED light    (true);
    ();
    //shock    (new long[]{0, 1000, 500, 1000});
    (true);
    if (notificationManager != null) {
      (notificationChannel);
    }
  }

   builder = new (this, notificationChannelId);
  //Notification icon  (.ic_launcher);
  //Notification title  ("ContentTitle");
  //Notification content  ("ContentText");
  //Set the time to display notification  (());
  //Set the content of the startup  Intent activityIntent = new Intent(this, );
  PendingIntent pendingIntent = (this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  (pendingIntent);

  //Create notification and return  return ();
}

When starting the service, create a notification:

@Override
public void onCreate() {
  ();
  (TAG, "onCreate");
  // Get service notification  Notification notification = createForegroundNotification();
  //Put the service in the startup state, NOTIFICATION_ID refers to the ID of the notification created  startForeground(NOTIFICATION_ID, notification);
}

When the service is stopped, remove the notification:

@Override
public void onDestroy() {
  (TAG, "onDestroy");
  // Tag service closed   = false;
  // Remove notification  stopForeground(true);
  ();
}

Determine whether the service is started and obtain the transmitted information:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  (TAG, "onStartCommand");
  // Mark service start   = true;
  // Data acquisition  String data = ("Foreground");
  (this, data, Toast.LENGTH_SHORT).show();
  return (intent, flags, startId);
}

The above is the creation process of the front-end service. The relevant comments are already very clear. For specific use, you can check the demo at the end of the article.

After the service is created, the service can be started. Before starting, do not forget to add the foreground service permissions in the manifest file:

<uses-permission android:name=".FOREGROUND_SERVICE" />

Start and stop of service

//Start the serviceif (!) {
  // Android 8.0 uses startForegroundService to start a new service in the foreground  mForegroundService = new Intent(this, );
  ("Foreground", "This is a foreground service.");
  if (.SDK_INT &gt;= Build.VERSION_CODES.O) {
    startForegroundService(mForegroundService);
  } else {
    startService(mForegroundService);
  }
} else {
  (this, "The front desk service is running...", Toast.LENGTH_SHORT).show();
}
//Stop servicemForegroundService = new Intent(this, );
stopService(mForegroundService);

This is the end of the introduction and use of front desk services. The related use has been uploaded toGithub development records, welcome to click on the check and Star, I will continue to add other useful knowledge and examples to the project.

This is the end of this article about the implementation of the Android notification bar front desk service. For more related content in the Android notification bar front desk, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!