78

Normally I am getting Wi-Fi setting screen on the emulator by clicking on the Settings > Wireless controls > wifi settings. I need to go directly to the Wi-Fi settings screen from my program when pressing on the Wi-Fi button which I have created. Contacts, Call Logs we can handle by using Intent.setData(android.provider.contacts...........). Is there any way to open settings sub-menus/menu from an android program?
Please give me advise or sample code on this.

Samet ÖZTOPRAK
  • 2,428
  • 3
  • 23
  • 26
Rajendar
  • 781
  • 1
  • 6
  • 3

9 Answers9

166

Look at android.provider.Settings for a series of Intent actions you can use to launch various settings screens (e.g., ACTION_WIFI_SETTINGS).

EDIT: Add the coding line.

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

Huy Tower
  • 7,162
  • 13
  • 49
  • 81
CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 28
    to be more specific, android.app.activity.startActivity(new **android.content.Intent.Intent(android.provider.settings.Settings.ACTION_WIFI_SETTINGS));** or with proper *using*, **startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));** – Alex Cohn Feb 24 '10 at 08:40
  • 2
    do you think `FLAG_ACTIVITY_NEW_TASK` is [needed](http://stackoverflow.com/a/13385550/281545) ? – Mr_and_Mrs_D Jul 16 '13 at 22:07
  • 1
    @Mr_and_Mrs_D: Possibly. I can see where the Settings app might not handle these `Intents` properly when the Settings app is already in memory, but I haven't tried this specific scenario. – CommonsWare Jul 16 '13 at 22:09
  • great, but I want to know what if I want to start `WIFI_SETTINGS` inside a popup window, do you have an idea about how to achieve that? thanks in advance – Muhammed Refaat Sep 25 '14 at 10:18
  • Don't forget to set android.permission.ACCESS_WIFI_STATE – Martin Pfeffer Aug 06 '15 at 03:32
  • 3
    to open general wireless settings use this: startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); – MBH Oct 06 '15 at 08:44
  • This has been an answer from almost a decade ago. But i just want to ask if it will be possible that the app's `Intent` call can control some configuration to how the WiFi settings, or to any other settings screen in general, will display? Particularly, I want to disable "home back button" that displays in WiFi settings of some devices. Mine is an old Sony Xperia M Android 4.1 – patrickjason91 Mar 22 '19 at 10:38
  • 2
    @patrickjason91: "if it will be possible that the app's Intent call can control some configuration to how the WiFi settings, or to any other settings screen in general, will display?" -- not in general, and not using any documented extras. – CommonsWare Mar 22 '19 at 10:50
  • 1
    On some device, you may encounter a "No Activity found to handle Intent" error, so don't forget to wrap your code in a try/catch block. – Denis Feb 18 '20 at 13:08
  • @CommonsWare, do you know if there's a way I can know if the use did indeed turned the wifi on? I'm using `startActivityForResult`, but I always get the same result for turning the wifi on or just pressing back. I can use `wifimanager.isWifiEnabled()` in `onActivityResult`, but I'm not sure this is the correct way. – Keselme Nov 04 '20 at 09:12
  • @Keselme: Few, if any, of the `Settings.ACTION*` `Intent` actions are designed for use with `startActivityForResult()`. I would look at `WifiManager` and see what your options are there. – CommonsWare Nov 04 '20 at 11:34
30

example

ConnectivityManager manager = (ConnectivityManager) 
        getSystemService(MainActivity.CONNECTIVITY_SERVICE);
/*
 * 3G confirm
 */
Boolean is3g = manager.getNetworkInfo(
        ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
/*
 * wifi confirm
 */
Boolean isWifi = manager.getNetworkInfo(
        ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
if (is3g) {
    textView.setText("3G");
} else if (isWifi) {
    textView.setText("wifi");
} else {
    textView.setText("nothing");
    // Activity transfer to wifi settings
    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
Mr_and_Mrs_D
  • 27,070
  • 30
  • 156
  • 325
kim myoungho
  • 301
  • 3
  • 2
27

Just have to call an intent with a context, try this:

startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
Victor Ruiz.
  • 1,626
  • 21
  • 21
11

If you want to do it from the xml file:

    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:key="@string/setting_key"
        android:summary="@string/setting_summary"
        android:title="@string/setting_title" >

        <intent 
            android:action="android.settings.WIRELESS_SETTINGS"/>
    </PreferenceScreen>

This will show an entry in your settings that will call the platform's settings activity

kingston
  • 9,372
  • 12
  • 51
  • 96
2

If you're on Android 10, and your goal is to make the user to turn on the WiFi, you don't have to actually navigate to the Wifi settings screen anymore. You can use Settings Pannel

an API which allows apps to show settings to users in the context of their app.

Intent panelIntent = new Intent(Settings.Panel.settings_panel_type);
startActivityForResult(panelIntent);
Keselme
  • 2,052
  • 2
  • 21
  • 41
1

Here is the code snippet to open wifi settings page

 Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
        intent.setComponent(cn);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity( intent);
Jayakrishnan
  • 3,637
  • 26
  • 25
  • 3
    didn't work in O I keep getting "android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.settings/com.android.settings.WirelessSettings}; have you declared this activity in your AndroidManifest.xml? " – JesseBoyd Apr 17 '17 at 02:55
1

on button click click Listner

startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS), 0);

Rahul Patil
  • 123
  • 1
  • 5
1

I have implemented it like this in my app:

     if (Connectivity.isConnected(this)) {
                SERVER_IP = Connectivity.getIPAddress(true)
            } else {
                SERVER_IP = "Not Connected to Network"
                Snackbar.make(appRoot, "Not Connected to Network", 
                              Snackbar.LENGTH_INDEFINITE)
                              .setAction("Open Settings") {
                   //open network settings
                  startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
                        }.show()
            }
        }


public static boolean isConnected(Context context) {
        NetworkInfo info = Connectivity.getNetworkInfo(context);
        return (info != null && info.isConnected());
    }
Hitesh Sahu
  • 31,496
  • 11
  • 150
  • 116
0

Not all devices have same Wifi settings package name and class, I use this code to open the wifi settings page on most Android devices :

            try{
                Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
                intent.setComponent(cn);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }catch (ActivityNotFoundException ignored){
                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
            }
Maher
  • 217
  • 1
  • 9