Android listens to network connection status via radio

Source: Internet
Author: User

First add the following permissions

<uses-permissionAndroid:name= "Android.permission.INTERNET" /><uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE" /><uses-permissionAndroid:name= "Android.permission.CHANGE_WIFI_STATE" /><uses-permissionAndroid:name= "Android.permission.CHANGE_NETWORK_STATE" /><uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE" />

New one broadcast Netstatechangebroadcast

 PackageAviationboss.broadcast;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.net.ConnectivityManager;Importandroid.net.NetworkInfo.State;ImportAndroid.util.Log;ImportAndroid.widget.Toast;ImportAviationboss.data.SystemData; Public classNetstatechangebroadcastextendsBroadcastreceiver {PrivateConnectivitymanager Connectivity; Private BooleanIsconnect =false; @Override Public voidonreceive (Context arg0, Intent arg1) {//TODO auto-generated Method StubConnectivity =(Connectivitymanager) arg0. Getsystemservice (arg0.        Connectivity_service); State Wifistate=Connectivity.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if(state.connected = =wifistate) {Isconnect=true; }                if(Connectivity.getnetworkinfo (connectivitymanager.type_mobile)! =NULL) {State mobilestate=Connectivity.getnetworkinfo (connectivitymanager.type_mobile). GetState (); if(state.connected = =mobilestate) {Isconnect=true; }} Intent Intent=NewIntent (); Intent.setaction ("Netconnect"); if(isconnect) {if(!systemdata.netstate) {toast.maketext (arg0,"Network is connected", Toast.length_short). Show (); Systemdata.netstate=true; Intent.putextra ("Netactions", "Network Connected");            Arg0.sendbroadcast (Intent); }        } Else {            if(systemdata.netstate) {Toast.maketext (arg0,"Network Disconnected", Toast.length_short). Show (); Systemdata.netstate=false; Intent.putextra ("Netactions", "Network Disconnected");            Arg0.sendbroadcast (Intent); }        }    }}

Determine the connection status of WiFi

Connectivity.getnetworkinfo (Connectivitymanager.type_wifi). GetState ()

Determine the connection status of a mobile network

Connectivity.getnetworkinfo (Connectivitymanager.type_mobile). GetState ()

Add the broadcast to the configuration file and set the listening action

<receiverAndroid:name= "Aviationboss.broadcast.NetStateChangeBroadcast" >    <Intent-filter>        <ActionAndroid:name= "Android.net.conn.CONNECTIVITY_CHANGE" />    </Intent-filter></receiver>

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.