Network connection exception handling tool

Source: Internet
Author: User

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 ();
}
}

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.