I want to write an event for different modes of Wi-Fi connection and mobile data.
- If the Wi-Fi is on and the mobile data is off, the Wi-Fi icon will be visible
- If the mobile data is on and the Wi-Fi is off, the mobile data icon will be visible
- If both are on, both icons will be visible
- If both are off, the icon will be gone
- If both were on, and one of them went off, the icon that went off would go off
Exactly the same as the phone's system performance!
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifiType = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobileType = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiType.isConnectedOrConnecting()) {
wifi.setVisibility(View.VISIBLE);
} else if (mobileType.isConnectedOrConnecting()) {
data.setVisibility(View.VISIBLE);
} else if (wifiType.isConnectedOrConnecting() || mobileType.isConnectedOrConnecting()) {
wifi.setVisibility(View.VISIBLE);
data.setVisibility(View.VISIBLE);
} else {
wifi.setVisibility(View.GONE);
data.setVisibility(View.GONE);
}
}
}