سؤال

I have a problem that I want to check the user who starts the WIFI or turns into AP in Android OS.
How should I check these services whether they are activated or not?
Please help me to solve this problem.
Thank you for your help:)

هل كانت مفيدة؟

المحلول

To check the Wifi state use this code

private WifiManager wifiManager;
@Override
public void onCreate(Bundle icicle) {
    ....................
     wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    if(wifiManager.isWifiEnabled()){
         // do whatever you wnat
         wifiManager.setWifiEnabled(false);
    }else{
       // do whatever you wnat
       wifiManager.setWifiEnabled(true);
    }
}

To perform this you have to declare the bellow permissions in the manifest.xml file.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top