如何申请开启Wi-Fi?

时间:2014-11-03 00:41:23

标签: android wifi

如何提示用户打开WiFi?我了解如何强制启用WiFi,例如this example,但我的目标是让用户能够选择。

我搜索WiFiManager class和其他类似的课程没有运气。提前谢谢!

我的目标示例,如蓝牙

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

1 个答案:

答案 0 :(得分:0)

嗯..为什么不用两个按钮创建一个警告框?根据用户选择的内容,您可以启用或不使用WIFI。

喜欢这个

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to turn WIFI ON?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
       WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
       wifi.setWifiEnabled(true); // true or false to activate/deactivate wifi
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                // Do Nothing or Whatever you want.
                dialog.cancel();
           }
       });
    AlertDialog alert = builder.create();
    alert.show();

希望有所帮助。我的目标是在某种程度上复制蓝牙警报框。