Android is a reliable way to determine whether WiFi is available, whether Android is networked

Source: Internet
Author: User

http://alex-yang-xiansoftware-com.iteye.com/blog/619841

In some programs, you need to download data from the Internet, or other ways to generate traffic to the network, when WiFi is not available should be prompted to the user WiFi is not available, whether to continue, because if the WiFi dropped, then the program may use 3G card or other toll channels using the network, will lead to a lot of internet costs when you don't know. By looking at the Android API, you can use the following methods to determine:

    1. Public Static boolean iswifiactive (Context incontext) {
    2. Context context = Incontext.getapplicationcontext ();
    3. Wifimanager Wifimanager = (wifimanager) context
    4. . Getsystemservice (Context.wifi_service);
    5. return wifimanager.iswifienabled ();
    6. }

When using this method on the simulator, it is possible to correctly determine whether WiFi is available, but it cannot be judged on the real machine. WiFi is disconnected, but the returned result is true, resulting in inaccurate wifi judgment. The following methods can be used to determine the correct:

  1. Public Static boolean iswifiactive (Context incontext) {
  2. Context context = Incontext.getapplicationcontext ();
  3. Connectivitymanager connectivity = (Connectivitymanager) context
  4. . Getsystemservice (Context.connectivity_service);
  5. if (connectivity! = null) {
  6. Networkinfo[] info = Connectivity.getallnetworkinfo ();
  7. if (info! = null) {
  8. For (int i = 0; i < info.length; i++) {
  9. if (Info[i].gettypename (). Equals ("WIFI") && info[i].isconnected ()) {
  10. return true;
  11. }
  12. }
  13. }
  14. }
  15. return false;
  16. }

Android is a reliable way to determine whether WiFi is available, whether Android is networked

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.