Bunny
Bunny

Reputation: 237

detect slow internet connection in android

I have developed Android application and it is working fine when it using WiFi. It also works when there is proper cellular network. But the problem is when the signal is weak (still active connection) then there is a problem.

I have used a loader till the data is being loaded on Activity. Now how to determine the slow internet connection because i have to give some toast information to the user that internet connectivity is slow. Can anyone help on this.?

Upvotes: 0

Views: 4023

Answers (2)

user3742480
user3742480

Reputation:

If the user has a stable connection and you just want to test if the connection is to slow for your purpose, you could implement a simple speedtest by downloading a test file from your server and stop the taken time. So you could set a maximum amount of time your file needs to download and if this maximum is exceeded the connection of your client is too slow.

Upvotes: 0

arjun
arjun

Reputation: 3574

You can get the total amount data transferred from you app using TrafficStats of android.net package. Then you detect amount of data transferred per second which gives the speed of you network link

    trafficStats = new TrafficStats();
    uid = android.os.Process.myUid();
    if(trafficStats.getUidRxBytes(uid) != trafficStats.UNSUPPORTED) {
        initialBytesCount = trafficStats.getUidTxBytes(uid) / 1024;
    } else{
        uid = -1;
    }

    startTime = System.currentTimeMillis();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            linkSpeed = trafficStats.getUidTxBytes(uid) / 1024 - initialBytesCount;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    transferRate = 0;
                    long recordedTime = (System.currentTimeMillis() - startTime) / 1000;
                    try {
                        transferRate = linkSpeed / recordedTime;
                        Log.i("TransferRate", transferRate + "");
                    }catch (Exception e){

                    }
                }
            });
        }
    };
    netWorkSpeedTimer = new Timer();
    netWorkSpeedTimer.scheduleAtFixedRate(task, 1000, 1000); 

Upvotes: 2

Related Questions