Skip to content
Advertisement

I am getting different startTimestamp and endtimestamp that i provided in networkstatsmanager.querydetailsforuid() method. How to solve it?

Getting different start time and end time that I am providing in NetworkStatsManager.queryDetailsForUid Here is my code snippet :

I want to get application data usage in an interval of start time and end time I am calling method and passing start date and end date from calendar in milliseconds but after calling to the queryDetailsForUid() it showing some different time:

Calendar cal1 = Calendar.getInstance();
Date end = cal1.getTime();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -100);
long start = cal.getTime().getTime();

long s = getPackageTxBytesMobile(uid, cal.getTimeInMillis(), cal1.getTimeInMillis());
long r = getPackageRxBytesMobile(uid, cal.getTimeInMillis(), cal1.getTimeInMillis());

Here is the method:

public long getPackageTxBytesMobile(int uid, long startDate, long endDate) {
    NetworkStats networkStats = null;

    NetworkStatsManager networkStatsManager = (NetworkStatsManager) 
    context1.getSystemService(Context.NETWORK_STATS_SERVICE);
    TelephonyManager tm = (TelephonyManager)context1.getSystemService(Context.TELEPHONY_SERVICE);
    String subId = null;
    if(ActivityCompat.checkSelfPermission(context1, Manifest.permission.READ_PHONE_STATE) ==
            PackageManager.PERMISSION_GRANTED) {
        subId = tm.getSubscriberId();
    }
    Log.d("Startdate: ",""+startDate+"   ");
    Log.d("Enddate: ",""+endDate+"    ");
    try {
        networkStats = networkStatsManager.queryDetailsForUid(
                ConnectivityManager.TYPE_MOBILE,
                subId,
                startDate,
                endDate ,
                uid);
    } catch (Exception e) {
        return -1;
    }
    NetworkStats.Bucket bucket = new NetworkStats.Bucket();
    networkStats.getNextBucket(bucket);
    long tx = bucket.getTxBytes();
    Log.d("tag1","StartTime: "+bucket.getStartTimeStamp());
    Log.d("tag1","EndTime: "+bucket.getEndTimeStamp());

    networkStats.close();
    return tx;
}

and here is log output:

2020-04-25 10:08:47.248 17889-17889/com.pkdev.mdmtemp D/tag1: StartTime: 1579168800000
2020-04-25 10:08:47.248 17889-17889/com.pkdev.mdmtemp D/tag1: EndTime: 1579176000000
2020-04-25 10:08:47.280 17889-17889/com.pkdev.mdmtemp D/Startdate:: 1579149527275   
2020-04-25 10:08:47.280 17889-17889/com.pkdev.mdmtemp D/Enddate:: 1587789527275    
2020-04-25 10:08:47.284 17889-17889/com.pkdev.mdmtemp D/tag1: StartTime: 1579154400000
2020-04-25 10:08:47.284 17889-17889/com.pkdev.mdmtemp D/tag1: EndTime: 1579161600000
2020-04-25 10:08:47.324 17889-17889/com.pkdev.mdmtemp D/Startdate:: 1579149527321   
2020-04-25 10:08:47.324 17889-17889/com.pkdev.mdmtemp D/Enddate:: 1587789527320    
2020-04-25 10:08:47.326 17889-17889/com.pkdev.mdmtemp D/tag1: StartTime: 1582466400000
2020-04-25 10:08:47.326 17889-17889/com.pkdev.mdmtemp D/tag1: EndTime: 1582473600000
2020-04-25 10:08:47.360 17889-17889/com.pkdev.mdmtemp D/Startdate:: 1579149527357   
2020-04-25 10:08:47.360 17889-17889/com.pkdev.mdmtemp D/Enddate:: 1587789527357    
2020-04-25 10:08:47.365 17889-17889/com.pkdev.mdmtemp D/tag1: StartTime: 1579161600000
2020-04-25 10:08:47.365 17889-17889/com.pkdev.mdmtemp D/tag1: EndTime: 1579168800000

Advertisement

Answer

You are using just one NetworkStats.Bucket, use hasNextBucket() to get the following buckets too.

So change your method’s bottom part to this:

long tx = 0L
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
while (networkStats.hasNextBucket()) {
    networkStats.getNextBucket(bucket);
    tx += bucket.getTxBytes();
    Log.d("tag1","StartTime: "+bucket.getStartTimeStamp());
    Log.d("tag1","EndTime: "+bucket.getEndTimeStamp());
}

networkStats.close();
return tx;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement