我正在尝试获取所有应用程序的网络流量统计信息。我只是打印设备中每个应用程序的总网络流量。该代码在 android 4.4 和 5.1 设备中运行良好,但在 android 6.0 设备中它始终为所有应用程序返回 0。任何人都可以告诉我为什么在 android 6.0 设备中发生这种情况。
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 https://developer.android.com/reference/android/net/TrafficStats.html:
从 N 开始,这只会报告呼叫的流量统计信息
UID。出于隐私考虑,它将为所有其他 UID 返回 UNSUPPORTED
原因。访问属于其他人的历史网络统计数据
UID,使用 NetworkStatsManager。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)