Android:如何在弹出窗口中显示wifi设置

时间:2018-10-01 01:19:55

标签: android android-layout android-studio android-activity

我有以下代码,这些代码使我的应用程序进入后台并显示WIFI设置

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

是否可以在警报对话框中显示 WIFI 设置,使其不会离开我的应用?

谢谢。

1 个答案:

答案 0 :(得分:1)

创建这样的对话框:

final WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

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

希望它会为您提供帮助