Android Network judgment

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 public    void OnReceive (context context, Intent Intent) {        String action = Intent.getaction ();        if (Action.equals (connectivitymanager.connectivity_action)) {            Toast.maketext (context, "network changed", Toast.length_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.& Nbsp;*/public class Networkutils {   /**     * Determine if there is an Internet connection      * @param context&nbs P    * @return      */    public static Boolean isnetworkconnected (context context) {&nbs P       if (context! = null) {            Connectivitymanager Mconnectivitymana GER = (Connectivitymanager) context                    Getsystemservice (context.connectivity_service);            Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();            if (mnetworkinfo! = null) {  & nbsp             return mNeTworkinfo.isavailable ();           }       }      &NBSP ; Return false;   }   /**     * determine if WiFi network is available      * @param context     * @return      */    public static Boolean iswificonnected (context context) {  & nbsp     if (context! = null) {            Connectivitymanager Mconnectivitymanager = ( Connectivitymanager) context                    Getsystemservice (Conte Xt. Connectivity_service);            Networkinfo mwifinetworkinfo = Mconnectivitymanager                     Getnetworkinfo (Connectivitymanager.type_wifi), & nbsp           if (mwifinetworkinfo! = null) {                Return mwifinetworkInfo.isavailable ();           }       }        RET Urn false;   }   /**     * determine if mobile network is available      * @param context  & nbsp  * @return      */    public static Boolean ismobileconnected (context context) {  &nbsp ;     if (context! = null) {            Connectivitymanager Mconnectivitymanager = ( Connectivitymanager) context                    Getsystemservice (Conte Xt. Connectivity_service);            Networkinfo mmobilenetworkinfo = mconnectivitymanager& nbsp                   Getnetworkinfo (connectivitymanager.type_mobile);  & nbsp         if (mmobilenetworkinfo! = null) {                Retur N MmobileNetworkinfo.isavailable ();           }       }      &NB Sp  Return false;   }   /**     * Get the type information for the current network connection      * @param context     * @return      */    public static int Getconnectedtype (context context) {  &NB Sp     if (context! = null) {            Connectivitymanager Mconnectivitymanager = ( Connectivitymanager) context                    Getsystemservice (Conte Xt. Connectivity_service);            Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();            if (mnetworkinfo! = null & & Mnetworkinfo.isavailable ()) {                return Mnetworkinfo.gettype ();            }  &NBsp    }        return-1;   }   /**     * get current network status: No network 0:wifi Network 1:3g Network 2:2g network 3     *     * @param context     * @return      */    public static int Getapntype (context context) {        int netType = 0;  &N Bsp     Connectivitymanager connmgr = (connectivitymanager) context                Getsystemservice (Context.connectivity_service);        Networkinfo networkinfo = Connmgr.getactivenetworkinfo ();        if (networkinfo = = null) {          &N Bsp Return nettype;       }        int nType = Networkinfo.gettype ();    &N Bsp   if (NType = = Connectivitymanager.type_wifi) {            NetType = 1;//wifi  &n Bsp     } else if (NType = = connectivitymanager.type_mobile) {            int nsubtype = Networkinf O.getsubtype ();            Telephonymanager mtelephony = (telephonymanager) context  & nbsp                 Getsystemservice (context.telephony_service);            if (Nsubtype = = telephonymanager.network_type_umts            &N Bsp       &&!mtelephony.isnetworkroaming ()) {                N Ettype = 2;//3g           } else {                NetType = 3;//2g           }       }        retur n 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>



Android Network judgment

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.