Mauro Esposito
Lurker
I want to get the mobile data consumption for a given range of date (1, 2, 5, 10 or 15 minutes) with the querySummaryForDevice method of NetworkStatsManager.
This is my code:
I tried to convert my date in calendar and back again in Date with this code:
This is the fromDate and toDate:
But totalBytes returned by getTotalTrafficInRange it's not real. If I have my phone in WIFI for 1 hour and then call my function with fromDate 15 minute ago, it return a very long value and not 0 as expected.
There is a limit for the range date of the querySummaryForDevice method of NetworkStatsManager? No clue found in documentation.
Anyone notice this weird behaviour?
This is my code:
Java:
public static long getTotalTrafficInRange(Context context, Date fromDate, Date toDate) throws UsageStatsNotActiveException, NoPermissionException
{
boolean usageStatsActive = doIHavePermission(context);
if (!usageStatsActive) {
throw new UsageStatsNotActiveException(context);
}
long total = 0;
TelephonyManager tele = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
throw new NoPermissionException(Manifest.permission.READ_PHONE_STATE);
}
String actualSubscriberId = tele.getSubscriberId();
NetworkStatsManager networkStatsManager = (NetworkStatsManager) context.getSystemService(Context.NETWORK_STATS_SERVICE);
Date from = ConvertDateToCalendar(fromDate);
Date to = ConvertDateToCalendar(toDate);
//Total
NetworkStats.Bucket bucket;
try {
bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_MOBILE, actualSubscriberId, from.getTime(), to.getTime());
if(bucket != null){
total = (bucket.getRxBytes() + bucket.getTxBytes());
}
} catch (RemoteException e) {
e.printStackTrace();
}
return total;
}
I tried to convert my date in calendar and back again in Date with this code:
Java:
public static Date ConvertDateToCalendar(Date date)
{
Calendar fromDateCalendar = Calendar.getInstance();
fromDateCalendar.setTime(date);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, fromDateCalendar.get(Calendar.YEAR));
calendar.set(Calendar.MONTH, fromDateCalendar.get(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH, fromDateCalendar.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, fromDateCalendar.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, fromDateCalendar.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, fromDateCalendar.get(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, fromDateCalendar.get(Calendar.MILLISECOND));
return calendar.getTime();
}
This is the fromDate and toDate:
Java:
//this date is new Date() minus 1,2,3,5,10 or 15 minutes, not more then this values
fromDate = new Date(PersistentHelper.getLastDateUsedForCalculateData(context));
//Calculate toDate
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
if(!userChangeDateManually || !userChangeZone) {
calendar.set(Calendar.SECOND, 0);
}
calendar.add(Calendar.MILLISECOND, -1);
toDateWS = calendar.getTime();
But totalBytes returned by getTotalTrafficInRange it's not real. If I have my phone in WIFI for 1 hour and then call my function with fromDate 15 minute ago, it return a very long value and not 0 as expected.
There is a limit for the range date of the querySummaryForDevice method of NetworkStatsManager? No clue found in documentation.
Anyone notice this weird behaviour?