Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getUidTxBytes(int uid) always return 0 in 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);
    }
}

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>
like image 735
Faheem Raza Avatar asked Dec 31 '16 07:12

Faheem Raza


1 Answers

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.

like image 110
Oleg Bogdanov Avatar answered Nov 10 '22 22:11

Oleg Bogdanov