android--Determine network status

Source: Internet
Author: User
Tags try catch

Usually we play QQ when we do not have Internet, QQ above back to show "network connection does not give force, please check the network connection" then how does it determine whether the network is connected?
Here's a case that shows how to check the status of the network connection
Because we're just testing it out here so our project is just a simple implementation click a button to determine if the device network is connected if the network is connected, a message box prompts "network unobstructed" instead prompts "is your network really connected?"


1. Create a new Android project Textnet (device version 4.4.2)
2. Open Activity_main.xml Add a button
3. Open Mainactivity.java Add a Click event to the button with the anonymous inner class
Note: Anonymous internal classes are not recommended if too many events can be messy, do the same package to build a class to implement convenient modification

The project code is as follows:

Package Com.example.internet;import Android.app.activity;import android.content.pm.packagemanager;import Android.net.connectivitymanager;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import android.widget.Toast; Public classMainactivity extends Activity {PrivateConnectivitymanager con; PrivateBoolean WiFi; PrivateBoolean Internet; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //Get handleButton btn1 = (button) This. Findviewbyid (R.id.button1); //Anonymous inner class implements click eventsBtn1.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {//We recommend that you do not use anonymous inner classes.//The try catch is added here to prevent the user experience from being affected by the device not giving permission to the second stop operation.                Try {                    //Monitor network connection Statuscon=(Connectivitymanager) Getsystemservice (Activity.connectivity_service); //Getnetworkinfo getting information for the specified network connection typeWifi=Con.getnetworkinfo (Connectivitymanager.type_wifi). isconnectedorconnecting (); Internet=Con.getnetworkinfo (connectivitymanager.type_mobile). isconnectedorconnecting (); } Catch(Exception e) {e.printstacktrace (); } packagemanager pm=Getpackagemanager (); //determine if you have permissionBoolean Permission = (packagemanager.permission_granted = =Pm.checkpermission ("Android.permission.ACCESS_NETWORK_STATE","com.example.internet")); if(permission) {if(WiFi |Internet) {                          //Toast Frame DisplayToast.maketext (mainactivity. This,                                  "Network unobstructed", Toast.length_long). Show (); }Else{toast.maketext (mainactivity). This,                                   "Pro, is the network connected? ", Toast.length_long). Show (); }                                     }Else {                      //prompt If you don't have permissionToast.maketext (mainactivity. This,                               "Please open the relevant network state permissions, easy to use", Toast.length_long). Show ();    }              }                    }); }

Note: the permission list method

  

Try {              = Pm.getpackageinfo ("packagename", packagemanager.get_permissions); </span>              = pack.requestedpermissions;              Showtoast (" rights List--->" + permissionstrings.tostring ());           Catch (namenotfoundexception e) {              e.printstacktrace ();          }  

A permission form is reproduced here:

    1. Http://www.cnblogs.com/classic/archive/2011/06/20/2085055.html

  

android--Determine network status

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.