0

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?

Edric
  • 24,639
  • 13
  • 81
  • 91

1 Answers1

0

Remove static form Views. Pass views in as parameter in method dialog03.

Check this code.

 TextView tv_check_connection; 
 LinearLayout connection;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);

    tv_check_connection = (TextView) findViewById(R.id.tv_check_connection_opdipd);
    connection = (LinearLayout) findViewById(R.id.check_connection_opdipd);

// for calling dialog03 method
// dialog03(true, tv_check_connection, connection );
}

public static void dialog03(boolean value, TextView tvCheckConnection, LinearLayout connectionLayout) {


    if (value) {
        tvCheckConnection.setText("Back online");
        connectionLayout.setBackgroundColor(Color.parseColor("#00BE84"));
        connectionLayout.startAnimation(slideDownToUp);
        tvCheckConnection.setTextColor(Color.WHITE);

        Handler handler = new Handler();
        Runnable delayrunnable = new Runnable() {
            @Override
            public void run() {
                if(tvCheckConnection != null){
                   tvCheckConnection.setVisibility(View.GONE);
                }
            }
        };
        handler.postDelayed(delayrunnable, 4000);
    } else {
        tvCheckConnection.setVisibility(View.VISIBLE);
        tvCheckConnection.setText("No connection");
        connectionLayout.startAnimation(slideDownToUp);
        connectionLayout.setBackgroundColor(Color.parseColor("#2D2D2D"));
        tvCheckConnection.setTextColor(Color.WHITE);

    }
}

As you are updating visibility of a view after some delay its safe way check that view is not null.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • I can't or have to call dialog method inside onCreate, i have a class App.class and NetworkReciever.class to handle it. And error is, it want final TextView – Sheikh Asif Iqbal Sep 30 '19 at 06:21
  • If you want to call `dialog03` method from another class than send `Broadcast` to Activity. In Activity call `dialog03` after receiving Broadcast. Its not good to call a `static` method of a `Activity` form other class – Abu Yousuf Sep 30 '19 at 06:45
  • @Abu_Yousuf My question is changed, can you please help me on that! – Sheikh Asif Iqbal Oct 01 '19 at 05:25
  • As you are sending Broadcast from `NetworkReceiver`, receive that Broadcast in `Activity` and show dialog if required. – Abu Yousuf Oct 01 '19 at 06:55