如何使用 Android 从我的应用程序调用 Wi-Fi 设置屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2318310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:23:31  来源:igfitidea点击:

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

androidwifi

提问by Rajendar

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.

通常我会通过单击Settings > Wireless controls > wifi settings. 当按下我创建的 Wi-Fi 按钮时,我需要从我的程序直接进入 Wi-Fi 设置屏幕。我们可以使用 Intent.setData(android.provider.contacts...........) 处理联系人、通话记录。有没有办法从android程序打开设置子菜单/菜单?
请给我建议或示例代码。

回答by CommonsWare

Look at android.provider.Settingsfor a series of Intentactions you can use to launch various settings screens (e.g., ACTION_WIFI_SETTINGS).

查看可用于启动各种设置屏幕android.provider.Settings的一系列Intent操作(例如,ACTION_WIFI_SETTINGS)。

EDIT:Add the coding line.

编辑:添加编码行。

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

回答by kim myoungho

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

回答by Victor Ruiz.

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

只需要调用一个带有上下文的意图,试试这个:

startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

回答by kingston

If you want to do it from the xml file:

如果要从 xml 文件执行此操作:

    <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

这将在您的设置中显示一个条目,该条目将调用平台的设置活动

回答by Jayakrishnan PM

Here is the code snippet to open wifi settings page

这是打开wifi设置页面的代码片段

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

回答by Rahul Patil

on button click click Listner

在按钮上单击单击 Listner

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

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

回答by Hitesh Sahu

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