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);
}
}
Here's my 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>
According to 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With