Problems with internet verification method on Android

0

I have this method to check if the device is connected. But I'm getting some msgs of problems with syncing my data. And I'm thinking there must have been this change in the internet verification method. I am not experienced, so I preferred to come and ask if there is something wrong or missing in this method. Thank you.

public static boolean isNetworkAvailable (Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected() && activeNetworkInfo.isAvailable();

}
    
asked by anonymous 30.01.2018 / 16:29

2 answers

0

I usually use these two methods to ensure that you are connected to a network and that you have internet.

public static boolean isOnline(Context context) {

    ConnectivityManager cm =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if( netInfo != null && netInfo.isConnectedOrConnecting()){
        Log.d("Utils","Esta conectado em um rede!");
        if( isInternetAvailable()){
            Log.d("Utils","Possui internet na rede!");
            return true;
        }else{
            Log.e("Utils","Não possui internet na rede!");
        }
    }
    Log.e("Utils","Não está conectado na rede!");
    return false;
}
public static boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com");
        return !ipAddr.equals("");

    } catch (Exception e) {
        return false;
    }

}
    
30.01.2018 / 16:52
0

Speak there;)

This method returns you a boolean saying whether or not you are with the internet on smart.

I hope it helps.

public boolean checkOnlineState() {
        ConnectivityManager CManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo NInfo = CManager.getActiveNetworkInfo();
        if (NInfo != null && NInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

if(checkOnlineState()){
   // tem internet
}else{
   // nao tem internet             
}
    
01.02.2018 / 12:56