[Android] listener for system network connection opening or closing messages

Source: Internet
Author: User

It's very simple.Code.

 
Package xxx; import Android. content. broadcastreceiver; import Android. content. Context; import Android. content. Intent; import android.net. connectivitymanager; import android.net network. Info;
 
Public class netstatereceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {connectivitymanager manager = (connectivitymanager) context. getsystemservice (context. connectivity_service); networkinfo GPRS = manager. getnetworkinfo (connectivitymanager. type_mobile); networkinfo WiFi = manager. getnetworkinfo (connectivitymanager. type_wifi); If (! GPRS. isconnected ()&&! Wifi. isconnected () {// network closed} else {// network opend
 
}}}

The Code customizesBroadcastreceiverTo listen for network settings.

ManifestFile to register the listener.

 
<Cycler Android: Name = "XXX. netstatereceiver "> <intent-filter> <action Android: Name =" android.net. conn. connectivity_change "/> </intent-filter> </receiver>

You also need to add permission information.

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

 

 

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.