is Android judged to be WiFi or 4G network code _android

Source: Internet
Author: User

This example for you to share the Android decision is WiFi or 4G network code for your reference, the specific content as follows

Package com.chengxinet.bobo.utils;
Import Android.content.Context;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;

Import Android.telephony.TelephonyManager;
 /** * Created by the Administrator on 2016/1/7. */public class Networkutils {public static Boolean isnetworkavailable (context C) {context-context = C.getapplicat
    Ioncontext (); Get all connection management objects on the phone (including the management of connections to wi-fi,net) Connectivitymanager Connectivitymanager = (Connectivitymanager)

    Context.getsystemservice (Context.connectivity_service);
    if (Connectivitymanager = = null) {return false;

      else {//Get Networkinfo object networkinfo[] Networkinfo = Connectivitymanager.getallnetworkinfo ();          if (networkinfo!= null && networkinfo.length > 0) {for (Networkinfo anetworkinfo:networkinfo) {//
SYSTEM.OUT.PRINTLN (i + "= = = State = =" + networkinfo[i].getstate ());
          SYSTEM.OUT.PRINTLN (i + "= = = Type = = =" + Networkinfo[i].gettypename ()); JudgeWhether the current network state is a connection state if (anetworkinfo.getstate () = = NetworkInfo.State.CONNECTED) {return true;
  }}} return false;
    /** * Determine if WiFi is open * @param context * @return/public static Boolean iswifienabled
    Connectivitymanager mgrconn = (connectivitymanager) context. Getsystemservice (Context.connectivity_service);
    Telephonymanager Mgrtel = (telephonymanager) context. Getsystemservice (Context.telephony_service); Return ((Mgrconn.getactivenetworkinfo ()!= null && mgrconn. Getactivenetworkinfo (). GetState () = = Networkin Fo. state.connected) | |
  Mgrtel. Getnetworktype () = = Telephonymanager.network_type_umts); /** * Determine if it is a 3G network * @param context * @return/public static Boolean is3rd (context) {Connec
    Tivitymanager cm = (connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo= Cm.getactivenetworkinfo ();
    if (networkinfo!= null && networkinfo.gettype () = = Connectivitymanager.type_mobile) {return true;
  return false; /** * Determine whether WiFi or 3g network * @param context * @return/public static Boolean Iswifi (context context) {C
    Onnectivitymanager cm = (connectivitymanager) context. Getsystemservice (Context.connectivity_service);
    Networkinfo networkinfo = Cm.getactivenetworkinfo ();
    if (networkinfo!= null && networkinfo.gettype () = = Connectivitymanager.type_wifi) {return true;
  return false;

 }
}

The above is the entire contents of this article, I hope to learn more about Android software programming help.

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.