Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:45:42+00:00 2026-05-30T15:45:42+00:00

I’ve written a nice little Android app to check data usage, unfortunately it relies

  • 0

I’ve written a nice little Android app to check data usage, unfortunately it relies heavily on android.net.TrafficStats which was introduced with Froyo (Android 2.2).

I’m attempting to back-port this class for my non-Froyo users, and what I’m able to determine from the Android source is:

  1. TrafficStats.java is just a native pointer to a c file
  2. The c file opens two files (see below) and reads their contents
  3. If either contains a numeric value it spits it back as the “bytes used” count

Here’s my challenge… when I call TrafficStats via the API on my device, I get a reading (ex. 1113853 bytes). When I open the two files and check their contents, one file doesn’t exist and the other file is 0 bytes.

So clearly I mis-understood what TrafficStats is doing. Can anyone shed some light on how it’s working it’s magic?

Thanks for the help.

(here is my attempt to port the c file to java)

package com.suttco.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.suttco.IOUtils;
import com.suttco.StringUtils;

import android.util.Log;

public class TrafficStatsFile {

 private static final String mobileRxFile_1 = "/sys/class/net/rmnet0/statistics/rx_bytes";
 private static final String mobileRxFile_2 = "/sys/class/net/ppp0/statistics/rx_bytes";
 private static final String mobileTxFile_1 = "/sys/class/net/rmnet0/statistics/tx_bytes";
 private static final String mobileTxFile_2 = "/sys/class/net/ppp0/statistics/tx_bytes";

 private static final String LOGGING_TAG = TrafficStatsFile.class.getSimpleName();

 public long getMobileRxBytes() {
  return tryBoth(mobileRxFile_1, mobileRxFile_2);
 }

 public long getMobileTxBytes() {
  return tryBoth(mobileTxFile_1, mobileTxFile_2);
 }

 // Return the number from the first file which exists and contains data
 private static long tryBoth(String a, String b) {
  long num = readNumber(a);
  return num >= 0 ? num : readNumber(b);
 }

 // Returns an ASCII decimal number read from the specified file, -1 on error.
 private static long readNumber(String filename) {
  File f = new File(filename);
  if(f.exists()) {
   if(f.canRead()) {
    try {
     Log.d(LOGGING_TAG, "f.length() = " + f.length());
     String contents = IOUtils.readFileAsString(f);
     if(StringUtils.IsNotNullOrEmpty(contents)) {
      try {
       return Long.parseLong(contents);
      }
      catch(NumberFormatException nfex) {
       Log.w(LOGGING_TAG, "File contents are not numeric: " + filename); 
      }
     }
     else {
      Log.w(LOGGING_TAG, "File contents are empty: " + filename); 
     }
    }
    catch (FileNotFoundException fnfex) {
     Log.w(LOGGING_TAG, "File not found: " + filename, fnfex);
    }
    catch(IOException ioex) {
     Log.w(LOGGING_TAG, "IOException: " + filename, ioex);
    }
   }
   else {
    Log.w(LOGGING_TAG, "Unable to read file: " + filename);
   }
  }
  else {
   Log.w(LOGGING_TAG, "File does not exist: " + filename);
  }
  return -1;
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:45:43+00:00Added an answer on May 30, 2026 at 3:45 pm

    Here’s a working, modified version of the code above. This one is using RandomAccessFile and doesn’t rely on custom imports but uses only built-in String functions with their Exceptions.

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    import android.util.Log;
    
    public class TrafficStatsFile {
    
    private static final String mobileRxFile_1 = "/sys/class/net/rmnet0/statistics/rx_bytes";
    private static final String mobileRxFile_2 = "/sys/class/net/ppp0/statistics/rx_bytes";
    private static final String mobileTxFile_1 = "/sys/class/net/rmnet0/statistics/tx_bytes";
    private static final String mobileTxFile_2 = "/sys/class/net/ppp0/statistics/tx_bytes";
    
    private static final String LOGGING_TAG = TrafficStatsFile.class.getSimpleName();
    
    public long getMobileRxBytes() {
        return tryBoth(mobileRxFile_1, mobileRxFile_2);
    }
    
    public long getMobileTxBytes() {
        return tryBoth(mobileTxFile_1, mobileTxFile_2);
    }
    
    // Return the number from the first file which exists and contains data
    private static long tryBoth(String a, String b) {
        long num = readNumber(a);
        return num >= 0 ? num : readNumber(b);
    }
    
    // Returns an ASCII decimal number read from the specified file, -1 on error.
    private static long readNumber(String filename) {
        try {
            RandomAccessFile f = new RandomAccessFile(filename, "r");
            try {
                Log.d(LOGGING_TAG, "f.length() = " + f.length());
                String contents = f.readLine();
                if(!contents.isEmpty() && contents!=null) {
                    try {
                        return Long.parseLong(contents);
                    }
                    catch(NumberFormatException nfex) {
                        Log.w(LOGGING_TAG, "File contents are not numeric: " + filename); 
                    }
                }
                else {
                    Log.w(LOGGING_TAG, "File contents are empty: " + filename); 
                }
            }
            catch (FileNotFoundException fnfex) {
                Log.w(LOGGING_TAG, "File not found: " + filename, fnfex);
            }
            catch(IOException ioex) {
                Log.w(LOGGING_TAG, "IOException: " + filename, ioex);
            }   
        }catch(FileNotFoundException ffe){
            Log.w(LOGGING_TAG, "File not found: " + filename, ffe);
        }
        return -1;
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.