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