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?
Without you giving details of what the
if
makes it difficult to give a solution, but you can make a function even.– Lucas Virgili
If what you need to reuse is the condition of
if
, create a function that returns aboolean
and makeif ( condicao() ) { ... }
. If what you want to reuse is the body, put it in the function and doif ( ... ) 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).– mgibsonbr
Try to explain better what you want. It is to create a function that returns true or false?
– Jorge B.
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.
– Maniero
edited, you can understand now? :)
– JBarbosa
If what you want is not what I answered, then I still can not understand.
– Maniero
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 theif
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.– Maniero
tried with a method more than wasn’t working because of the value that wasn’t boolean but string, @bigown vlw ae :)
– JBarbosa