Android program checks whether the Network is available

Source: Internet
Author: User

First, define a method to check the network status in Activity:/*** determine the network connection status ** @ return true, available; false, */private boolean isOpenNetwork () {ConnectivityManager connManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE); if (connManager. getActiveNetworkInfo ()! = Null) {return connManager. getActiveNetworkInfo (). isAvailable ();} return false;} Then/*** call the method to be performed next when the Network is available. If the network is unavailable, Set */private void initIntener () {// determine whether the Network is available if (isOpenNetwork () = true) {// if the Network is available, load the network. InitPross (); // here is how my personal program loads the network. It can be flexibly used according to your own program .} Else {AlertDialog. Builder builder = new AlertDialog. Builder (MainActivity. this); builder. setTitle ("no available network"). setMessage ("do you want to set the network? "); Builder. setPositiveButton ("yes", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Intent intent = null; try {String sdkVersion = android. OS. build. VERSION. SDK; if (Integer. valueOf (sdkVersion)> 10) {intent = new Intent (android. provider. settings. ACTION_WIRELESS_SETTINGS);} else {intent = new Intent (); ComponentName comp = new ComponentName ("com. a Ndroid. settings "," com. android. settings. wirelessSettings "); intent. setComponent (comp); intent. setAction ("android. intent. action. VIEW ");} MainActivity. this. startActivity (intent);} catch (Exception e) {// Log. w (TAG, // "open network settings failed, please check... "); e. printStackTrace ();}}}). setNegativeButton ("no", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, Int which) {dialog. cancel (); // finish (); // because the network is unavailable, the running of your program is not allowed. This is set according to your individual needs. Toast. makeText (MainActivity. this, "network exception, loading failed! ", Toast. LENGTH_SHORT ). show (); initAll (); // if there is no network and you do not need to set it manually, a static page is displayed based on your needs .}}). Show () ;}} then implement the initIntener (); Method in the initialization method of the Activity. To start the Activity, perform network detection first, and then execute other methods. @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title bar setContentView (R. layout. main); // check whether the Network is available initIntener ();} finally, you need to go to AndroidManifest. add related permissions in xml: <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE "/> <uses-permission android: name =" android. permission. INTERNET "/> if something is wrong, please give me more advice.

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.