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

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.

110923 次浏览

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

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

startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

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

on button click click Listner

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

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

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

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