gpt4 book ai didi

android - getUidTxBytes(int uid) 在 android 6.0 中总是返回 0

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:46 25 4
gpt4 key购买 nike

我正在尝试获取所有应用程序的网络流量统计信息。我只是打印设备中每个应用程序的总网络流量。该代码在 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 :

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.

关于android - getUidTxBytes(int uid) 在 android 6.0 中总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406551/

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