How to call Wi-Fi settings screen from my application using Android

Viewed 109482

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.

9 Answers

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));

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));
}

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);

on button click click Listner

startActivityForResult(new Intent(Settings.ACTION_WIFI_SETTINGS), 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));
            }

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());
    }
Related