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.
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.
1 . Public Class Connectionchangereceiver Extends
Broadcastreceiver
2 .{
3 . @ Override
4 . Public Void Onreceive (context, intent)
5 .{
6 . Connectivitymanager =
(Connectivitymanager) Context. getsystemservice
(Context. connectivity_service );
7 . Networkinfo activenetinfo = Connectivitymanager.
Getactivenetworkinfo ();
8 . Networkinfo mobnetinfo = Connectivitymanager. getnetworkinfo
(Connectivitymanager. type_mobile );
9 . If (Activenetinfo ! = Null )
10 .{
11 . Toast. maketext (context, " Active Network Type: " +
Activenetinfo. gettypename (), Toast. length_short). Show ();
12 .}
13 . If (Mobnetinfo ! = Null )
14 .{
15 . Toast. maketext (context, " Mobile Network Type: " +
Mobnetinfo. gettypename (), Toast. length_short). Show ();
16 .}
17 .}
18 .}
19 . < ! -- Needed to check when the network connection changes -->
20 . < Uses - Permission Android: Name = " Android. permission.
Access_network_state " />
21 . < Cycler Android: Name = " Com. blackboard. androidtest.
Receiver. connectionchangereceiver "
22 . Android: Label = " Networkconnection " >
23 . < Intent - Filter >
24 . < Action Android: Name = " Android.net. Conn. connectivity_change " />
25 . < / Intent - Filter >
26 . < / Cycler >