我正在尝试使用 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);
}
}