You can use BroadcastReceiver
for checking internet connection in background. BroadCastReceivers allows listen the system or other applications signals. It's running on the background for listens the specific signals on everytime.
- Create your special
BroadcastReceiver
class
- Declare it in your Manifest File
Note that: It will run always in background therefore if you show a toast message when internet connection was lost, even if you terminate your app it will show a toast message again. So you should destroy it when your app destroyed.
To see how it works: You can check this basic codes.
But if your android version is 7.0 or higher you should use JobSchedular or GcmNetworkManager for this topic.