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
.
Create a new project in Android Studio and call it isOnline
. Use the default Phone & Tablet
options and select Empty Activity
in the Add an Activity
to Mobile
dialog.
First, we need to add the necessary permissions to access the network. Then, we'll create a simple layout with 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 existing layout with the following:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android...