What code do I need to know if my android app has mobile internet (data) or wifi?

0

I am programming a hybrid android application, in which I need to know if the user (phone) is connected to some kind of network, whether mobile data or wi-fi, and in case DOES NOT EXIST this connection the application will launch a Toast where it asks to verify the connection.

    
asked by Rose Hernandez 17.08.2016 в 19:52
source

2 answers

2

You can detect connectivity, using this method:

private static ConnectivityManager manager;

    public static boolean isOnline(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isAvailable() && 

networkInfo.isConnected();
}

To detect if the network is WiFi type  :

public static boolean isConnectedWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}

or if it is Mobile type:

  public static boolean isConnectedMobile(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    }

Simply call the methods in an IF statement, for example to check connectivity, if it does not exist we show a Toast :

if (isOnline(getApplicationContext())){
    //existe conectividad
    Toast.makeText(getActivity(), "Existe conectividad", 
   Toast.LENGTH_LONG).show();
}else{
    //No existe conectividad.
    Toast.makeText(getActivity(), "No existe conectividad", 
   Toast.LENGTH_LONG).show();
}
    
answered by 18.08.2016 в 06:03
1

There are some questions in your question, I had android-studio as a label but you explain that it is a hybrid app, if it is with Android Studio then it is native and to check the connectivity is this:

There are many examples, one of them ...

public class RevisarInternet{


private static final String TAG = RevisarInternet.class.getSimpleName();



public static boolean ConexionDisponible(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"No hay conexión de internet");
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            return true;
        }
        else
        {
            Log.d(TAG," Conexión a internet");
            return true;
        }

    }
}
    }

In your activity you want to show the Toast :

if(RevisarInternet.ConexionDisponible(Activity.this)) 
                {

                       // Muestras tu toast que si hay conexión
                }   
               else
                {
                     // Muestras tu toast que no hay conexión
                }  

You add this to your Manifest :

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >

Now, if it is a hybrid application (taking into account that it is Ionic because you do not specify what technology you use) you can test the network:

var myApp = angular.module('myapp', ['ionic'])
    .run(function($ionicPlatform, $ionicPopup) {
        $ionicPlatform.ready(function() {
            if(window.Connection) {
                if(navigator.connection.type == Connection.NONE) {
                    $ionicPopup.confirm({
                        title: "Internet Desconectado",
                        content: "No tienes internet."
                    })
                    .then(function(result) {
                        if(!result) {
                            ionic.Platform.exitApp();
                        }
                    });
                }
            }
        });
    });

Answers obtained from here and here.

    
answered by 17.08.2016 в 20:17