Network connection exception handling tool
Public class NetStateUtils {
/**
* Determine the network connection status
*
* @ Return true, available; false, unavailable
*/
Public static boolean isNetworkConnected (Context context ){
If (context! = Null ){
ConnectivityManager connManager = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo networkInfo = connManager. getActiveNetworkInfo ();
If (networkInfo! = Null ){
Return networkInfo. isAvailable ();
}
}
Return false;
}
/**
* Prompt for setting network connection
*
*/
Public static void alertSetNetwork (final Context context ){
AlertDialog. Builder builder = new AlertDialog. Builder (context );
Builder. setTitle (context. getResources (). getString (R. string. neterror). setMessage (context. getString (R. string. question ));
Builder. setPositiveButton (context. getResources (). getString (R. string. set), new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Intent intent = null;
Try {
Int sdkVersion = android. OS. Build. VERSION. SDK_INT;
If (sdkVersion> 10 ){
Intent = new Intent (
Android. provider. Settings. ACTION_WIRELESS_SETTINGS );
} Else {
Intent = new Intent ();
ComponentName comp = new ComponentName (
"Com. android. settings ",
"Com. android. settings. WirelessSettings ");
Intent. setComponent (comp );
Intent. setAction ("android. intent. action. VIEW ");
}
Context. startActivity (intent );
} Catch (Exception e ){
E. printStackTrace ();
}
}
});
Builder. setNegativeButton (context. getString (R. string. cancle), new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Dialog. cancel ();
System. exit (0 );
System. gc ();
}
});
Builder. show ();
}
}