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?