Network: Android uses broadcast to listen to the network status

Source: Internet
Author: User

Blog http://blog.csdn.net/androidbluetooth/article/details/6860146 detailed paste a lot of methods to determine the network.


Recently, we have encountered such a requirement:

The mobile phone can monitor the network status at any time. If the network status changes, you must promptly update the app information to notify users.


There is a good way to achieve this (I personally think that you must have a better way to share it) and share it with everyone!

To listen at any time, you need to monitor the network status of a service in the background. How can you receive information that changes in the network status?

Well, of course it's broadcastreceiver.


When the network status changes, the system sends android.net. Conn. connectivity_change.


The value is described as follows:

Public static final string connectivity_action

Since: API Level 1
A change in network connectivity has occurred.
A connection has either been established or lost.
The networkinfo for the affected network is sent as an extra;
It shoshould be consulted to see what kind of connectivity event occurred.
If this is a connection that was the result of failing over from a disconnected network,
Then the failover_connection Boolean extra is set to true.
For a loss of connectivity,
If the connectivity manager is attempting to connect (or has already connected) to another network,
The networkinfo for the new network is also passed as an extra.
This lets any receivers of the broadcast know that they shocould not necessarily tell the user that no data traffic will be possible.
Instead, the reciever shoshould have CT another broadcast soon,
Indicating either that the Failover attempt succeeded (and so there is still overall data connectivity ),
Or that the Failover attempt failed, meaning that all connectivity has been lost.
For a disconnect event, the Boolean extra extra_no_connectivity is set to true if there are no connected networks at all.
Constant Value: "android.net. Conn. connectivity_change"


This is a constant of the connectivitymanager class.

OK. The demo is implemented as follows:


Package mark. Zhang;

Import Android. App. Service;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. intentfilter;
Import android.net. connectivitymanager;
Import android.net. networkinfo;
Import Android. OS. ibinder;
Import Android. util. log;

Public class listennetstateservice extends Service {
Private connectivitymanager;
Private networkinfo Info;

Private broadcastreceiver mreceiver = new broadcastreceiver (){

@ Override
Public void onreceive (context, intent ){
String action = intent. getaction ();
If (action. Equals (connectivitymanager. connectivity_action )){
Log. D ("mark", "the network status has changed ");
Connectivitymanager = (connectivitymanager)

Getsystemservice (context. connectivity_service );
Info = connectivitymanager. getactivenetworkinfo ();
If (info! = NULL & info. isavailable ()){
String name = info. gettypename ();
Log. D ("mark", "current network name:" + name );
} Else {
Log. D ("mark", "No network available ");
}
}
}
};

@ Override
Public ibinder onbind (intent ){
Return NULL;
}

@ Override
Public void oncreate (){
Super. oncreate ();
Intentfilter mfilter = new intentfilter ();
Mfilter. addaction (connectivitymanager. connectivity_action );
Registerreceiver (mreceiver, mfilter );
}

@ Override
Public void ondestroy (){
Super. ondestroy ();
Unregisterreceiver (mreceiver );
}

@ Override
Public int onstartcommand (intent, int flags, int startid ){
Return super. onstartcommand (intent, flags, startid );
}
}


Add a permission to the manifest file:

<Uses-Permission Android: Name = "android. Permission. access_network_state"/>


Let's look back at connectivity_action. We can also get a message from the API:

You can use intent to obtain some extra, such as extra_no_connectivity.


Boolean B = intent. getbooleanextra (connectivitymanager. extra_no_connectivity, true );


For more information, see connectivitymanager.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.