Question

I'm attempting to monitor the amount of WiFi usage consumed by a device using TrafficStats however when I attempt to do so I result in an extraordinarily large value. Even after performing a factory reset on the device I'm getting a VERY large value for the amount of data used (411970 MB of data usage within one minuteof resetting the device does not seem accurate)

Any suggestions are greatly appreciated!

SOURCE:

public class WifiMonitor extends Activity {

    Button sendButton;

    EditText msgTextField;

    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView infoView = (TextView) findViewById(R.id.traffic_info);
        String info = "";

        info += ("\tWifi Data Usage: " + TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes() / 1000000 + " MB");

        infoView.setText(info);

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("3055555555", null, info, null, null);   

        if (info.length() > 0) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://wifiusage.atwebpages.com/receiver.php");
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
                nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                nameValuePairs.add(new BasicNameValuePair("message", info));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }

        Intent myIntent = new Intent(WifiMonitor.this, Alarm.class);

        pendingIntent = PendingIntent.getService(WifiMonitor.this, 0,
                myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.add(Calendar.SECOND, 10);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), 30 * 1000, pendingIntent);

    }
}
Was it helpful?

Solution

You are adding the number of bytes, too

TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes() / 1000000 

So you are writting two numbers together, without spaces or any separators in between (first number of bytes, then number of MB).

Use brackets to aggrupate the operations

(TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes()) / 1000000 

OTHER TIPS

These are bytes! For correct result you must divide bytes like this:

  • bytes / 1024 to get KB
  • bytes / (1024 * 1024) to get MB
  • bytes / (1024 * 1024 * 1024) to get GB
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top