0

I implemented the code to check wifi state connected or not. I made a text view

TextView WifiTv = (TextView) findViewById(R.id.wifistate);

    ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiCheck = connectionManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


    if (wifiCheck.isConnected()) {
        // Do whatever here
        WifiTv.setText("WiFi is Connected");
        WifiTv.notify();
    } else {
        WifiTv.setText("WiFi is not Connected");
    }

It works but it does not update dynamically and therefore if I turn off wifi I have to refresh the activity for the textview to change (not data binded). How can I make the textview update without refresh please? Thx

EDIT: I have this code in a seperate class which works well with a toast. All I want to do is display in textview instead. How can I connect the textview variable to this class which does not have xml please?

public void onReceive(Context context, Intent intent) {

    NetworkInfo info =
            intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    if (info != null && info.isConnected()) {
        // Do your work.
        // e.g. To check the Network Name or other info:
        WifiManager wifiManager = (WifiManager)
                context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String ssid = wifiInfo.getSSID();

        Toast.makeText(context, "Connected to: " + ssid,
                Toast.LENGTH_SHORT).show();


    }
trooper
  • 4,444
  • 5
  • 32
  • 32

2 Answers2

0

You required BroadcastReceiver and wifimanager. BroadcastReceiver which start when app launches to receive the the implicit intent every time. wifimanager to to check the status.

Sunil Hamne
  • 1
  • 1
  • 4
0

did you try to add a onResume function to check again each time you resume yout activity?

Don't know your app but maybe this,

@Override
    protected void onResume() {
        super.onResume();

        if (wifiCheck.isConnected()) {
        // Do whatever here
            WifiTv.setText("WiFi is Connected");
            WifiTv.notify();
        } else {
            WifiTv.setText("WiFi is not Connected");
        }
    }

If your others variables are not global to Activity, create them again within the onResume

Alexandre
  • 1,259
  • 2
  • 15
  • 25