getUidTxBytes(int uid)在Android 6.0中始终返回0 [英] getUidTxBytes(int uid) always return 0 in android 6.0

查看:134
本文介绍了getUidTxBytes(int uid)在Android 6.0中始终返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取所有应用程序的网络流量统计信息.我只打印设备中每个应用程序的总网络流量.该代码在android 4.4和5.1设备上工作正常,但在android 6.0设备中,对于所有应用程序,它始终返回0.任何人都可以告诉我为什么这会在android 6.0设备中发生.

I am trying to get network traffic stats of all apps. I just print total network traffic of every application in my device. The code is working fine in android 4.4 and 5.1 devices but in android 6.0 device it always return 0 for all applications. Anyone can please tell me why this happened in android 6.0 devices.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    for(ApplicationInfo app : getPackageManager().getInstalledApplications(0)){
        long tx = TrafficStats.getUidTxBytes(app.uid);
        long rx = TrafficStats.getUidRxBytes(app.uid);
        long total = tx + rx;
        Log.e("total data of ", app.packageName + " = " + total);
    }
}

这是我的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mts.trafficstatsdemo">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

推荐答案

根据 doc :

从N开始,它将仅报告呼叫的流量统计信息 UID.它将为所有其他UID返回UNSUPPORTED,以保护隐私 原因.访问属于其他人的历史网络统计信息 UID,请使用NetworkStatsManager.

Starting in N this will only report traffic statistics for the calling UID. It will return UNSUPPORTED for all other UIDs for privacy reasons. To access historical network statistics belonging to other UIDs, use NetworkStatsManager.

这篇关于getUidTxBytes(int uid)在Android 6.0中始终返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆