Checking online status and connection type

This is a simple recipe, but one that is very common and will probably be included in every Internet application you build: checking online status. While checking online status, we can also check the connection type: WIFI or MOBILE.

Getting ready

Create a new project in Android Studio and call it isOnline. Use the default Phone & Tablet option and select Empty Activity when prompted for Activity Type.

How to do it...

First, we need to add the necessary permissions to access the network. Then, we'll create a simple layout with a Button and TextView. To get started, open the Android Manifest and follow these steps:

  1. Add the following permissions:
    <uses-permission android:name="android.permission.INTERNET"/> <uses-permission ...

Get Android Application Development Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.