Eduardo
Eduardo

Reputation: 4382

How to use TrafficStatsCompat?

The minimum SDK version for my project is 7, I am not able to use TrafficStats out of the box. Therefore, currently I am using an approach based on reflection as shown here. The main problem with that is that I cannot have this stats on devices with earlier than Android 2.3, it just gives me the possibility to do not Force Close on a 2.1 device.

Then now I am looking for a way to use the TrafficStatsCompat as it is described on the doc as "Helper for accessing features in TrafficStats introduced after API level 14 in a backwards compatible fashion."

My main problem is that I could not find any example on how to use this compatibility class. I've been looking for other classes inside the support library so I could try to mimic the behavior on how to use it, but I was not successful. Can someone provide an example on how to use the TrafficStats methods, e.g., getTotalTxBytes, but with TrafficStatsCompat.

Upvotes: 0

Views: 364

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007124

Can someone provide an example on how to use the TrafficStats methods, e.g., getTotalTxBytes, but with TrafficStatsCompat.

No, because that class has nothing to do with accessing traffic stats on API Level 7. It does precisely what you quoted: it helps developers use methods added to TrafficStats after API Level 14.

There is no way for you to get traffic information prior to API Level 8 when TrafficStats was introduced.


UPDATE

Since trying to answer the comment in a comment was going to be painful...

You only need to play reflection games like that if you are continuing to support Android 1.x (and if you are, you are a saint).

If you are sticking to Android 2.x and higher, you can simply route using Build:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.FROYO) {
    // do something involving TrafficStats
}

Upvotes: 1

Artyom Kiriliyk
Artyom Kiriliyk

Reputation: 2513

You should add to your project (into lib folder) following library android-support-v4.jar (android-sdk\extras\android\support\v4)

Upvotes: 0

Related Questions