user960845
user960845

Reputation: 63

How can I turn Wifi on when my application runs in Android?

When I start my twitter app, how can I turn on WiFi automatically through coding?

Upvotes: 3

Views: 127

Answers (2)

Santanu Barik
Santanu Barik

Reputation: 1

  if (text.indexOf("wi-fi") != -1) {
            if (text.indexOf("on") != -1) {
                WifiManager wifi = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                if (wifi.isWifiEnabled()) {
                    Toast.makeText(MainActivity.this, "Already WiFi is Enabled", Toast.LENGTH_SHORT).show();
                    tts.speak("Already WiFi is Enabled", TextToSpeech.QUEUE_ADD, null);
                }
          else {
                wifi.setWifiEnabled(true);
                Toast.makeText(MainActivity.this,"Turning on",Toast.LENGTH_SHORT).show();
                Toast.makeText(MainActivity.this, "Turning on wifi", Toast.LENGTH_SHORT).show();
                tts.speak("Turning on wifi", TextToSpeech.QUEUE_ADD, null);
            }
        }

Upvotes: -1

Vineet Shukla
Vineet Shukla

Reputation: 24031

I have not tried this but it should work:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null)
  wifiManager.setWifiEnabled(true);

Add this permission to the manifest file:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Upvotes: 5

Related Questions