【问题标题】:How to request Wi-Fi be turned on?如何请求开启 Wi-Fi?
【发布时间】:2014-11-03 00:41:23
【问题描述】:

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

我已经搜索了WiFiManager class 和其他类似的课程,但没有运气。提前致谢!

我的目标示例,如在蓝牙中所做的那样

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

【问题讨论】:

  • 我猜你已经想到了,但如果真的不能像蓝牙一样,通知会做吗?
  • 不错的主意,但最好减少权限!

标签: android wifi


【解决方案1】:

嗯.. 为什么不创建一个带有两个按钮的警报框呢?根据用户的选择,您可以启用或不使用 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();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2012-05-19
    • 1970-01-01
    • 2017-02-21
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多