Android front desk service explanation

Table of contents

The difference between Android front-end services and background services

Foreground Service:

Background Service:

Summarize:

Front desk service update:

Differences and usage between JobScheduler and WorkManager

android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() How to solve it?

WorkManager


The difference between Android front-end services and background services

In Android, foreground service and background service are two different types of services. They have some differences in functions and system treatment.

Foreground Service:

Official documentation https://developer.android.com/guide/components/foreground-services

  • A foreground service is a visible service that displays a notification in the status bar to inform the user that there is a running service.

  • Foreground services are useful when performing certain tasks that require user awareness or interaction, such as playing music, downloading files, etc.

  • Foreground services are considered part of the user experience, so the system will give them higher priority and will not be easily terminated by the system.

  • When using a foreground service, you need to call the startForeground() method to start the service and provide a service-related notification.

  • When you use a foreground service, a notification must be displayed so that the user actively knows that the service is running. This notification cannot be dismissed unless the service is stopped or removed from the foreground.

Background Service:

Official documentation https://developer.android.com/guide/background#categories_of_background_tasks

  • A background service is a service that performs tasks in the background, and users will not directly perceive its existence.

  • Background services are typically used to perform long-running tasks that do not require user interaction, such as data synchronization, periodic updates, etc.

  • Background services have a lower priority, and the system may terminate their operation to free up system resources when resources are tight.

  • When using a background service, the startService() method is usually called to start the service.

Summarize:

  • Foreground services are suitable for tasks that users know and need to interact with. The system has higher priority and is usually used for long-running tasks.

  • Background services are suitable for tasks that do not require user interaction and user awareness. The system has a low priority and may terminate its operation when resources are tight.

It should be noted that starting from Android 8.0 (API level 26), background service restrictions are introduced, that is, background services are subject to some restrictions when performing tasks in the background. When executing tasks in the background, appropriate background execution methods should be used, such as using JobScheduler, WorkManager or foreground services to ensure task execution and reasonable utilization of system resources.

Front desk service update:

Starting with Android 13 (API level 33), users can turn off notifications associated with foreground services by default. To do this, the user performs a swipe gesture on the notification. Traditionally, notifications are not ignored unless the foreground service is stopped or removed from the foreground.

Differences and usage between JobScheduler and WorkManager

JobScheduler and WorkManager are two different tools for scheduling and executing background tasks in Android. They have some differences and applicable scenarios.

JobScheduler:

  • JobScheduler is a system service for scheduling tasks provided by Android, introduced starting from Android 5.0 (API level 21).

  • JobScheduler uses flexible conditions and trigger mechanisms to schedule task execution. Tasks can be triggered based on conditions such as time, device idle status, network connection status, etc.

  • JobScheduler is suitable for background tasks that need to be delayed or executed periodically, such as data synchronization, update operations, etc.

  • JobScheduler has lower power and resource consumption, and the system automatically decides when to execute tasks based on conditions to save power and system resources.

Example using JobScheduler:

// 创建JobInfo对象
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, new ComponentName(context, YourJobService.class))
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
        .setRequiresCharging(true)
        .build();

// 获取JobScheduler
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

// 调度任务
jobScheduler.schedule(jobInfo);

WorkManager:

  • WorkManager is one of the Android Jetpack components used to perform deferrable, reliable and flexible background tasks, available from Android 5.0 (API level 21) and above.

  • WorkManager provides a simple and powerful way to handle background tasks, including delayed execution, periodic execution, execution when the device is idle, etc.

  • WorkManager provides consistent behavior across different versions of Android, automatically adapting to system and device requirements.

  • WorkManager is suitable for background tasks that require reliable execution and guaranteed task completion, such as uploading files, sending notifications, etc.

Example using WorkManager:

// 创建OneTimeWorkRequest对象
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(YourWorker.class)
        .setConstraints(new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.UNMETERED)
                .setRequiresCharging(true)
                .build())
        .build();

// 调度任务
WorkManager.getInstance(context).enqueue(workRequest);

Summarize:

  • If you need a flexible task scheduling mechanism and use it on Android 5.0 and above, you can choose JobScheduler.

  • If you need a reliable, cross-version background task execution method and use the Android Jetpack component, you can choose WorkManager.

  • For devices below Android 5.0, you can choose a suitable method according to your needs, such as using AlarmManager or a custom background service.

You need to choose an appropriate background task scheduling tool based on your specific needs, target version, and functional features.

android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() How to solve it?

On Android 12+, to start a foreground service in the background, some additional conditions need to be met. For example, the application must have the appropriate permissions or use the appropriate API to request the startup of the foreground service. If these conditions are not met, a ForegroundServiceStartNotAllowedException exception will be thrown.

The solution to this exception is to consider using an appropriate background task scheduling method, such as using WorkManager to replace the foreground service. WorkManager provides a reliable background task execution mechanism that can adapt to Android 5.0 (API level 21) and above, and avoids the problem of background service limitations.


WorkManager

Official documentation https://developer.android.com/guide/background#categories_of_background_tasks

 

Guess you like

Origin blog.csdn.net/zyy_give/article/details/131602476