Enabling Internet Connection in an Android Application

3

I would like to know a way to identify if the android cell phone is connected to the internet independently if it is the mobile network of the mobile or wifi carrier, and if it is not connected, I activate all connections both wifi and mobile data. >

Note: I know how to check if the wifi is active and how it activates it, I just do not know the mobile data yet.     

asked by anonymous 20.07.2015 / 21:23

2 answers

3

To find out what the state of a network connection is, you need to get an object of type NetworkInfo .
The way to do this is by calling the ConnectivityManager class obtained as follows:

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

An Android device has several types of network connectivity available, including TYPE_MOBILE and TYPE_WIFI .

To test whether a particular type is available you can use the following method:

public static boolean isNetworkConnected(ConnectivityManager connectivityManager, int type){
    final NetworkInfo network = connectivityManager.getNetworkInfo(type);
    if (network != null && network.isAvailable() && network.isConnected()){

        return = true;
    } else {
        return false;
    } 
}

For example if you have 3G / 4G connection:

ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isConnected = isNetworkConnected(manager, ConnectivityManager.TYPE_MOBILE);

Normally, the active connection is made through:

ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = manager.getActiveNetworkInfo();

boolean isConnected = network != null && network.isConnected() && network.isAvailable();  

To find out the name of the active connection use:

String connectionName = network.getTypeName();

Do not forget the permission required:

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

There is no such thing as "enable the connection to mobile data" , what is activated is the connection to the network / internet .

The active connection type, the one that is returned by manager.getActiveNetworkInfo(); is, within available ones, the lowest cost for the user. If TYPE_MOBILE is available, TYPE_WIFI is active TYPE_WIFI .

    
20.07.2015 / 23:13
1

To check if a is connected to the Internet independent of the interface can be used the following code:

ConnectivityManager cm =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    boolean internetDisponível = netInfo != null && netInfo.isConnected();

Instead of using isConnected() it can also be used isConnectedOrConnecting() if you want to know if you are still trying to connect.

Now to activate 3G (mobile data) it is necessary to use the Java Reflections API, since it can not be activated through the Android API. Then follow the code to activate:

private void setMobileDataEnabled(Context context, boolean enabled) {
    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}

You must declare in AndroidManifest.xml that you will check the state of the connection and change it respectively:

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    
20.07.2015 / 22:44