Enabling Internet Connection in an Android App

Asked

Viewed 2,833 times

3

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

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

2 answers

3


To know the status of a network connection it is necessary to obtain an object of type Networkinfo.
The way to achieve this is by resorting to the class Connectivitymanager obtained as follows:

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

A device Android has several types of network connection available, among them 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 to know if you have 3G/4G connection:

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

Usually what is used is to get the active connection through:

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

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

To know the name of the active link use:

String connectionName = network.getTypeName();

Don’t forget the necessary permission:

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

There’s no such thing as "enable mobile data connection", what is active is the link to network/internet.

The type of active link, the one that is returned by manager.getActiveNetworkInfo(); is, within the available, the lowest cost to the user. If available TYPE_MOBILE and the TYPE_WIFI the active is TYPE_WIFI.

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() can also be used isConnectedOrConnecting() if you want to know if you’re still trying to connect.

Now to activate 3G (mobile data) it is necessary to use the Java Reflections API as it cannot 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);
}

It is necessary to state in the 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"/>

Browser other questions tagged

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