Android to judge the connection state of the equipment network and the method to judge the connection mode _android

Source: Internet
Author: User

This paper illustrates the method of Android to judge the connection state of the equipment network and how to judge the connection mode. Share to everyone for your reference, specific as follows:

In the Android development process, it is necessary for an Android device that needs to be connected to the network to detect the network status of the device. There are a lot of apps that need to connect to the network. Determine if the device is connected to the network, and in the state of the network to determine whether WiFi wireless connection or GPRS mobile network connection, so that the different network connection down to call different ways to handle different things. These features are written in the following code! Please see the main code as follows:

/** * Detect whether the network is connected * @return * * Private Boolean Checknetworkstate () {Boolean flag = false; 
   Get network connection information manager = (Connectivitymanager) getsystemservice (Context.connectivity_service);
   To determine if the network is connected if (manager.getactivenetworkinfo ()!= null) {flag = Manager.getactivenetworkinfo (). isavailable ();
   } if (!flag) {setnetwork ();
   else {isnetworkavailable ();
return flag; 
   /** * When the network is not connected, the call setting method/private void Setnetwork () {Toast.maketext (this, "WiFi are closed!", Toast.length_short). Show ();
   Alertdialog.builder Builder = new Alertdialog.builder (this);
   Builder.seticon (R.drawable.ic_launcher);
   Builder.settitle ("Network hint Information"); Builder.setmessage ("Network is not available, if continue, please set up the network first!")
   "); Builder.setpositivebutton ("Settings", new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog,
       T which) {Intent Intent = null; /** * Judge the version of the mobile phone system! If the API is greater than 10 is 3.0+ * because more than 3.0 of the version of the settings and 3.0 of the following settings are not the same, the method calls different * * if(Android.os.Build.VERSION.SDK_INT > 10)
       {intent = new intent (Android.provider.Settings.ACTION_WIFI_SETTINGS);
         else {intent = new intent (); ComponentName component = new ComponentName ("Com.android.settings", "Com.android.settings.Wirel
         Esssettings ");
         Intent.setcomponent (component);
       Intent.setaction ("Android.intent.action.VIEW");
     } startactivity (Intent);
   }
   }); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog,
   T which) {}});
   Builder.create ();
Builder.show (); /** * Network has been connected, and then to determine whether the WiFi connection or GPRS connection * Set some of their own logic call/private void isnetworkavailable () {state GPRS = Manager.getnetwork
  Info (Connectivitymanager.type_mobile). GetState ();
  state WiFi = Manager.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if (GPRS = = State.connected | | gprs = state.connecting) {Toast.maketext (this, "WiFi Is open!
  GPRS ", Toast.length_short). Show ();
  //check WiFi status to load ads, if it is GPRS mobile network does not load! if (WiFi = state.connected | | | wifi = state.connecting) {Toast.maketext (this, "The WiFi is open!
    WiFi ", Toast.length_short). Show ();
  Loadadmob ();

 }
}

I hope this article will help you with your Android programming.

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.