I want to enable/disable wifi from my Android application. How can I do that?
我想从我的Android应用程序启用/禁用wifi。我怎样才能做到这一点?
5 个解决方案
#1
43
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false); // true or false to activate/deactivate wifi
You also need to request the permission in your AndroidManifest.xml :
您还需要在AndroidManifest.xml中请求权限:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
#2
13
To enable/disable WiFi in your application you need to use WiFiManager class. Create an Object of WiFiManager class to get the services of WiFi.
要在应用程序中启用/禁用WiFi,您需要使用WiFiManager类。创建WiFiManager类的对象以获得WiFi的服务。
WifiManager wifi;
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);//Turn off Wifi
wifi.setWifiEnabled(true);//Turn on Wifi
And you have to put the following permissions in AndroidManifest.xml
您必须在AndroidManifest.xml中添加以下权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
To get the whole sample code of enable/disable Wifi in android with UI visit this website
要获取在UI中启用/禁用Wifi的完整示例代码,请访问此网站
#3
3
try this code
试试这段代码
Intent gpsOptionsIntent = new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivityForResult(gpsOptionsIntent,0);
#4
1
try this
尝试这个
public void disableWifi(Context context, Boolean bool) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(bool)
wifi.setWifiEnabled(false);
else
wifi.setWifiEnabled(true);
}
#5
0
public class MainActivity extends AppCompatActivity {
Switch btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Switch) findViewById(R.id.switch1);
btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleWiFi(true);
Toast.makeText(getApplicationContext(), "Wi-Fi Enabled!", Toast.LENGTH_LONG).show();
} else {
toggleWiFi(false);
Toast.makeText(getApplicationContext(), "Wi-Fi Disabled!", Toast.LENGTH_LONG).show();
}
}
});
}
public void toggleWiFi(boolean status){
WifiManager wifiManager = (WifiManager)this.getSystemService(WIFI_SERVICE);
if (status && !wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
} else if (!status && wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
}
}
Add User Permission in Manifest Files
在清单文件中添加用户权限
#1
43
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false); // true or false to activate/deactivate wifi
You also need to request the permission in your AndroidManifest.xml :
您还需要在AndroidManifest.xml中请求权限:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
#2
13
To enable/disable WiFi in your application you need to use WiFiManager class. Create an Object of WiFiManager class to get the services of WiFi.
要在应用程序中启用/禁用WiFi,您需要使用WiFiManager类。创建WiFiManager类的对象以获得WiFi的服务。
WifiManager wifi;
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);//Turn off Wifi
wifi.setWifiEnabled(true);//Turn on Wifi
And you have to put the following permissions in AndroidManifest.xml
您必须在AndroidManifest.xml中添加以下权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
To get the whole sample code of enable/disable Wifi in android with UI visit this website
要获取在UI中启用/禁用Wifi的完整示例代码,请访问此网站
#3
3
try this code
试试这段代码
Intent gpsOptionsIntent = new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivityForResult(gpsOptionsIntent,0);
#4
1
try this
尝试这个
public void disableWifi(Context context, Boolean bool) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(bool)
wifi.setWifiEnabled(false);
else
wifi.setWifiEnabled(true);
}
#5
0
public class MainActivity extends AppCompatActivity {
Switch btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Switch) findViewById(R.id.switch1);
btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleWiFi(true);
Toast.makeText(getApplicationContext(), "Wi-Fi Enabled!", Toast.LENGTH_LONG).show();
} else {
toggleWiFi(false);
Toast.makeText(getApplicationContext(), "Wi-Fi Disabled!", Toast.LENGTH_LONG).show();
}
}
});
}
public void toggleWiFi(boolean status){
WifiManager wifiManager = (WifiManager)this.getSystemService(WIFI_SERVICE);
if (status && !wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
} else if (!status && wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
}
}
Add User Permission in Manifest Files
在清单文件中添加用户权限