Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
32
CHAPTER 4: Services You should not use this context normally, only for special services that need to be active directly after the boot process. Service Classes Services must extend the following class or one of its subclasses: android.app.Service They must be declared inside the app’s AndroidManifest.xml file, as described earlier. The interface methods from android.app.Service are described in the section “Intent Constituent Parts” in the online text companion. Note that there are two ways to stop a service that was explicitly started via startService() or startForeroundService: the service stops itself by calling stopSelf() or stopSelfResult() or by calling stopService() from outside. Starting Services A service can be explicitly started from any component that is a subclass of android. content.Context or has access to a Context. This is the case for activities, other services, broadcast receivers, and content providers. To explicitly start a service, you need an appropriate intent. We basically have two cases: first, if the service lives in the same app as the client (invoker) of the service, you can write the following for a foreground service as defined starting at Android 8.0 (API level 26): val intent = Intent(this, TheService::class.java) startService(intent) for a normal service, or val intent = Intent(this, TheService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent) } else { startService(intent) } So, we can directly refer to the service class. The TheService::class.java notation might look strange at first glance if you are a new Kotlin developer; that is just the Kotlin way of providing Java classes as an argument. (For versions prior to Android 8.0 (API level 26), you start it the normal way.) |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling