我得到了我在networkstatsmanager.querydetailsforuid()方法中提供的不同的start



获取我在NetworkStatsManager.queryDetailsForUid中提供的不同开始时间和结束时间这是我的代码片段:

我想在开始时间和结束时间的间隔内获取应用程序数据的使用情况我正在调用方法,并以毫秒为单位从日历中传递开始日期和结束日期,但在调用queryDetailsForUid()后,它显示了一些不同的时间:

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

方法如下:

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

这是日志输出:

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

您只使用一个NetworkStats.Bucket,也可以使用hasNextBucket()来获得以下存储桶。

因此,将您的方法的底部更改为:

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;

相关内容

  • 没有找到相关文章

最新更新