How to Observe Internet Connectivity in Android - Android Studio Tutorial

How to Observe Internet Connectivity in Android - Android Studio Tutorial

Philipp Lackner

1 год назад

45,768 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Asad Ansar
Asad Ansar - 29.07.2023 10:22

Hey Phillip its a great video. but how to use the check internet flag in our viewModel. do we need to check internet on every api call or any better global way to check?

Ответить
Ayush Singhal
Ayush Singhal - 29.06.2023 07:18

How to use it in viewModel where we can't have applicationContext

Ответить
Music Shorts
Music Shorts - 27.04.2023 09:16

Hi bro you know me who I'm

Ответить
Let's Code
Let's Code - 17.04.2023 17:26

Good Explanation . Thank you

Ответить
Rajneesh Roy
Rajneesh Roy - 26.03.2023 04:58

Network status is updated,but what happens when we have wifi connection but not internet.

Ответить
Ray DeVivo
Ray DeVivo - 17.03.2023 22:05

I believe there is a bug here - if you open the app while network is already available, wouldn't the app still show Unavailable since the initial state you set was UNAVAILABLE and the callback would not actually be triggered for AVAILABLE since it is already available?
I think there needs to be a check for initial state

Ответить
Brian Maum
Brian Maum - 01.03.2023 00:56

I tried this implementation to show a toast/snackbar whenever the network connection is lost. It works but the observe function seems to emit duplicate states showing multiple toasts, even with distinctuntilchanged(), I see the same behavior even when converting to a state flow. I fixed this by assigning the send blocks to a job and and canceling the job on each network change call with a delay: ex: override fun onLost(network: Network) {
super.onLost(network)
job?.cancel()
job = launch {
delay(500)
trySend(ConnectivityObserver.Status.Lost)
}
}

Ответить
Sajid García
Sajid García - 04.02.2023 04:29

Very useful, but what happen with mobile network but not internet data, still get Available

Ответить
David
David - 13.01.2023 17:57

What if user has +4G on but got no data plan? What can i do in that case?

Ответить
Moments like this
Moments like this - 05.01.2023 16:20

this shit.
this code work only when we off wifi and mobile data. when we have bad internet connection this code return avaliable

Ответить
Talha Ç
Talha Ç - 30.12.2022 11:01

Should i use sync adapter to synchronize my local db with server?

Ответить
Mohini Thakkar
Mohini Thakkar - 22.12.2022 19:14

I've implemented this, but when my internet is off and i'm opening the application, it doesn't trigger disconnected state!!
Can you please give brief!
Thanks

Ответить
TheRcfrias
TheRcfrias - 21.12.2022 01:18

Did you ever handle that last scenario?, A very frequent scenario is when you connect to a public wifi and the network is saturated or internet is down. You will still be connected to the internet, but you wont have any internet access whatsoever.

Ответить
Dota2 Heralds
Dota2 Heralds - 05.12.2022 17:38

Hey I'm Unable to get the unavailable status im not using compose just observing in activity what am I doing wrong?

Ответить
Anudeep Ananth
Anudeep Ananth - 24.11.2022 13:28

Nice video.... Thank u so much for this....

Ответить
Tejas Tidake
Tejas Tidake - 23.11.2022 20:57

Thank You for making video in Kotlin 🙏

Ответить
ApunichJOHNCENA
ApunichJOHNCENA - 23.11.2022 09:32

How to pass initial state in non-compose application?

Ответить
Chinedum Roland Eke
Chinedum Roland Eke - 01.11.2022 16:36

Hi Philip thanks for this video. I am trying to implement this in my application that uses hilt for DI. I have a custom Interceptor that adds some auth header before the request is being processed, but my application crashes as soon as it opens. How can I catch the error and send it to the user? Thank you

Ответить
Vishnu Haridas
Vishnu Haridas - 15.09.2022 21:36

The observe() function registers a new network callback every time we try to observe it and returns a cold flow. We can prevent this by converting the cold flow into a hot flow using the `shareIn` or `stateIn` operators. We can add a property to the `class NetworkConnectivityObserver` like this:

```
val status: Flow<Status> = observe().stateIn(scope, WhileSubscribed(), Status.Unavailable) // Injected scope
```

Or creating a `class NetworkStatusRepository(CoroutineScope, ConnectivityObserver)` which creates a property like above will be better.

Ответить
David Scammell
David Scammell - 15.09.2022 15:32

Thank you for your great content, however I seem to get an issue with this - my App doesn't fire when you first hook up the flow and you have no internet - I was expecting it to show 'Unavailable' as it does with 'Available'...

Ответить
DK Compilation 🔴
DK Compilation 🔴 - 11.09.2022 09:44

how to observe it without compose collectasstate is not the option available in normal xml

Ответить
HXRIN
HXRIN - 16.08.2022 16:20

Hey, great video, I've tried to implement it but it seems like onUnavailable is not getting triggered? I've tried and turned off wifi and data in emulator, started the app and no log message showed up. Then when I turned on Wifi, Available status got sent to the channel and then when I turned it off, Lost status got sent. But never the Unavailable.

Ответить
Dïkėñ MhRź
Dïkėñ MhRź - 16.08.2022 09:56

If I have to ping google to check whether connection really exists or not, how should I do it?

Ответить
Chinenye Ikpa
Chinenye Ikpa - 15.08.2022 15:00

This is timely. Thanks a lot for the thorough breakdown on how and why.

Ответить
wanjusi abdalla
wanjusi abdalla - 14.08.2022 11:17

Hi, this crushes app if you turn off and on and turn it back while observing some state.

Ответить
sanket vetkoli
sanket vetkoli - 13.08.2022 12:57

Hi Philipp, thanks for the awesome video, one question, if we were to implement this in ViewModel, we would need to extend AndroidViewModel for getting the application context, is there a way we can do this with extending the simple ViewModel class?

Ответить
Luke Alvin Madzedze
Luke Alvin Madzedze - 13.08.2022 10:05

Hey bro, Thank you so much for the great content 🙏

One question, can we not use trySend() instead of launch { send( ) }

or is it ultimately the same thing

Thanks

Ответить
Baiju Sharma
Baiju Sharma - 13.08.2022 09:57

Hi, I have a doubt. For eg: fun getUsers(): Flow<List<ApiUser>> and suspend fun getUsers(): List<ApiUser>. Here both the function will fetch the list of data from the API. What are the benefits of converting API data or List into FLOW? Also, my app does not retrieve streams of data like the stock market or video streaming app. Do I really need to use Flow?

Ответить
Hristo Ivanov
Hristo Ivanov - 12.08.2022 22:00

Android боклук 😂 Iso iphone 🤘

Ответить
Antonio Guerra
Antonio Guerra - 12.08.2022 20:26

Hey bro I have a question, Have you thought about doing an updated augmented reality course, perhaps with the scene view library on udemy or I don't know if on any platform? Because the scene form is no longer maintained and it is a world with a lot of potential but little explored. Thank you.

Ответить
Apzar
Apzar - 12.08.2022 20:04

What's your thought on creating a state based application like implementing FSM(Finite State Machine) in its brother ?

Ответить
Sandra Verma
Sandra Verma - 12.08.2022 12:43

*Mrs Charlotte is legit and her method works like magic I keep on earning every single week with her new strategies *

Ответить
Mohammeddhiyaeddine GOUAOURI
Mohammeddhiyaeddine GOUAOURI - 12.08.2022 01:32

Thank you sir for the great content, I just want to know where does the observer fit in the clean architecture design

Ответить
Emmanuel Mtera
Emmanuel Mtera - 11.08.2022 16:32

Phillip i was finding a solution like this, thanks brother.

Ответить
Soumya Ranjan
Soumya Ranjan - 11.08.2022 15:44

Awesome tutorial Phill

Ответить
Hao The
Hao The - 11.08.2022 08:02

I try these code but these callback is not work(apart from onAvailable)..

Ответить
Max Weninger
Max Weninger - 10.08.2022 23:33

Very nice. Exactly what I needed right now 😂

Ответить
Mustafa Ammar
Mustafa Ammar - 10.08.2022 22:05

VERY COOL
YOU ARE THE BEST

Ответить
Erfan Sn
Erfan Sn - 10.08.2022 21:26

Wouldn't it be better to use StateFlow, less and cleaner code?

Ответить
alco
alco - 10.08.2022 21:26

It's a useful demo for handling the context depend resource by callbackflow ❤️

Ответить
Dmytro Berezhnyi
Dmytro Berezhnyi - 10.08.2022 21:17

Connectivity means nothing, if it is connected not the fact user have internet

Ответить
Support reply email
Support reply email - 10.08.2022 19:40

please tell me how to create game animation
please tell me about graphics
canvas unity, agdk

Ответить
MrSoommy
MrSoommy - 10.08.2022 19:07

Great tutorial! I would love to have a video which will expand this knowledge to Ethernet connection observer via broadcast receiver for Android boxes!

Ответить
Ankit Verma
Ankit Verma - 10.08.2022 18:25

Much needed 💙

Ответить
Izaac Doyle
Izaac Doyle - 10.08.2022 18:13

I am having an Issue to when I add the Permissions for the Network_State. it Informs me even after adding to manifest permission is still missing

Ответить
iJoZPepe
iJoZPepe - 10.08.2022 16:58

How can I convert this code to a service, to monitoring all mi app no only one activity? 🤔

Ответить