简体   繁体   English

当我打开我的android app且没有WiFi连接时,它显示没有WiFi连接,如代码中所给

[英]when i open my android app when no WiFi connected it show no WiFi connected as i given in the code

but when i connect my app with the WiFi it still show no WiFi connected , so i have to close the app and open it again with WiFi connected. 但是当我使用WiFi连接我的应用程序时,它仍然显示未连接WiFi,因此我必须关闭该应用程序,然后在连接WiFi的情况下再次打开它。

if (wifimanager.setWifiEnabled(true)) {
      punchin.setOnClickListener {

          if ((conMgr.activeNetworkInfo != null) && (wifi.isAvailable == true && wifi.isConnected == true)) {

              Log.v("MAc", "mac=" + wMAC + "\n" + wbssid + "\n" + wssid)

              if ((wbssid == "") && (wssid.equals(""))
                      && conMgr.activeNetworkInfo != null && conMgr.activeNetworkInfo.isAvailable
                      && conMgr.activeNetworkInfo.isConnected) {

                  textView.text = "Connected to correct Wifi"
                  punchout.visibility = View.VISIBLE

              } else if (wbssid!="") {

                  Toast.makeText(applicationContext, "Connect To Correct Wifi", Toast.LENGTH_LONG).show()
              }
          } else {

              Toast.makeText(this, "No Wifi Connection", Toast.LENGTH_LONG).show()
          }
      }
  }

You need to add a receiver if you want to listen connection change status . 如果要监听连接更改状态,则需要添加接收器。 Check code below - 检查下面的代码-

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkWifiConnectionStatus();
        registerBroadCastReceiver();
    }


    private void registerBroadCastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(receiver, intentFilter);
    }

    private void checkWifiConnectionStatus() {
        WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (wifiMgr.isWifiEnabled()) {
            WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

            if (wifiInfo.getNetworkId() == -1) {
                Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
            return;
        } else {
            Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkWifiConnectionStatus();
        }
    };

Register the broadcast receiver inside your activity and within that receiver monitor the status of connection by connectivity manager when connection status changes call the method to perform your task. 在您的活动中注册广播接收器,并在该接收器中通过连接管理器监视连接状态,当连接状态发生更改时,该方法会调用该方法来执行您的任务。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       BroadcastReceiver  netReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
                if (isConnected) {
                    try {
                        performTask(isConnected);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    performTask(isConnected);
                }
            }
        };
    }



public void performTask(boolean isConnected) {
    if (isConnected) {
        Log.i("test", "connection successfull");
    } else {
        Log.i("test", "connection failed");
    }
}

and add the following permissions in menifest 并在清单中添加以下权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我连接到我的Wifi时,没有在Android上接收推送通知 - Not receiving push notifications on Android when I'm connected to my Wifi 当我的wifi连接到Android移动设备时打开一些链接 - open some link when my wifi connected in Android mobile 当单击按钮时,在连接WiFi时,在Android上显示进度对话框 - when click button, while connected wifi, show progress dialog on android Android平板电脑:连接到wifi后自动安装/更新应用 - Android Tablet: Automatically install/update app when connected to wifi 连接互联网/ WiFi时运行代码 - Run code when internet / WiFi is connected 在android中更改wifi时如何获取连接的wifi ssid - How to take the connected wifi ssid when change the wifi in android 当我打开wifi并连接时,TrafficStats.getMobileRxBytes()和TrafficStats.getMobileTxBytes()始终返回0 - When I open wifi and connected, TrafficStats.getMobileRxBytes() and TrafficStats.getMobileTxBytes() always return 0 当支持 Internet 的 WiFi 网络可用时,如何使 Android 连接到无法访问 Internet 的 IoT WiFi AP? - How do I keep Android connected to an IoT WiFi AP with no internet access when an internet-enabled WiFi network is available? 通过WiFi下载数据时,如何知道我已通过Wifi连接 - When downloading data over WiFi, how do I know I am connected using Wifi 即使关闭wifi也可以“连接” - “Connected” even when wifi is off
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM