Android determines the network status. In the Android operating system, how can we correctly determine whether the network we connect is disconnected? Today, we will make a detailed analysis on this application technique.
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 "/>
<Cycler android: name = "com. blackboard. androidtest.
Receiver. ConnectionChangeReceiver"
Android: label = "NetworkConnection">
<Intent-filter>
<Action android: name = "android.net. conn. CONNECTIVITY_CHANGE"/>
</Intent-filter>
</Cycler>
The specific application method for Android to determine the network status is described here.