Get the Android device WiFi connection status

Source: Internet
Author: User

This article describes how to get the status of your Android device WiFi connection!


Add access permissions (in the Androidmanifest.xml file)

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>


Java code (mainactivity.java file)

Package Com.example.androidtest;import Android.net.connectivitymanager;import android.net.NetworkInfo.State; Import Android.os.bundle;import android.app.activity;import Android.content.context;import Android.util.Log;import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Boolean bFlag = Iswifiavailable (This.getapplicationcontext ()); if (bflag) {log.i ("Wifi state-", "Connected");} else {log.i ("Wifi state-", "Not Connected");}} /** * Determine WiFi connection Status * * @param CTX * @return */public boolean iswifiavailable (Context ctx) {Connectivitymanager conman = (Co Nnectivitymanager) Ctx.getsystemservice (Context.connectivity_service); state WiFi = Conman.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if (state.connected = = WiFi) {return true ;} else {return false;}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds Items to the Action bar if it is present.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}} 



Get the Android device WiFi connection status

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.