Surabhi Choudhary
Surabhi Choudhary

Reputation: 548

check for poor internet connectivity android studio

I am using reward ad video in my app. When there is no internet connection, I check it using this function and do not show the ad.

public final boolean isInternetOn() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

But the problem is, when there is poor connection it takes so much time to show the ad and ad is not shown many times. I want to check for poor connectivity and if connection is poor don't show the ad and show alert for poor connection. How to achieve this?

Upvotes: 1

Views: 6797

Answers (2)

Ezio
Ezio

Reputation: 2985

You need to define what slow network means. I will assume that by the slow network you mean 2G and 3G networks. In that case, you can use subType of NetworkInfo class to identify which type of network the user is currently in and perform the appropriate action.

For example, let say you decided that 2G is slow, you can identify the 2G network as follows

NetworkInfo network = connectivity.getActiveNetworkInfo();
int netSubType = network.getSubtypeName();
if(netSubType == TelephonyManager.NETWORK_TYPE_GPRS ||
    netSubType == TelephonyManager.NETWORK_TYPE_EDGE ||
    netSubType == TelephonyManager.NETWORK_TYPE_1xRTT) {
  //user is in slow network
}

See TelephonyManager class for more such constant values.

Upvotes: 3

Black.Jack
Black.Jack

Reputation: 1957

You could build a simple logic through Java Thread basic specifics, for example this runnable class is what you would use to get and time the connectivity:

public class CheckInternetActivity implements Runnable{


    public CheckInternetActivity() {

    }

    @Override
    public void run()  {
        System.out.println("Call...sleep....zzzzZZZZZ");
         try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            System.out.println("Call...wake up!"); 
    } 
}

and this is a possible basi main method call attempt:

ExecutorService executor = Executors.newSingleThreadExecutor();

    executor.submit( new CheckInternetActivity() );  
    executor.shutdown();

    System.out.println(executor.awaitTermination(4, TimeUnit.SECONDS));//this gives boolean if you respect given timing rate...

So wrapping all up in a pseudo:

public class CheckInternetActivity implements Runnable{

    private ConnectivityManager connectivityManager;
    private NetworkInfo activeNetworkInfo;

    public CheckInternetActivity(ConnectivityManager connectivityManager, NetworkInfo activeNetworkInfo ) {
        this.connectivityManager = connectivityManager;
        this.activeNetworkInfo = activeNetworkInfo;
    }

    @Override
    public void run()  {
        System.out.println("getting internet connection");
        ConnectivityManager connectivityManager
        = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            System.out.println("Connected!"); 
    }


}

main...:

ConnectivityManager connectivityManager;
NetworkInfo activeNetworkInfo;

ExecutorService executor = Executors.newSingleThreadExecutor();

executor.submit( new CheckInternetActivity(connectivityManager, activeNetworkInfo) );  
executor.shutdown();

System.out.println(executor.awaitTermination(4, TimeUnit.SECONDS));//this tells you how much did it takes...and let you do your move based on this output
if(activeNetworkInfo != null)
    System.out.println( "activeNetworkInfo--->" + activeNetworkInfo.isConnected());
else
    System.out.println( "activeNetworkInfo--->null!!");

Let me know if it helps...

Upvotes: 1

Related Questions