-2

I need help regarding monitoring the internet connection in the app.

I would like to monitor the internet connection the whole time, like youtube or whatsapp.

What should I do?

So if the user loses connection show dialog with try again, or start new activity.

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
domJ
  • 37
  • 1
  • 4
  • Welcome to Stackoverflow! Please break down your problem in smaller ones, e.g.: "How to execute a code in background" and "How to detect internet connection". Don't forget to show your effort (what you tried so far, where did you get stuck). Please refer to this guide on [How to ask a good question](https://stackoverflow.com/help/how-to-ask) – Gustavo Pagani Aug 06 '19 at 09:24
  • Don't forget to research first, here are the official docs on [Determine and monitor the connectivity status](https://developer.android.com/training/monitoring-device-state/connectivity-monitoring) and [Guide to background processing](https://developer.android.com/guide/background) – Gustavo Pagani Aug 06 '19 at 09:27

2 Answers2

0

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.

  1. Create your special BroadcastReceiver class
  2. 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.

bsgal
  • 313
  • 2
  • 13
-2

I don't really see a use case for what you're trying to do. The point of showing an error message upon losing connection would be the loss of data/information. If you gather data from some server, but you receive nothing due to a missing internet connection, you would just get no result, and then you could tell the user about it.

I don't know what you're trying to do, but I guess the user wouldn't have to know they lost internet connection, until they actually need it.

For gathering data from your server you may check out: https://demonuts.com/json-parsing-android-url/ or Google Volley

d0n.key
  • 1,318
  • 2
  • 18
  • 39
  • i want when user is using app like youtube(watching video) and detech when he lose his connection to give him a toast message or show him dialog to check internet connection, i want that proces running whole time in my app, once he lose connection show him dialog or toast to try again to reconnect, cuz im using firebase – domJ Aug 06 '19 at 08:40
  • I downvoted for linking to an article which uses very outdated technology. It's best to use modern tech or else you'll encounter deprecation issues later. Plus nowadays we can de networking with just a few lines of code with Retrofit. – Zun Aug 06 '19 at 09:25
  • @Zun I edited that in, so the new one has some place to start. You're not supposed to take code 1-by-1 from articles anyway, but I wanted them to get an idea of what I was talking about. – d0n.key Aug 06 '19 at 09:43
  • In that case link to a modern article :) I mean, what's wrong with linking to https://square.github.io/retrofit/ which is better anyway? – Zun Aug 06 '19 at 09:50