Code sharing _android to determine if the network is available in Android

Source: Internet
Author: User
 package cn.hackcoder.beautyreader.broadcast;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.net.ConnectivityManager;
Import Android.util.Log;

Import Android.widget.Toast;
Import cn.hackcoder.beautyreader.activity.base.BaseActivity;

Import Cn.hackcoder.beautyreader.utils.NetWorkUtils; public class Networkstatusreceiver extends Broadcastreceiver {public networkstatusreceiver () {} @Override Pub
    LIC void OnReceive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals (connectivitymanager.connectivity_action)) {Toast.maketext (context, "network changed", Toast.lengt
      H_long). Show ();
    baseactivity.isnetworkconnected = Networkutils.getapntype (context) >0; }
  }
}
 Package cn.hackcoder.beautyreader.utils;
Import Android.content.Context;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;


Import Android.telephony.TelephonyManager;
/**  * Created by Hackcoder on 15-1-25.  */public class Networkutils {   /**      * Determine if there is a network connection      * @param cont Ext      * @return      */    public static Boolean isnetworkconnected (Context Co ntext) {        if (context!= null) {            Connectivitymanager  Mconnectivitymanager = (Connectivitymanager) context                    
. Getsystemservice (Context.connectivity_service);
            Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();             if (mnetworkinfo!= null) {          &NBSP
    return mnetworkinfo.isavailable ();
            {       }         return false;    }    /**      * determine if WiFi is available      * @param context      * @return      */    public static Boolean iswificonnected (context context) {  &NBSP ;     IF (context!= null) {          Connectivitymanager   Mconnectivitymanager = (Connectivitymanager) Context                     Getsystemservice (Cont Ext.
Connectivity_service);             Networkinfo mwifinetworkinfo = mconnectivitymanager        
           . Getnetworkinfo (Connectivitymanager.type_wifi);             if (mwifinetworkinfo!= null) {        &nbsp
      return mwifinetworkinfo.isavailable ();
            {       }         return false;    }    /**      * determine if mobile network is available      * @param context   &NBSP ;  * @return      */    public static Boolean ismobileconnected (context context) {  &NB Sp     IF (context!= null) {          Connectivitymanager   Mconnectivitymanager = (Connectivitymanager) Context                     Getsystemservice (Cont Ext.
Connectivity_service);             Networkinfo mmobilenetworkinfo = mconnectivitymanager       &NBSP ;
           . Getnetworkinfo (Connectivitymanager.type_mobile);             if (mmobilenetworkinfo!= null) { 
              return mmobilenetworkinfo.isavailable ();
            {       }         return false;     }    /**      * get current network connection type information      * @param context      * @return      */    public static int Getconnectedtype (context context) {        IF (context!= null) {          Connectivitymanager   Mconnectivitymanager = (Connectivitymanager) Context                     Getsystemservice (Cont Ext.
Connectivity_service);
            Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();             if (mnetworkinfo!= null && mnetworkinfo.isavailable ()) {                return Mnetworkinfo.gettype ();
                    {        return-1;    }    /**      * get current network status: No network 0:wifi network 1:3g Network 2:2g Network 3      * &nbsp ;    * @param      * @return      */    public static int GETAPNT Ype (Context context) {        int nettype = 0;         Connectivitymanager CONNMG R = (Connectivitymanager) context                 Getsystemservice (Context.connec
Tivity_service);
        Networkinfo networkinfo = Connmgr.getactivenetworkinfo ();         if (Networkinfo = null) {            return NetType   &N Bsp
   }         int ntype = Networkinfo.gettype ();         if (ntype = ConnectivitymanAger.  Type_wifi) {            NetType = 1;//WIFI        } else if (ntype = = Connectivitymanager.type_mobile) {            int nsubtype = Networkinfo.getsubtype (); &nb Sp           Telephonymanager mtelephony = (telephonymanager) context         &N Bsp
         . Getsystemservice (Context.telephony_service);             if (Nsubtype = telephonymanager.network_type_umts         & nbsp           &&!mtelephony.isnetworkroaming ()) {                NetType = 2;//3G            } else {          &NBS P     NetType = 3;//2G                    }     &N Bsp
  return nettype;    }
} 

Registered:

   <receiver
      android:name= ". Broadcast. Networkstatusreceiver "
      android:enabled=" true "
      android:exported=" true ">
      <intent-filter>
        <action android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/>
      </intent-filter>
    </ Receiver>

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.