Service problems
A Service has no interface and runs in the background. It consumes device resources and may cause poor user experience. For example, a Service occupies too many resources and does not run smoothly. To alleviate this problem, the Android O version (Android 8.0, API 26) imposes some restrictions on background services. Note that only background services are restricted. Foreground services are not affected.
What is a foreground application
Before explaining the limitations of background services, we need to know what a foreground application is and what a background application is.
A foreground application must meet one of the following conditions:
- There are visible activities. Either resume or pause, as long as it is visible.
- There are foreground services.
- There are other apps that connect to the current app, either by binding Service or using ContentProvider.
Foreground Service and background Service
What is the difference between foreground Service and background Service?
A foreground Service sends a notification notifying the user that a Service is running, whereas a background Service does not notify the user that a Service is running.
How do foreground and background services start?
In the case of background services, it is simple to start the background Service with context.startService ().
For foreground services, this is a bit more complicated. Prior to Android 8.0, you can start a background Service through context.startService (), then send a notification through service.startForeground (), so the background Service becomes foreground Service. However, starting from Android 8.0, the system limits the background app to create background services, so it can no longer use the previous method to promote the background Service to the foreground. In order to solve the problem of background app can’t create the front desk Service, Android 8.0 and introduced a new method of the Context, startForegroundService () to start a front desk Service directly, but after the system to create the front desk Service, The application must call service.startforeground () within 5 seconds to display a notification, otherwise the foreground Service will be stopped and the ANR will pop up.
Starting from Android 9.0 (28) Android P, API, if you want to create the front desk Service, but also in the AndroidManifest. In the XML declaration Android. Permission. FOREGROUND_SERVICE permissions, This is a normal permission that the system automatically grants to the app. If you do not do so, an exception is thrown.
Here is an example of how to create a foreground Service.
First, the target version of your App is Android 10(API 29), so you need to declare permissions in the manifest file.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Copy the code
Then start the foreground Service in the Activity. For compatibility with Android versions prior to 8.0, the code should look like this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntent = new Intent(this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(mIntent);
} else{ startService(mIntent); }}Copy the code
We also remember to destroy the Service when the task completes, either by calling stopService() in the Activity onDestroy() or stopSelf() in the Service. Which one to call depends on your specific requirements.
When the Service is created, we need to call service.startforeground ()
public void onCreate(a) {
super.onCreate();
// Send a notification to bring the service to the foreground
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// Starting with Android 8.0, you need to register the notification channel
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
"Service notification channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Service");
// Note that the first argument cannot be 0
startForeground(Awesome!, builder.build());
}
Copy the code
Background Service Restrictions
When an app is in the foreground, it can create and use backend services at will. When the app goes into the background, it only has a window of a few minutes to create and use services. When this window period ends, the system considers that the app enters the idle state and stops the background service of the APP.
Resolve background Service limitations
The official recommendation is to use JobScheduler to replace the background Service. For example, a gallery app needs to check whether the current user has received a photo shared by a friend, even though the app is not running in the foreground. Prior to Android 8.0, it was possible to use a background Service to detect the cloud storage of an application, but there was a problem. The Service ran in the background together, which consumed resources and affected the performance of the phone. But from Android 8.0, the background Service is replaced with JobScheduler, which periodically starts a task, queries the server, and then exits. Compared with the background Service, it consumes significantly less resources and indirectly improves the performance of the mobile phone.
In my next article, I’ll share an example of how to use JobScheduler to download images.
reference
The official documentation developer. The android. Google. Cn/about/versi…