razielsarafan razielsarafan - 1 year ago 103
Android Question

Ask user to turn on Wi-Fi

I have an app which almost always needs to know the user location.

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

final AlertDialog.Builder builder = new AlertDialog.Builder(
builder.setMessage("The location service is off. Do you want to turn it on?");
builder.setPositiveButton("Enable location",
new DialogInterface.OnClickListener() {
public void onClick(
final DialogInterface dialogInterface,
final int i) {
startActivity(new Intent(
builder.setNegativeButton("Continue without location", null);

However, the GPS gives me some info that isn't always precise enough. Wi-Fi always gives me enough precission, so I want to ask the user to turn on the Wi-Fi the same way I ask them to turn on the location. I do not want to just turn it on, I want the user to be notified about it, and to manually enable it.

Is there any Intent to bring the WiFi menu to the user?

Answer Source

Following intent shows the wireless settings such as Wi-Fi, Bluetooth and Mobile networks:

startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

For the complete list of Settings: https://developer.android.com/reference/android/provider/Settings.html

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

(keep in mind startActivity is just throw and forget, if you want to capture the response of what the user did out there you could instead call startActivityForResult, probably not needed in this case)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download