Skip to main content

Posts

Showing posts with the label Kotlin

How to check internet connectivity in Android app using BroadcastReceiver

So let's get started with How to check internet connectivity in Android using BroadcastReceiver. For this post I'm using Kotlin language. Well lots of people are using simple method to get network connectivity using ConnectivityManager. But using that approach we will get network state only when we are calling that method. So how would I know that network state changed. This will achieved using BroadcastReceiver.  What is BroadcastReceiver? BroadcastReceiver uses Publish-Subscribe pattern. Android app can send and receive messages from Android system and other apps.  Android system sends broadcast when various system events occurred like device charging, network state changes. Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast. So for this scenario we need to add some changes in AndroidManifest.xml file - ...

Introduction to Kotlin

Why Kotlin? Java have so much boilerplate code. Some other languages has performance issues.  Kotlin is statically typed language that runs on the Java virtual machine and also can be compiled to JavaScript source code. Inspired by Java, Scala, C# and Groovy. Kotlin develop such a way that it can be use for any type of application such as web, Desktop,  Server-side. But it's adopted heavily in Android.  Interoperability allows gradual adoption. So no need to change whole codebase into Kotlin. Gradle has adopted Kotlin for it’s scripting and plugins. Gradle using Groovy for scripting and plugins. In Kotlin everything is an object. Two ways to declare variables in Kotlin : var for mutable and val is for immutable. These are some of the plus points of this language. So let's dive into this language. Kotlin programs start at the main function. When targeting JVM, function will be compiled as a static method. To save...