Class control internet connection Android - Wifi-3G

0

Hello.

Currently I control screen by screen the type of internet connection to which the user is connected or not.

I use a method similar to this:

    /*Check conection*/
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        //For 3G check
        final boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .isConnectedOrConnecting();
        //For WiFi Check
        boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .isConnectedOrConnecting();

        if (!is3g && !isWifi){

            Toast.makeText(AgendamentoActivity.this, "Sem conexão!! Verifique...", Toast.LENGTH_LONG).show();
            finish();

}

I would like to know how I could create a single class for this and call it in Activity that I need to control it.

I do not have much experience in this and even found some models but I did not quite understand.

    
asked by anonymous 17.11.2017 / 14:37

1 answer

0

Just create a normal class with this method, and call in the constructor:

public class Conexao {
    private boolean wifi;
    private boolean movel;

    public Conexao() {
        verificarConexao();
    }

    public boolean getWifi() {
        return wifi;
    }

    public boolean getMovel() {
        return movel;
    }

    private void verificarConexao() {
       ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        //For 3G check
        movel = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        //For WiFi Check
        wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

        if (!is3g && !isWifi) {
            Toast.makeText(AgendamentoActivity.this, "Sem conexão!! Verifique...", Toast.LENGTH_LONG).show();
            finish();
        }
    }
}

When you install, you will assign the true or false to the variables, then just get the

    
17.11.2017 / 14:52