【问题标题】:Android Traffic Stats Results in Extraordinarily Large ValueAndroid 流量统计的结果非常大
【发布时间】:2013-06-13 14:03:54
【问题描述】:

我正在尝试使用 TrafficStats 监控设备消耗的 WiFi 使用量,但是当我尝试这样做时,我会得到一个非常大的值。即使在设备上执行出厂重置后,我的数据使用量值也非常大(重置设备后一分钟内使用 411970 MB 的数据似乎并不准确)

非常感谢任何建议!

来源:

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);

    }
}

【问题讨论】:

标签: java android networking wifi traffic-measurement


【解决方案1】:

你也在添加字节数

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

所以你将两个数字写在一起,中间没有空格或任何分隔符(第一个字节数,然后是 MB 数)。

使用括号来聚合操作

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

【讨论】:

  • 快速提问——如果我需要从总使用量中减去 3g/4g 数据使用量(以确定具体的 wifi 使用量),我想我会使用以下方法——但这会导致异常大再次价值(你能帮忙吗?最后一个问题 - 我保证!) info += ("\tWifi Data Usage: " + ((TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes()) - ((TrafficStats.getMobileRxBytes() + TrafficStats .getMobileTxBytes())) / 1000000 + "MB"));
  • 再次,您没有对值的第一项进行分组。删除未使用的括号(例如第一个括号)并检查每个括号的关闭位置(大多数 IDE 在您单击打开的括号时会显示它)。
【解决方案2】:

这些是字节! 为了获得正确的结果,您必须像这样划分字节:

  • bytes / 1024 得到 KB
  • bytes / (1024 * 1024) 得到 MB
  • bytes / (1024 * 1024 * 1024) 得到 GB

【讨论】:

  • 不要学究气,它们分别是 KiB、MiB 和 GiB。
猜你喜欢
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多