How to check internet availability in app | Android App Development | Java
In this post, you will learn how to check the internet availability access in your application. Checking the internet access is important when working with API's, fetching or uploading data from servers etc...
Implementation:
First, in the activity_main.xml file, add the following code:
Then, create a new activity (NewActivity), and in its xml file (activity_new.xml), add the following code:
Now in the MainActivity.java file, add the onClickListener to the button to navigate to the NewActivity. To do this, add the following code:
After this, create a new Java class to check the network state. In this class (CheckNetwork.java), add the following code:
Now, in the NewActivity.java file, add the following code:
Finally, add the Internet and Access Network State permissions in the AndroidManifest.xml file
And that's it...Now try running your app. To learn more about reading the Network States in Android, refer this : https://developer.android.com/training/basics/network-ops/reading-network-state?authuser=1
Happy Learning... :-)
Comments
Post a Comment