The note is used to get the status of the network in real-time, a bit like the QQ off-screen reminders, in order to use in the future work, here to write the code about this judgment, the following is written some code:
Package com.example.nettest;import receiver. netreceiver;import android.app.activity;import android.content.intentfilter;import Android.net.connectivitymanager;import android.os.bundle;public class mainactivity extends activity { netreceiver mreceiver = new netreceiver (); intentfilter mfilter = new intentfilter (); @Override public void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (R.layout.activity_main); mfilter.addaction (connectivitymanager.connectivity_action); registerreceiver (mreceiver, mfilter); &nbsP;&NBSP;&NBSP;&NBSP,} @Overrideprotected void ondestroy () {unregisterreceiver (mreceiver); Super.ondestroy ();}} Package receiver;import network. netutils;import android.content.broadcastreceiver;import android.content.context;import Android.content.intent;import android.net.connectivitymanager;import android.widget.toast;public class netreceiver extends broadcastreceiver {@Overridepublic void OnReceive (context context, intent intent) {string action = intent.getaction ();if (ConnectivityManager.CONNECTIVITY_ACTION.equals (ACTION)) {boolean isconnected = netutils.isnetworkconnected (context); system.out.println ( "Network status:" + isconnected); system.out.println ("WiFi Status:" + netutils.iswificonnected (context)); &NBSP;SYSTEM.OUT.PRINTLN ("Mobile network Status:" + netutils.ismobileconnected (context)); &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Network connection type:" + netutils.getconnectedtype (context)); if (isconnected) { toast.maketext (context, "Already connected to the network", toast.length_long). Show (); } else { toast.maketext ( context, "Disconnected network", toast.length_long). Show (); }}} Package network;import android.content.context;import android.net.connectivitymanager;import android.net.NetworkInfo;public class NetUtils {// Determine network connection status public static Boolean isnetworkconnected (Context context) {if (context != null) { Connectivitymanager mconnectivitymanager = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mnetworkinfo = mconnectivitymanager.getactivenetworkinfo ();if (mNetworkInfo != null) {return mnetworkinfo.isavailable ();}} Return false;} Determine WiFi status public static boolean iswificonnected (context context) {if ( Context != null) {ConnectivityManager mConnectivityManager = ( Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mwifinetworkinfo = mconnectivitymanager.getnetworkinfo (ConnectivityManager.TYPE_ WIFI);if (mwifinetworkinfo != null) {return mwifinetworkinfo.isavailable ();}} Return false;} Judge Mobile network public static boolean ismobileconnected (Context context) {if ( Context != null) {ConnectivityManager mConnectivityManager = (ConnecTivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mmobilenetworkinfo = mconnectivitymanager.getnetworkinfo (ConnectivityManager.TYPE_ MOBILE);if (mmobilenetworkinfo != null) {return mmobilenetworkinfo.isavailable ();}} Return false;} Get connection Type Public static int getconnectedtype (context context) {if (Context != null) {ConnectivityManager mConnectivityManager = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mnetworkinfo = mconnectivitymanager.getactivenetworkinfo ();if (mNetworkInfo != null && mnetworkinfo.isavailable ()) {return mnetworkinfo.gettype ();}} Return -1;}} Finally, two important permissions were added to the manifest file: <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>< Uses-permission android:name= "Android.permission.INTERNET"/> Summary under the basicThere are a few points to note: 1, first to obtain the corresponding manager Context.getsystemservice (context.connectivity_service) return ConnectivityManager2, Get the corresponding network information status: Networkinfo mnetworkinfo = mconnectivitymanager.getactivenetworkinfo (); Networkinfo mwifinetworkinfo = mconnectivitymanager.getnetworkinfo (ConnectivityManager.TYPE_ WIFI); Networkinfo mmobilenetworkinfo = mconnectivitymanager.getnetworkinfo (ConnectivityManager.TYPE_ MOBILE); 3, determine whether the information is valid workinfo.isavailable () 4, after the broadcast is registered, as long as the wireless signal has been sent to change, will send the broadcast then we make the corresponding judgment on it can be
This article is from "Wish You Happiness" blog, please be sure to keep this source http://zhunixingfu.blog.51cto.com/7430537/1635766
Android Monitor network status