I'm working on the ability to check for an internet connection like what YouTube currently does on the Android app.
The problem is if internet is available, it shows as online every time. I want to make it like if it is already online, don't show any message but when not online then show it every time.
Here is My MainActivity code:
public class LoginActivity extends AppCompatActivity implements NetworkReceiver.ConnectionChangeCallback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
IntentFilter intentFilter02 = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
networkReceiver = new NetworkReceiver();
registerReceiver(networkReceiver, intentFilter02);
networkReceiver.setConnectionChangeCallback(this);
}
@Override
public void onConnectionChange(boolean isConnected) {
if (isConnected){
// will be called when internet is gone.
tv_check_connection.setText("Back Online");
connection.setBackgroundColor(Color.parseColor("#00BE84"));
connection.startAnimation(slideDownToUp);
tv_check_connection.setTextColor(Color.WHITE);
Handler handler = new Handler();
Runnable delayrunnable = new Runnable() {
@Override
public void run() {
tv_check_connection.setVisibility(View.GONE);
}
};
handler.postDelayed(delayrunnable, 4000);
}else{
// will be called when internet is back
new MyDialog().show(getSupportFragmentManager(), "my_dialog");
tv_check_connection.setText("No connection");
tv_check_connection.setVisibility(View.VISIBLE);
connection.startAnimation(slideDownToUp);
connection.setBackgroundColor(Color.parseColor("#2D2D2D"));
tv_check_connection.setTextColor(Color.WHITE);
}
}
}
And here is my NetworkReceiver class:
public class NetworkReceiver extends BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = "com.example.ritecare.NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";
ConnectionChangeCallback connectionChangeCallback;
@Override
public void onReceive(Context context, Intent intent) {
Intent networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
networkStateIntent.putExtra(IS_NETWORK_AVAILABLE, isOnline(context));
LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
if (connectionChangeCallback != null) {
connectionChangeCallback.onConnectionChange(isOnline(context));
}
}
private boolean isOnline(Context context) {
try {
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//boolean isConnected = networkInfo != null && networkInfo.isConnectedOrConnecting();
return networkInfo != null && networkInfo.isConnected();
}
return false;
} catch (Exception e) {
Log.e(NetworkReceiver.class.getName(), e.getMessage());
return false;
}
}
public void setConnectionChangeCallback(ConnectionChangeCallback connectionChangeCallback) {
this.connectionChangeCallback = connectionChangeCallback;
}
public interface ConnectionChangeCallback {
void onConnectionChange(boolean isConnected);
}
}
How can this be achieved?