How to judge the state of network in Android
This article introduces the relevant knowledge of "how to judge the status of the network in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The code is as follows:
Public class ConnectionChangeReceiver extends
BroadcastReceiver
{
@ Override
Public void onReceive (Context context, Intent intent)
{
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService
(Context.CONNECTIVITY_SERVICE)
NetworkInfo activeNetInfo = connectivityManager.
GetActiveNetworkInfo ()
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
(ConnectivityManager.TYPE_MOBILE)
If (activeNetInfo! = null)
{
Toast.makeText (context, "Active Network Type:" +
ActiveNetInfo.getTypeName (), Toast.LENGTH_SHORT) .show ()
}
If (mobNetInfo! = null)
{
Toast.makeText (context, "Mobile Network Type:" +
MobNetInfo.getTypeName (), Toast.LENGTH_SHORT) .show ()
}
}
}
< !-- Needed to check when the network connection changes --> < uses-permission android:name="android.permission. ACCESS_NETWORK_STATE"/> < receiver android:name="com.blackboard.androidtest. receiver.ConnectionChangeReceiver" android:label="NetworkConnection"> < intent-filter> < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> < /intent-filter> < /receiver>This is the end of the content of "how to judge the status of the network in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!