Book Image

Mastering Android Development with Kotlin

By : Miloš Vasić
Book Image

Mastering Android Development with Kotlin

By: Miloš Vasić

Overview of this book

Kotlin is a programming language intended to be a better Java, and it's designed to be usable and readable across large teams with different levels of knowledge. As a language, it helps developers build amazing Android applications in an easy and effective way. This book begins by giving you a strong grasp of Kotlin's features in the context of Android development and its APIs. Moving on, you'll take steps towards building stunning applications for Android. The book will show you how to set up the environment, and the difficulty level will grow steadily with the applications covered in the upcoming chapters. Later on, the book will introduce you to the Android Studio IDE, which plays an integral role in Android development. We'll use Kotlin's basic programming concepts such as functions, lambdas, properties, object-oriented code, safety aspects, type parameterization, testing, and concurrency, which will guide you through writing Kotlin code in production. We'll also show you how to integrate Kotlin into any existing Android project.
Table of Contents (24 chapters)
Title Page
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Using on boot and on shutdown broadcasts


Sometimes, it's crucial for services to run as the application starts. Also, sometimes it is important to do some cleanup work before we terminate it. In the following example, we will extend the Journaler application to listen for these broadcast messages and do some work. First thing that we will do is create two classes that extend the BroadcastReceiver class:

  • BootReceiver: This is to handle the system boot event
  • ShutdownReceiver: This is to handle the system shutdown event

Register them in your manifest file as follows:

    <manifest  
     ... 
    > 
    ... 
    <receiver 
        android:name=".receiver.BootReceiver" 
        android:enabled="true" 
        android:exported="false"> 
        <intent-filter> 
          <action android:name=
           "android.intent.action.BOOT_COMPLETED" /> 
        </intent-filter> 
        
        <intent-filter> 
          <action android:name=
          "android.intent...