0

I am currently working on display toasts whenever the application is not connected to the wifi and the code for broadcast receiver has been implemented like this :

public class Broadcast extends BroadcastReceiver {
    @Override
    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();


        }

        else if   (info != null && !info.isConnected()){

            Toast.makeText(context, "Not Connected",
                    Toast.LENGTH_LONG).show();
        }
    }
}

It works but i want it that the intent is passed as soon as the application starts so if the application is started without wifi connection immediately the toast is displayed and keeps spamming until there is a connection. How can i implement this please. Also if i turn wifi on and off again it displays connected to unknown ssid. Is there any wifi i could remove that please?

  • "Spamming" is not a good idea. Implement a separate class that tracks your WiFi connection state. When you don't have connection, add something to the layout to show the user they aren't connected (and maybe [offer to help them](http://stackoverflow.com/a/2318800/1207921)). Remove that if the state changes and they are connected. – Karakuri Jan 07 '17 at 18:56
  • At the moment i have this class broadcast which tracks the state but is not triggered when app starts – Glen Micallef Jan 07 '17 at 19:16

0 Answers0