Ask user to turn on Wi-Fi

I have an application that almost always needs to know the location of the user.

When I need to access a location, I do this:

final AlertDialog.Builder builder = new AlertDialog.Builder( MapScreen.this); builder.setTitle("MyAppName"); builder.setMessage("The location service is off. Do you want to turn it on?"); builder.setPositiveButton("Enable location", new DialogInterface.OnClickListener() { @Override public void onClick( final DialogInterface dialogInterface, final int i) { startActivity(new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }); builder.setNegativeButton("Continue without location", null); builder.create().show(); 

However, GPS gives me some information that is not always accurate enough. Wi-Fi always gives me enough refueling, so I want to ask the user to turn on Wi-Fi in the same way as I ask to turn it on. I donโ€™t want to just turn it on, I want the user to be notified about it and manually turn it on.

Are there any intentions to bring the WiFi menu to the user?

+6
source share
3 answers

The following intention shows wireless settings such as Wi-Fi, Bluetooth, and mobile networks:

 startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); 

Full list of settings: https://developer.android.com/reference/android/provider/Settings.html

For documentation on the startActivity method: https://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)

(keep in mind that startActivity just throws and forgets, if you want to capture the response of what the user did there, you could instead call startActivityForResult , maybe you don't need it in this case)

+11
source

You can try

 Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS); startActivity(i); 
+3
source

The previous answers related to "Settings.ACTION_WIRELESS_SETTINGS", but it is too general as it will open the settings for the general wireless connection, while the question only concerns Wi-Fi. I use "Settings.ACTION_WIFI_SETTINGS" as follows

 Intent turnWifiOn = new Intent(Settings.ACTION_WIFI_SETTINGS); startActivity(turnWifiOn); 

Note. It will not open only the dialog box, but instead.

+2
source

Source: https://habr.com/ru/post/944957/


All Articles