Observe Internet connectivity state using old API (deprecated) and in API 29.
To observe Internet connectivity we do:
1. create new project
2. create new class ConnectivityReceiver.java extends BroadcastReceiver to observe network changes.
add new method CheckInternet() return true when mobile is connected to Internet, or false when is not connected.
create ConnectivtyManager object,then create NetworkInfo from it by calling getActiveNetworkInfo() method.
-evaluate network state by evaluate networkinfo.isConnected()
in onReceive mothod check value from previous method, call your code when connected/disconnected.
3. in Manifest file:
add premissions to manifest, ACCESS_NETWORK_STATE this is enough, however you also need INTERNET permission as a logic next step to handle data from INTERNET.
add receiver element with IntentFilter action: CONNECTIVITY_ACTION , and this step can be done either in XML Manifest file or by code.
4. In MainActivity create object for ConnectivityReceiver class.
onResume register/unregister our broadcast receiver to start watching network status, or to stop it.
----------------------------------------------------
The previous code is work well, but unfortunately deprecated in API 29
Some methods and constants like NetworkInfo method and Connectivity_action for example.
We have to use NetworkCapabilities and ConnectivityManager.NetowrkCallback.
We don't need any new Classes extends Broadcastreceiver, and no need to define receiver in Manifest just follow these steps.
1. create new project (I will delete the previous steps to return as new project)
2. in MainActivity we will create checkInternet() method return boolean value to observe internet connectivity (true when connected, false when disconnected).
create ConnectivityManager object.
create NetworkRequest object using builder, we will add all networks transports we interested to watch its state like WIFI and CELLULAR.
create callback object from ConnectivityManager.NetworkCallback and override the needed event methods like onAvailable(), onLost()
3. register/unregister NetworkCallback using onResume and onPause methods.
Note: I noticed English typos, but this does not affect the video goal.
Nesta página do site você pode assistir ao vídeo on-line Check Internet Connectivity in Android (java) duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Samer T 29 Junho 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 747 vezes e gostou 8 espectadores. Boa visualização!