Android real-time monitoring network status

Source: Internet
Author: User

When developing an Android app, it involves network access, often requiring network status checks to provide users with the necessary reminders. This work can generally be done through Connectivitymanager.

Connectivitymanager has four main tasks:

1. Monitor mobile network status (including Gprs,wifi, UMTS, etc.)

2, when the mobile phone status changes, send broadcast

3. Failover when a network connection fails

4. Provide the application with a high-precision and coarse state that can obtain available networks

When we want to listen to the network status in the program, just a few steps:

1, processing Recevier interface

 PackageCom.gense.ips.main.receiver;ImportJava.util.Map;/*** * Broadcast Receiver Processing interface * *@authorJ.wang *@version[version number, April 29, 2015] *@see[Related Classes/methods] *@since[Product/module Version]*/ Public Interfacereceiverhandler{ Public voidHandlerreceiver (intReceivertype,map<string,object>param);}

2, define a receiver overloaded with the OnReceive function, in which to complete the required functions, such as based on WiFi and GPRS is disconnected to change the appearance of the space or whether to access the network

 PackageCom.gense.ips.main.receiver;ImportJava.util.HashMap;ImportJava.util.Map;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.net.ConnectivityManager;ImportAndroid.net.NetworkInfo;/** * @authorJ.wang **/ Public classConnectionchangereceiverextendsBroadcastreceiver { Public StaticString net_conenct_status = "Net_conenct_status";  Public Static intReceiver_type = 100010; //broadcast receive processor    PrivateReceiverhandler Receiverhandler;  Publicconnectionchangereceiver (Receiverhandler receiverhandler) { This. Receiverhandler =Receiverhandler; } @Override Public voidOnReceive (Context context, Intent Intent) {Connectivitymanager Connectivitymanager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo Mobnetinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_mobile); Networkinfo Wifinetinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); Map<string,object> params =NewHashmap<string,object>(); if(!mobnetinfo.isconnected () &&!wifinetinfo.isconnected ()) {            //change the background or deal with the global variables of the networkParams.put (Net_conenct_status, "false"); }Else {            //change the background or deal with the global variables of the networkParams.put (Net_conenct_status, "true"); }                 This. Receiverhandler.handlerreceiver (Receiver_type,params); }}

3, register receiver, you can register in the program,

    /**      * Register receiver     @see  [Class, Class # method, Class # member]     */    protected void _registerreceiver (broadcastreceiver broadcastreceiver)    {        intentfilter filter=new  intentfilter (connectivitymanager.connectivity_action);          This . Registerreceiver (broadcastreceiver, filter);    }

4. Cancellation of receiver Registration

/**@param@see  [Class, Class # method, Class # member]*/protected  void  _unregisterreceiver (broadcastreceiver broadcastreceiver) {this. Unregisterreceiver (Broadcastreceiver);}

5. Receiver Program Processing

/*** Handling System broadcast Events *@paramparam*/     Public voidHandlerreceiver (intReceivertype,map<string, object>param) {        //Handling Network Connection Notifications        if(Receivertype = =Connectionchangereceiver.receiver_type) {String Isconnect=stringutils.get (param, connectionchangereceiver.net_conenct_status); if("True". Equals (Isconnect)) {Httptask Task=NewHttptask ( This, constants.login_load_parameters);            Task.execute (); }            Else            {                 This. Showshortmessage ("Network Not Connected"); }        }            }

Android real-time monitoring network status

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.