How to Build Android Apps with Kotlin by Alex Forrester Eran Boudjnah Alexandru Dumbravan 
 and Jomar Tigcal

How to Build Android Apps with Kotlin by Alex Forrester Eran Boudjnah Alexandru Dumbravan 
 and Jomar Tigcal

Author:Alex Forrester, Eran Boudjnah, Alexandru Dumbravan,
 and Jomar Tigcal
Language: eng
Format: epub
Publisher: Packt Publishing Pvt. Ltd.
Published: 2021-02-25T00:00:00+00:00


Run your app:

Figure 8.1: Toasts showing in order

You should now see a simple Hello World! screen. However, if you wait a few seconds, you will start seeing toasts informing you of the progress of your SCA preparing to deploy to the field. You will notice that the toasts follow the order in which you enqueued the requests and execute their delays sequentially.

Background Operations Noticeable to the User – Using a Foreground Service

With our SCA all suited up, they are now ready to get to the assigned destination. To track the SCA, we will periodically poll the location of the SCA using a foreground service and update the sticky notification (a notification that cannot be dismissed by the user) attached to that service with the new location. For the sake of simplicity, we will fake the location. Following what you learned in Chapter 7, Android Permissions and Google Maps, you could later replace this implementation with a real one that uses a map.

Foreground services are another way of performing background operations. The name may be a bit counter-intuitive. It is meant to differentiate these services from the base Android (background) services. The former are tied to a notification, while the latter run in the background with no user-facing representation built in. Another important difference between foreground services and background services is that the latter are candidates for termination when the system is low on memory, while the former are not.

As of Android 9 (Pie, or API level 28), we have to request the FOREGROUND_SERVICE permission to use foreground services. Since it is a normal permission, it will be granted to our app automatically.

Before we can launch a foreground service, we must first create one. A foreground service is a subclass of the Android abstract Service class. If we do not intend to bind to the service, and in our example, we indeed do not, we can simply override onBind(Intent) so that it returns null. As a side note, binding is one of the ways for interested clients to communicate with a Service. We will not focus on this approach in this book, as there are other, easier approaches, as you will discover below.

A foreground service must be tied to a notification. On Android 8 (Oreo, or API level 26) and above, if a foreground service is not tied to one within the Application Not Responding (ANR) time window (around 5 seconds), the service is stopped, and the app is declared as not responding. Because of this requirement, it is best if we tie the service to a notification as soon as we can. The best place to do that would be in the onCreate() function of the service. A quick implementation would look something like this:

private fun onCreate() {

val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

val newChannelId = "ChannelId"

val channelName = "My Background Service"

val channel =

NotificationChannel(newChannelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)

val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

service.createNotificationChannel(channel)

newChannelId

} else {

""

}

val pendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent ->

PendingIntent.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.