Network Status of Android notes

Source: Internet
Author: User

First the code of the previous tool class:

Package Com.yonyou.util;import Android.content.context;import Android.net.connectivitymanager;import android.net.networkinfo;/** * Determine current network status * @author XXX * */public class Networkutil {static Networkutil networkutil;private Networkutil () {}public static networkutil Getnetworkutil () {if (Networkutil = = null) {Networkutil = new Networkutil ();} return networkutil;} /** * Determine if network status is connected * @param context * @return */public Boolean isuseble (context context) {Networkinfo info = Getnetworkinfo (c Ontext); if (info! = null) {return info.isavailable ();} return false;} /** * Check if it is wify * @param context * @return */public static Boolean Iswifi (context context) {Networkinfo info = Getnetworki NFO (context); if (info! = null) {if (info.gettype () = = Connectivitymanager.type_wifi) return true;} return false;} /** * Check if it is a mobile network * @param context * @return */public static Boolean IsMobile (context context) {Networkinfo info = Getnetwor Kinfo (context); if (info! = null) {if (info.gettype () = = Connectivitymanager.type_mobile) return True;} return false;} /** * Get Network Information * @param context * @return */private static Networkinfo Getnetworkinfo (context context) {Connectivitymanager CM = (Connectivitymanager) context.getsystemservice (context.connectivity_service); return Cm.getactivenetworkinfo () ;}}
Connectivitymanager as the name implies, network manager, for the management of mobile phone wify and data network, its Getactivenetworkinfo () method can obtain a Networkinfo object, This Networkinfo object encapsulates the current phone's network information, including network connection status, network failure information, etc., before sending or requesting information using the HTTP protocol yourselves determine whether the current network state is available, That is, if the isavailable of this Networkinfo object is True,true network, FALSE is not available. But often ignores a problem, habitually to networkinfo object to empty but unclear when is empty. Today I did a test (I am lazy, referring to an interface of the previous project): First modify the next tool class code,
/*** Determine if the network status is connected *@paramContext *@return     */     Public Booleanisuseble (Context context) {Networkinfo Info=Getnetworkinfo (context); if(Info! =NULL){//return info.isavailable ();            return true; }        return false; }

Then the main page:

 Packagecom.yonyou.activity;ImportJava.util.HashMap;ImportJava.util.Map;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.net.wifi.WifiManager;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportCOM.YONGYOU.WEI.R;ImportCom.yonyou.abstractClass.NetCallBack;ImportCom.yonyou.constantClass.CIMCUrl;ImportCom.yonyou.util.NetUtil;ImportCom.yonyou.util.NetWorkUtil;/** *  * @authorXXX **/ Public classLoginactivityextendsbaseactivity {Wifimanager mwifimanager;    EditText Et_name;    EditText Et_password; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.login);    Init (); }     Public voidinit () {Mwifimanager= (Wifimanager) loginactivity. This. Getsystemservice (Context.wifi_service); Et_name=(EditText) Findviewbyid (r.id.edt_name); Et_password=(EditText) Findviewbyid (R.id.edt_password); }     Public voidLogin (view view) {if(mwifimanager.getwifistate () = =wifimanager.wifi_state_disabled) {Et_name.settext ("Wify off State"); }Else if(mwifimanager.getwifistate () = =wifimanager.wifi_state_enabled) {Et_name.settext ("Wify Open State"); }        BooleanBoo = Networkutil.getnetworkutil (). isuseble (loginactivity. This); if(Boo) {Et_password.settext ("Networkinfo is not NULL"); }Else{Et_password.settext ("Networkinfo is null"); }    }}

The effect is as follows:

This is only a test of whether the wify is empty for networkinfo.
Mobile data effect is the same, interested in the source can be found.

Network Status of Android notes

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.