2011-09-30 65 views

回答

5

我還沒有試過,但它應該工作:

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

此權限添加到清單文件:

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

我要寫的地方.. – user960845

+0

請幫我.. – user960845

+0

什麼是_wifiLock ..我們必須使用它 – user960845

-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); 
      } 
     } 
+0

請添加更多關於你的答案的信息。 –

0

如果(text.indexOf( 「關」)! = -1){WifiManager wifi =(WifiManager)this.getApplicationContext()。getSystemService(Context.WIFI_SERVICE);如果(!wifi.isWifiEnabled()){Toast.makeText(MainActivity.this,「Already WiFi is Disabled」,Toast.LENGTH_SHORT).show(); tts.speak(「已禁用WiFi」,TextToSpeech.QUEUE_ADD,null); } else {wifi.setWifiEnabled(false); Toast.makeText(MainActivity.this,「關閉wifi」Toast.LENGTH_SHORT).show(); tts.speak(「關閉wifi」,TextToSpeech.QUEUE_ADD,null); }}

+0

請格式化代碼並解釋它如何解決問題。 – MikaS

相關問題