Android:显示启用wifi的对话框

问题描述 投票:2回答:3

我想,如果禁用wifi,则向用户显示一个对话框,可以决定是否要启用或禁用wifi。

好吧,我可以看看是否启用了wifi,但我怎么能显示该设置对话框。

这是我正在使用的代码:

if(wifiMan.isWifiEnabled()==false){
    Log.i("DEBUG","turning on wifi");
    wifiMan.setWifiEnabled(true);//I would like that the user decide
    }

  else {  
  Log.i("DEBUG","wifi is on");
   }

PS:我使用过:startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));但我收到此错误:ACTION_WIFI_SETTINGS无法解析或不是字段

我想有这个互动对话:

android android-alertdialog android-wifi
3个回答
8
投票

如果不能正常工作,你可以尝试这种方式。问题可能是导入错误的命名空间。试试这个

android.app.activity.startActivity(new android.content.Intent.Intent(android.provider.settings.Settings.ACTION_WIFI_SE TTINGS));

如果上面的情况不起作用。你也可以试试这个

startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

像这样创建对话框:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

            // set title
            alertDialogBuilder.setTitle("Wifi Settings");

            // set dialog message
            alertDialogBuilder
                .setMessage("Do you want to enable WIFI ?")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        //enable wifi
                        wifiMan.setWifiEnabled(true);
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        //disable wifi
                        wifiMan.setWifiEnabled(false);
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

2
投票

试试这个:

startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

0
投票

嗨,一旦看看

http://developer.android.com/reference/android/provider/Settings.html

对于不同类型的Intent操作,您可以使用它来启动各种设置屏幕

在提到一些网站后,这些是给出的最佳解决方案

    android.app.activity.startActivity(newandroid.content.Intent.Intent(android.provider.settings.Settings.ACTION_WIFI_SE‌​TTINGS)); 

    or 

    using, startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

or

context.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

这里有一些教程链接,解释了你的想法。

http://www.coderzheaven.com/2011/09/18/how-to-open-wifi-settings-in-android/

http://andbrain.com/blog/how-to-start-wifi-settings-screen-programmatically-with-intent/

http://www.androidsnippets.com/open-wifi-settings

© www.soinside.com 2019 - 2024. All rights reserved.