Search code examples
androidconnectionwifi

How can I check if wifi is receiving data on android?


So I've gone through a lot of trouble recently trying to figure out how to check if wifi connection is actually working on Android. I'm aware there are a lot of threads with similar matter around the community, but those mostly approach the issue from ConnectivityManager which seemingly is only able to check if there is a WiFi connection stablished and that is not enough.

I've also seen people suggesting ping/HTTP checks but I would like to know if there is a way around those methods since that would use any available data.

I intend to run the solution in a service that periodically checks if connection is available and phone is online, although I'm not quite sure if that's a viable way of accomplishing my goals.

Service:

package com.mattos.murilorm.meavisaquandotiverinternet;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service{

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this,"Service started!",Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this,"Service destroyed!",Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Please help!


Solution

  • There's no way of detecting actual connection quality without sampling some data, so if you don't want to do ping style requests you don't have a lot of options other than a simple check for whether or not you are connected to something.

    Facebook made a pretty useful library that will:

    Listen to current network traffic in the app and categorize the quality of the network.

    It's called network-connection-class.