In fact, the monitoring of the network of mobile phones is also more important. Sometimes we must monitor the real-time network status of this program in real time. Android will broadcast when the network is disconnected and connected. We can realize network monitoring by receiving the broadcast of the system.
1. Add permissions to access the network and obtain network status
<uses-permission android:name=".ACCESS_NETWORK_STATE"/> <uses-permission android:name="" />
2. Create a new Application class and inherit the declaration variable to save network status
package ; import ; public class Application extends { private static Application mApplication; public static int mNetWorkState; public static synchronized Application getInstance() { return mApplication; } @Override public void onCreate() { (); mApplication = this; initData(); } public void initData() { mNetWorkState = (this); } }
3. Create a new NetBroadcastReceiver class to inherit BroadcastReceiver and implement the onReceive method. The onReceive method notifies the interface to complete the loading.
package ; import ; import ; import ; import ; import ; import ; public class NetBroadcastReceiver extends BroadcastReceiver { public static ArrayList<netEventHandler> mListeners = new ArrayList<netEventHandler>(); private static String NET_CHANGE_ACTION = ".CONNECTIVITY_CHANGE"; @Override public void onReceive(Context context, Intent intent) { if (().equals(NET_CHANGE_ACTION)) { = (context); if (() > 0)// Notify the interface to complete loadingfor (netEventHandler handler : mListeners) { (); } } } public static abstract interface netEventHandler { public abstract void onNetChange(); } }
4. Implement the netEventHandler interface in the appropriate Activity
package ; import ; import ; import ; import ; import ; public class MainActivity extends BaseActivity implements netEventHandler{ @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); (this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } @Override public void onNetChange() { // TODO Auto-generated method stub if ((this) == NetUtil.NETWORN_NONE) { (this, .net_err); }else { (this, "The network can be used"); } } }
5. Tools for judging the network
package ; import ; import ; import ; 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 state = (ConnectivityManager.TYPE_WIFI) .getState(); if (state == || state == ) { return NETWORN_WIFI; } // 3G state = (ConnectivityManager.TYPE_MOBILE) .getState(); if (state == || state == ) { return NETWORN_MOBILE; } return NETWORN_NONE; } }
The above is an example of the real-time monitoring code for Android network status introduced by the editor (I). I hope it will be helpful to everyone!