In fact, the mobile phone in the network monitoring is also more important, sometimes we must real-time monitoring of the program's real-time network status, Android in the network disconnect and connect the time will be broadcast, we through the reception system broadcast can realize network monitoring.
1, add access to the network and obtain network status of the rights
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android: Name= "Android.permission.INTERNET"/>
2. Create a new application class and inherit the android.app.Application declaration variable to save the network state
Package com.javen.application;
Import Com.javen.netbroadcast.NetUtil;
public class Application extends Android.app.Application {
private static application mapplication;
public static int mnetworkstate;
public static synchronized Application getinstance () {return
mapplication;
}
@Override public
void OnCreate () {
super.oncreate ();
Mapplication = this;
InitData ();
}
public void InitData () {
mnetworkstate = netutil.getnetworkstate (this);
}
}
3, the new Netbroadcastreceiver class inherits Broadcastreceiver and implements the OnReceive method when the OnReceive method notifies the interface to complete loading
Package com.javen.receiver;
Import java.util.ArrayList;
Import com.javen.application.Application;
Import Com.javen.netbroadcast.NetUtil;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
public class Netbroadcastreceiver extends Broadcastreceiver {public
static arraylist<neteventhandler> Mlisteners = new arraylist<neteventhandler> ();
private static String net_change_action = "Android.net.conn.CONNECTIVITY_CHANGE";
@Override public
void OnReceive (context context, Intent Intent) {
if (intent.getaction (). Equals (Net_change_ ACTION) {
application.mnetworkstate = netutil.getnetworkstate (context);
if (mlisteners.size () > 0)//notification interface completes loading for
(Neteventhandler handler:mlisteners) {
handler.onnetchange (); c17/>}}} public
Static abstract interface Neteventhandler {public
abstract void Onnetchange (); c22/>}
}
4, in the activity should implement Neteventhandler interface
Package com.javen.netbroadcast;
Import Com.javen.receiver.NetBroadcastReceiver;
Import Com.javen.receiver.NetBroadcastReceiver.netEventHandler;
Import com.javen.ui.base.BaseActivity;
Import Android.os.Bundle;
Import Android.view.Menu;
public class Mainactivity extends baseactivity implements neteventhandler{
@Override
protected void OnCreate ( Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
NetBroadcastReceiver.mListeners.add (this);
}
@Override Public
Boolean oncreateoptionsmenu (Menu menu) {
//inflate the menu; This adds items to the action Bar If it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
@Override public
void Onnetchange () {
//TODO auto-generated method stub
if Netutil.getnetworkstate ( this) = = = Netutil.networn_none) {
T.showlong (this, r.string.net_err);
} else {
T.showlong (this, "network available to use");}}
5, Judge the Network tool class
Package com.javen.netbroadcast;
Import Android.content.Context;
Import Android.net.ConnectivityManager;
Import android.net.NetworkInfo.State;
public class Netutil {public
static final int networn_none = 0;
public static final int networn_wifi = 1;
public static final int networn_mobile = 2;
public static int Getnetworkstate (context context) {
Connectivitymanager Connmanager = (Connectivitymanager) Context
. Getsystemservice (context.connectivity_service);
Wifi state
= Connmanager.getnetworkinfo (Connectivitymanager.type_wifi)
. GetState ();
if (state = = State.connected | | state = = state.connecting) {return
networn_wifi;
}
3G state
= Connmanager.getnetworkinfo (connectivitymanager.type_mobile)
. GetState ();
if (state = = State.connected | | state = = state.connecting) {return
networn_mobile;
}
Return Networn_none
}
}
The above is a small series to introduce the Android network status of real-time monitoring code example (i), I hope to help!