Android Network State Real-time listener instance code (b) _android

Source: Internet
Author: User

The previous article to introduce the Android network status of real-time monitoring code example (i), interested friends can click to learn more, this article then to introduce the status of the Android network monitoring related knowledge, the specific content as follows:

When developing Android applications, it involves network access, often requiring network state checks to provide users with the necessary reminders. It is generally possible to complete the work by Connectivitymanager.

Connectivitymanager has four main tasks:

1, monitor the status of mobile phone network (including Gprs,wifi, UMTS, etc.)

2, mobile phone status change, send broadcast

3, when a network connection fails to failover

4. Provide applications with high precision and rough state of access to available networks

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

1, define a receiver overload of which the OnReceive function, in which to complete the required functions, such as WiFi and GPRS is disconnected to change the appearance of the space or to indicate whether access to the network

 package com.bsharksdk.receiver; import com.bsharksdk.main.BSharkApi; import
Com.bsharksdk.ui.BSToast;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo; /** * @author Javen * */public class Connectionchangereceiver extends Broadcastreceiver {@Override public void Onreceiv E (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); if (!mobnetinfo.isconnected () &&!wifinetinfo.isconnected ()) {Bstoast.showlong (context, "Network not Available");//change background or Handle network global variable}else {//change background or global variable for processing Network}}} 

2, in the appropriate place to register receiver, you can register in the program, in OnCreate call the following functions can be:

private void Registerreceiver () {
intentfilter filter=new intentfilter (connectivitymanager.connectivity_action) ;
Myreceiver=new connectionchangereceiver ();
This.registerreceiver (myreceiver, filter);

3, cancel the registration receiver when appropriate, can cancel in the program, call the following function in Ondestroye:

private void Unregisterreceiver () {
this.unregisterreceiver (myreceiver);

Remember to set variables in the activity private connectionchangereceiver myreceiver;

Remember to add the permissions needed to access the network

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android: Name= "Android.permission.INTERNET"/>

The above is a small set to introduce the Android network status real-time Monitor instance code (2), I hope to help you!

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.