[Android] Android listens to network status + source code

Source: Internet
Author: User

You want to obtain the android network events

First, you must register a explorer.

 <  Cycler  Android: Name  = "Com. Ray. Ray. Receiver. connectionchangereceiver"  Android: Label  = "Networkconnection"   >     <  Intent-Filter  >          <  Action  Android: Name  = "Android.net. Conn. connectivity_change"  />          <  Action  Android: Name  = "Android. Intent. Action. user_present"   />     </  Intent-Filter  >  </  Cycler  > 

When the network status changes, the android.net. Conn. connectivity_change broadcast is sent.

The android. Intent. Action. user_present broadcast will be sent when the mobile phone is unlocked. In order to get it when the mobile phone is unlocked, the broadcast of the mobile phone unlock event will be added.

After registering a consumer, you can receive a broadcast of network status changes and mobile phone unlock actions in connctionchangereceiver.

Next, we can process the received broadcast by adding the network status judgment in onreceive as follows:

 
Connectivitymanager connectmgr =(Connectivitymanager) Context. getsystemservice (context. connectivity_service); networkinfo mobnetinfo=Connectmgr. getnetworkinfo (connectivitymanager. type_mobile); networkinfo wifinetinfo=Connectmgr. getnetworkinfo (connectivitymanager. type_wifi );

Connectivitymanager obtains the current system service information
Getnetworkinfo obtains the information of two network modes, mobile and WiFi.

Determine the current network status by judging the Boolean values returned by mobnetinfo. isconnected () and wifinetinfo. isconnected.

If(! Mobnetinfo. isconnected ()&&!Wifinetinfo. isconnected ()){//Unconnect Network}Else{//Connect Network}

Only whether the network is connected or not is added here. If you need to distinguish between data and Wi-Fi connections, you can determine whether to connect to the network separately.

Note that the permission to access Android permission is added.

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

 

The preceding figure shows the network status of the android listener.CodeImplementation Process

RequiredSource codeCan be obtained in the following two ways:

(1) http://download.csdn.net/detail/fnext/5228714

(2) enter the email address in the comment and the source code will be sent through the email address.

 

//////////////////////////////////////// //////

Reprinted original statement, please note

This article is from Ray-Ray's blog

ArticleAddress http://www.cnblogs.com/rayray/archive/2013/03/25/2965474.html 

Thank you for your recommendation and favorites.

Your support! Our motivation!

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.