Android Network Status Detection

Source: Internet
Author: User

 Packagecom.example.administrator.yunstore.net;ImportAndroid.app.AlertDialog;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;ImportAndroid.content.DialogInterface;Importandroid.content.Intent;ImportAndroid.net.ConnectivityManager;ImportAndroid.net.NetworkInfo;ImportCOM.EXAMPLE.ADMINISTRATOR.YUNSTORE.R;/*** Created by Administrator on 2016/10/20.*/ Public classchecknetexception{PrivateConnectivitymanager Connectivitymanager; Privatecontext Context;  Publicchecknetexception (Context context) { This. context=context; }    /**     * @returnwhether the network is connected*/     Public BooleanChecknetconn () {//Get the network link ManagerConnectivitymanager =(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); //Get network link informationNetworkinfo Activenetworkinfo =Connectivitymanager.getactivenetworkinfo (); //determine if the network is linked        if(Activenetworkinfo! =NULL) {            if(activenetworkinfo.isconnected ()) {return true; } Else {                return false; }        }        return false; }    /*** Set WiFi*/     Public voidsetnetwork () {alertdialog.builder alert=NewAlertdialog.builder (context);        Alert.seticon (R.drawable.set); Alert.settitle ("Network Alert Information"); Alert.setmessage ("Network is not available, if you continue, please set up the network first!" "); Alert.setpositivebutton ("Set up a network",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Intent Intent=NULL; /*** Determine the version of the phone system! If the API is greater than 10 is 3.0+ * Because the setting of more than 3.0 is different than the setting of 3.0, the method is called differently*/                if(Android.os.Build.VERSION.SDK_INT > 10) {Intent=NewIntent (Android.provider.Settings.ACTION_WIFI_SETTINGS); } Else{Intent=NewIntent (); ComponentName Component=NewComponentName ("Com.android.settings",                            "Com.android.settings.WirelessSettings");                    Intent.setcomponent (component); Intent.setaction ("Android.intent.action.VIEW");        } context.startactivity (Intent);        }        }); Alert.setnegativebutton ("No setting",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)        {            }        });        Alert.create ();    Alert.show (); }    /*** To determine whether the network is GPRS or WiFi * return 1:GPRS; * return to 2:wifi; * return to 3:error;*/     Public intjudgenew () {networkinfo.state GPRS=Connectivitymanager.getnetworkinfo (connectivitymanager.type_mobile). GetState (); Networkinfo.state WiFi=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if(GPRS = = NetworkInfo.State.CONNECTED | | gprs = =NetworkInfo.State.CONNECTING) {           //Toast.maketext (Context, "This is GPRS", Toast.length_short). Show ();            return1; }        if(WiFi = = NetworkInfo.State.CONNECTED | | wifi = =NetworkInfo.State.CONNECTING) {            //Toast.maketext (Context, "This is WiFi", Toast.length_short). Show ();            return2; }        return3; }}

Android Network Status Detection

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.