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: