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:
Add the following permissions:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Open the
activity_main.xml
file and replace the existingTextView
with the following views:<TextView android:id="@+id/textView" android:layout_width=...