2

Currently, I am struggling to show the internet speed consistently in the status bar. What I am able to do is, finding the internet speed by the old approach(Upload & download some file from/to server and using TrafficStats API). I tried some of the play store apps, they all displaying the internet speed when the user opens up any app which uses the internet. I want to implement the same feature. I believe they either use a broadcast receiver or service. How can I implement the same?

Or is there any library which does the same or any other better approach?

RHS.Dev
  • 412
  • 6
  • 18
hacker
  • 8,919
  • 12
  • 62
  • 108

1 Answers1

0

The problem with displaying an internet speed is that it is always going to be 'historical' or an estimate - i.e. even if you were able to achieve 300Mbps 1 second ago it does not mean you will be able to achieve it now.

Bearing this in mind, the approach you already outlined is as reliable as any - i.e. measure the round trip time for a known size of data. If accuracy is important to you, this is probably the most accurate provided you are confident of the turnaround time on the server side.

You can listen to Android Network events which will tell you when the type of network connections has changed etc - the recommended approach has changed over API levels but this is a good starting point: https://developer.android.com/training/monitoring-device-state/connectivity-monitoring

For your example it sounds like you want to get as recent as possible Network Stats rather than connectivity changes - for that the 'NetworkStatsManager' is probably the best match.

Provides access to network usage history and statistics. Usage data is collected in discrete bins of time called 'Buckets'.

Mick
  • 24,231
  • 1
  • 54
  • 120