In android, by using the ConnectivityManager class we can easily determine whether the device connected to the network/internet or not and also we can determine the type of internet connection currently available i.e. whether it’s mobile data or Wi-Fi.
To get the internet connection status, our app must acquire the INTERNET and ACCESS_NETWORK_STATE permissions. For that, we need to add the following permissions in the android manifest file like as shown below.
....
</manifest>
In android, we can determine the internet connection status easily by using getActiveNetworkInfo() method of ConnectivityManager object.
Following is the code snippet of using the ConnectivityManager class to know whether the internet connection is available or not.
ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cm.getActiveNetworkInfo();
boolean connected = nInfo != null && nInfo.isAvailable() && nInfo.isConnected();
If you observe above code snippet, we used getActiveNetworkInfo() method of ConnectivityManager object to know whether internet connection available or not.
In android, we can easily determine the type of internet connection currently available i.e. either WI-FI or mobile data by using the getType() method of NetworkInfo object.
Following is the code snippet to get the type of internet connection in the android application.
ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cm.getActiveNetworkInfo();
boolean isWiFi = nInfo.getType() == ConnectivityManager.TYPE_WIFI;
If you observe above code snippet, we used getType() method of NetworkInfo object to know the type of internet connection.
Now we will see how to save files directly on the device’s internal memory and read the data files from device internal memory by using FileOutputStream and FileInputStream objects in android application with examples.
Following is the example of checking whether the internet connection available or not using the android ConnectivityManager object.
Create a new android application using android studio and give names as InternalConnectionExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.
Now open your main activity file MainActivity.java from \java\com.tutlane.internalstorageexample path and write the code like as shown below
If you observe above code, we are getting the internet connection status by using ConnectivityManager object.
Now we need to acquire the permissions of INTERNET and ACCESS_NETWORK_STATE for our android application for that open AndroidManifest.xml file and add the permissions like as shown below.
If you observe above example, we are getting the internet connection status and added required permissions in AndroidManifest.xml file.
When we run the above example in the android emulator we will get a result like as shown below.
If you observe the above result, When we click on the Check Internet Connection button it will fetch the status of internet connection whether the internet available not.
This is how we can check the internet connection status in android applications using the ConnectivityManager object based on our requirements.