Calling the same condition in the code

Asked

Viewed 142 times

0

//Função para verificar existência de conexão com a internet
public boolean verificaConexao() {  
    boolean conectado;  
    ConnectivityManager conectivtyManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
    if (conectivtyManager.getActiveNetworkInfo() != null  
        && conectivtyManager.getActiveNetworkInfo().isAvailable()  
        && conectivtyManager.getActiveNetworkInfo().isConnected()) {  
            conectado = true;  
    } else {  
        conectado = false;  
    }  
    return conectado;  
}

// Verifica a conexao
if (verificaConexao() == true){
    // Chama a classe RequestTask
    new RequestTask().execute(ok);
} else {
    loader.setVisibility(View.GONE);
    tx.setVisibility(View.VISIBLE);
    tx.setText("Por favor, conecte-se a internet :(");
    ba.setVisibility(View.VISIBLE);
}

I want to use that same condition in several places in my code. I’d have to do something to call her instead of having to write her all over, every time I needed her?

  • 2

    Without you giving details of what the if makes it difficult to give a solution, but you can make a function even.

  • 1

    If what you need to reuse is the condition of if, create a function that returns a boolean and make if ( condicao() ) { ... }. If what you want to reuse is the body, put it in the function and do if ( ... ) corpo(). If you want to reuse everything, simply create a function with if and body, and call it when you need it. If necessary, pass parameters to this function, and remember that it cannot change local variables in the calling method (if this is necessary, in Java there is no output, unless you encapsulate these variables in an object or array).

  • 1

    Try to explain better what you want. It is to create a function that returns true or false?

  • There is something special you want to do and we don’t understand (then you need to explain it better). Or you still don’t know how to program? What everyone is understanding is that you want to do something very basic in programming, we need to know if this is the way to help you. Otherwise the question will be closed because it is not clear.

  • edited, you can understand now? :)

  • If what you want is not what I answered, then I still can not understand.

  • You changed the question once again and still not giving to understand what you want. If you want to use the method verificaConexao() in several places, just use it. If you want all the if whole (which alias has a redundant condition) is in a method, put it in a method and call where you need it. It’s the same as what you’ve done with the other condition. If it’s not yet that, find a way to explain what you want. Changing the examples isn’t helping.

  • tried with a method more than wasn’t working because of the value that wasn’t boolean but string, @bigown vlw ae :)

Show 3 more comments

2 answers

3


Solved:

public void conexao() {
    if (verificaConexao() == true){
        // Chama a classe RequestTask
        new RequestTask().execute(site);
    } else {
        loader.setVisibility(View.GONE);
        tx.setVisibility(View.VISIBLE);
        tx.setText("Por favor, conecte-se a internet :(");
        ba.setVisibility(View.VISIBLE);
    }
}

// Verifica a conexao
conexao();
  • So that the return if it is null? Wouldn’t it be better to define public void conexao()?

  • Thanks @Gustavocinque, because I didn’t know :S

2

Hello!

The polite thing is that you separate the responsibilities there.

Predicate<ConnectivityManager> connectionValidator = 
                c -> null != c.getActiveNetworkInfo() 
                    && c.getActiveNetworkInfo().isAvailable() 
                    && c.getActiveNetworkInfo().isConnected();

Then, in the guy you’re going to validate the connection, you manipulate the attributes and pass.

ConnectivityManager conectivtyManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  

if (connectionValidator.test(conectivtyManager)){
    new RequestTask().execute(ok);
} else {
    loader.setVisibility(View.GONE);
    tx.setVisibility(View.VISIBLE);
    tx.setText("Por favor, conecte-se a internet :(");
    ba.setVisibility(View.VISIBLE);
}

Of course here I assumed that you will validate only at class level. If it is something that will be used throughout the system, it is best that you create a class that implements Predicate and leave the code there. If you are using dependency injection somewhat better.

  • 1

    It should be noted that Predicate is only available from API24 and requires java 8.

  • 1

    It’s a point, young man. I forgot to comment, as well as that I chose to write using Lambda expression.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.