android--Solution-Monitor mobile network status changes with Broadcastreceiver

Source: Internet
Author: User

There are a number of ways to implement a feature, but what we're looking for is the one that works best for your project.

Like the app to determine the state of the network, if every time when using the network to determine the network status, some time-consuming. For example, if you want to determine whether the network is unblocked before you open the Web page and determine the type of network before downloading the image, it is important to increase the wait time. Therefore, we can try simply to record the current network status when the mobile network status changes to our app, in the use of the network where there is no need to actively check the network.

Needless to say, this article is mainly through the use of Android broadcasting mechanism broadcastreceiver to receive the system in the network state changes when the broadcast connectivitymanager.connectivity_action can be issued.

First, write a Broadcastreceiver class:

Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.net.connectivitymanager;import Android.util.log;import Android.widget.toast;public class MyReceiver extends                broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals (connectivitymanager.connectivity_action)) {Toast.maketext (context, "my                     Areceiver network Changed ", Toast.length_long). Show ();     Methods.refreshapntype (context); New Thread (New Runnable () {@Overridepublic void Run () {//Where methods is the final class, the parameter Networktype is the current    The logo of the network type in the application//here needs to be modified according to the project Methods.networktype = Getapntype (context);}                }). Start (); }}/** * Get current network status: No network 0:wifi network 1:3g Network 2:2g Network 3 * @param context * @return */private static int Getapntype (context context ) {int netType = 0; Connectivitymanager CONNMGR = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo networkinfo = Connmgr.getactivenetworkinfo (); if (networkinfo = = null) {return netType;} int nType = Networkinfo.gettype (); if (NType = = Connectivitymanager.type_wifi) {NetType = 1;//WIFI} else if (NType = = Conn ectivitymanager.type_mobile) {int nsubtype = Networkinfo.getsubtype (); Telephonymanager mtelephony = (telephonymanager) context.getsystemservice (Context.telephony_service); if (NSubType = = telephonymanager.network_type_umts&&!mtelephony.isnetworkroaming ()) {NetType = 2;//3G} else {netType = 3;//2G }}return NetType;}}

Second, register the above broadcast recipients in the app-initiated activity or in the Androidmanifest.xml.

mode one: The code for registering the broadcast recipient in the activity is as follows:

Intentfilter mfilter = new Intentfilter ();        Mfilter.addaction (connectivitymanager.connectivity_action);        Myreceiver mreceiver = new Myreceiver ();        Registerreceiver (Mreceiver, Mfilter);

mode two: Through the configuration file for static registration:

<receiverandroid:name= "Com.xxx.MyReceiver" android:label= "networkconnection" >            <intent-filter>                <action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/>            </intent-filter></ Receiver>

at this point, a complete network monitoring function has been finished.


android--Solution-Monitor mobile network status changes with Broadcastreceiver

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.