Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet38/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   34   35   36   37   38   39   40   41   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

28
CHAPTER 4: Services
Foreground Services
The intrinsic functioning of foreground services differs with different Android versions. While 
foreground services prior to Android 8.0 (API level 26) were just background services with 
an entry inside the status bar and otherwise no stringent influence on how the Android OS 
handles them, with Android 8.0 (API level 26), foreground services follow a special notation 
and receive improved attention from the Android OS, making them less likely to be killed 
because of resource shortages. Here are some details:

Foreground services before Android 8.0 (API level 26) are services 
that just present a notification entry in the status bar. The client 
component that needs to use a service doesn’t know whether the 
service that started is a foreground service or not; it just starts the 
service via startService(intent). See Chapter 
12
.

Foreground services starting with Android 8.0 (API level 26) run 
with the user being made aware of them. They must interfere with the 
operating system by notifications in the status bar. A client component 
explicitly starts a foreground service by invoking startForeroundService
(intent), and the service itself must readily tell the Android OS within a 
few seconds that it wants to run as a foreground service by calling start
Foreground(notificationId, notification).
One noticeable lifetime characteristics of a foreground service is it is less likely to be killed 
by Android because of an available resource shortage. The documentation is, however, not 
precise about that. Sometimes you’ll read “will not be killed” and sometimes “less likely to 
be killed.” Also, the way Android handles such things is subject to change with new Android 
versions. As a general rule of thumb, you should be conservative and expect the worst. In 
this case, read “less likely to be killed” and take precautions if the service ceases functioning 
while your app is performing some work.
Background Services
Background services run in the background; that is, they will not show an entry in the status 
bar. They are, however, allowed to use Toasts to send short notification messages to the 
user. Background services are more brittle compared to foreground services since Android 
expects them to be more loosely connected to user activities and thus more readily decides 
to kill them when there is a resource shortage.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   34   35   36   37   38   39   40   41   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling