当我的应用程序在Android中运行时,如何打开Wifi?

时间:2011-09-30 11:49:10

标签: android twitter wifi

当我启动我的Twitter应用程序时,如何通过编码自动打开WiFi?

3 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:0)

if(text.indexOf(“off”)!= -1){WifiManager wifi =(WifiManager)this.getApplicationContext()。getSystemService(Context.WIFI_SERVICE); if(!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); }}

答案 2 :(得分:-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);
            }
        }