Detect network in Android

Posted by


Detecting network is so important for application. in the case disconnected and connected make a prompt to users know what the user should do next action.

In this tutorial. I will make guide how to listen Broadcast receiver component in Android. when the network is changing or connecting.

Let’s get started.

Download Now

  1. Creating project.
    • From Android Studio File -> New Project
    • ¬†
  2. Creating TPApplication class
    • Let your app init context you need to create the TPApplication¬† class and extend Application component.
    • This class I am using singleton in order to get context

  3. Creating TPReceiver class.
    • This class has mission to listener wifi network changing and then pushing info to component Activity or Application.

  4. Next step register permission and declare application and Broadcast receiver.
    • In this step is so important to pay attention. after TPApplication and TPReceiver was created. You need to call them to manifest. take a look at.

    • Asking permission

    • Full code

  5. Designing UI

  6. MainActivity 
    • This is final step you need to map id from xml and implement interface to listen some detect network.
    • In this tutorial I am using Switch widget to change status of wifi
    • Status is on allow wifi to open and else off wifi.
    • onNetworkConnectionChanged this func to listener from broadcast receiver when the status of network changing.
    • onDetectStatus this func to update status to TextView let user know what the status now on your device.
    • Remember this guide work on real device.

  7. Running and getting a great result.


That’s all detect network working on real device. it’s necessary for your project. when your project interact with network. it will help you catch action and avoid crash app.

Wish happy coding.

Leave a Reply

Your email address will not be published. Required fields are marked *