我开发了Android应用,它使用TrafficStats API获取后台服务中所有已安装应用的数据使用情况统计信息。本周早些时候,在三星Galaxy S5上,在软件更新后,即6.0.1,TrafficStats每个uid返回0。相同的应用程序完美适用于HUAWEI KIW-L21和OS 6.0.1。
我在API 23及更高版本中有一些使用NetworkStatsManager类的帖子,有些帖子提到这需要系统级权限。
我也浏览了NetworkStatsManager的文档。
任何人都可以成功实施NetworkStatsManager以获取总数据和应用程序数据吗?
如果有人真的帮助,请提前致谢。
答案 0 :(得分:2)
因此,我将同时显示使用 TrafficeStats 和 NetworkStatsManager
的两种方法我将获得手机中安装的UID的所有应用程序,并使用两种方式来处理数据使用情况。
您可以参考此作为一个很好的示例https://github.com/RobertZagorski/NetworkStats 前五种方法来自他的代码。
//when using NetworkStatsManager you need the subscriber id
private String getSubscriberId(Context context, int networkType) {
if (ConnectivityManager.TYPE_MOBILE == networkType) {
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSubscriberId();
}
return "";
}
// to get mobile data recived
public long getPackageRxBytesMobile(Context context,NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_MOBILE,
getSubscriberId(context, ConnectivityManager.TYPE_MOBILE),
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
networkStats.getNextBucket(bucket);
return bucket.getRxBytes();
}
// to get mobile data transmitted
public long getPackageTxBytesMobile(Context context,NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_MOBILE,
getSubscriberId(context, ConnectivityManager.TYPE_MOBILE),
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
return bucket.getTxBytes();
}
// to get wifi data received
public long getPackageRxBytesWifi(NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_WIFI,
"",
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
return bucket.getRxBytes();
}
// to get wifi data transmitted
public long getPackageTxBytesWifi(NetworkStatsManager networkStatsManager,int packageUid) {
NetworkStats networkStats = null;
networkStats = networkStatsManager.queryDetailsForUid(
ConnectivityManager.TYPE_WIFI,
"",
0,
System.currentTimeMillis(),
packageUid);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
return bucket.getTxBytes();
}
// print to log all the data usage value per application
public void printAllDataUsage(){
PackageManager pm = getPackageManager();
// get all the applications in the phone
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
NetworkStatsManager networkStatsManager = (NetworkStatsManager) getApplicationContext().getSystemService(Context.NETWORK_STATS_SERVICE);
for (ApplicationInfo packageInfo : packages) {
Log.d("MYLOG", String.valueOf(packageInfo.uid));
Log.d("MYLOG", String.valueOf(packageInfo.name));
Log.d("MYLOG", String.valueOf(packageInfo.packageName));
// get data usage from trafficStats
Log.d("MYLOG", String.valueOf(TrafficStats.getUidRxBytes(packageInfo.uid)));
Log.d("MYLOG", String.valueOf(TrafficStats.getUidTxBytes(packageInfo.uid)));
// get data usage from networkStatsManager using mobile
Log.d("MYLOG", String.valueOf(getPackageRxBytesMobile(this,networkStatsManager,packageInfo.uid)));
Log.d("MYLOG", String.valueOf(getPackageTxBytesMobile(this,networkStatsManager,packageInfo.uid)));
// get data usage from networkStatsManager using wifi
Log.d("MYLOG", String.valueOf(getPackageRxBytesWifi(networkStatsManager,packageInfo.uid)));
Log.d("MYLOG", String.valueOf(getPackageTxBytesWifi(networkStatsManager,packageInfo.uid)));
}
}