gpt4 book ai didi

java - 我得到了在 networkstatsmanager.querydetailsforuid() 方法中提供的不同的 startTimestamp 和 endtimestamp 。怎么解决呢?

转载 作者:行者123 更新时间:2023-12-02 08:40:18 43 4
gpt4 key购买 nike

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

我想获取开始时间和结束时间间隔内的应用程序数据使用情况我正在调用方法并以毫秒为单位传递日历中的开始日期和结束日期,但在调用 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;

关于java - 我得到了在 networkstatsmanager.querydetailsforuid() 方法中提供的不同的 startTimestamp 和 endtimestamp 。怎么解决呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61421101/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com