android2.2 使用TrafficStats.getUidRxBytes(uid[n])获取流量返回-1

如题,我使用android2.2版本中的TrafficStats类中
static long getUidRxBytes(int uid) //获取某个网络UID的接受字节数
static long getUidTxBytes(int uid) //获取某个网络UID的发送字节数
两个方法获取手机中对于uid进程的接收和发送字节数时,返回的值一直是-1。在网上查资料说TrafficStsts类是从2.2版本才加进来的一个类,如果返回等于-1代表 UNSUPPORTED 当前设备不支持统计。我使用的2.2版本的虚拟机,请问这其中有什么问题吗?

getTotalTxBytes()和getTotalRxBytes()俩方法又有值返回,
getMobileTxBytes()和getMobileRxBytes()方法返回值也为-1.
还望高手指点

下面是我对应方法的代码

// TODO Auto-generated method stub
Map<Integer, String> users = new TreeMap<Integer, String>();
PackageManager packageManager = getPackageManager();
for (ApplicationInfo info : packageManager
.getInstalledApplications(0)) {
int uid = info.uid;
if (!users.containsKey(uid))
users.put(uid, packageManager.getApplicationLabel(info)
.toString());
// users.put(uid,
// packageManager.getNameForUid(uid).split(":")[0]);
}

long totalRx = 0;
long totalTx = 0;
int i = 1;
for (Entry<Integer, String> entry : users.entrySet()) {

int uid = entry.getKey();
String uidName = entry.getValue();

System.out.println("[" + i + "]" + "uid----->" + uid
+ "uidName------>" + uidName + "\n");
i++;
long recv = TrafficStats.getUidRxBytes(uid);
long sent = TrafficStats.getUidTxBytes(uid);
System.out.println(recv);
if (sent > 0) {
// Log.v("tmp", "uid=" + uidName + " tx=" + sent);
System.out.println("uid=" + uidName + " tx=" + sent);
totalTx += sent;
} else {
System.out.println("暂时还未产生发送流量!");
}
if (recv > 0) {
// Log.d("tmp", "uid=" + uidName + " rx=" + recv);
System.out.println("uid=" + uidName + " rx=" + recv);
totalRx += recv;
} else {
System.out.println("暂时还未产生接收流量!");
}
}
Log.i("tmp", "totalRx sum=" + totalRx + " api="
+ TrafficStats.getMobileRxBytes());
Log.i("tmp", "totalTx sum=" + totalTx + " api="
+ TrafficStats.getMobileTxBytes());

你可能感兴趣的:(虚拟机,android,String,api,网络,手机)