Cannot resolve symbol 'networkStatsManager' [networkStatsManager]

160 views Asked by At

I'm writing a small part of an app that looks up the txBytes for an Uid using networkstatsManager for android. However, I'm getting an error while trying to use the networkStatsManager. Android Studio keeps giving me the error "Cannot resolve symbol 'networkStatsManager'". I'm not sure what I'm doing wrong. Any Advice would be very appreciated.

This is the line that continues to give me an error: networkStats = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI, "", 0, System.currentTimeMillis(),stat); Line 13.

Here is my code:

PackageManager packageManager = getPackageManager();

    ApplicationInfo info = null;
    try {
        info = packageManager.getApplicationInfo(packname, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    int stat = info.uid;

    NetworkStats networkStats = null;
    NetworkStats.Bucket bucket = new NetworkStats.Bucket();
    networkStats = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI, "", 0, System.currentTimeMillis(),stat);

    long txBytes = 0L;
    while (networkStats.hasNextBucket()) {
        networkStats.getNextBucket(bucket);
        txBytes += bucket.getTxBytes();
        TextView textView2 = (TextView) findViewById(R.id.test2);
        textView2.setText(String.valueOf(txBytes));
    }
    networkStats.close();
1

There are 1 answers

1
Jameido On BEST ANSWER

You're getting this error because networkStatsManager hasn't been declared and initialised

NetworkStats networkStats = null;
NetworkStats.Bucket bucket = new NetworkStats.Bucket();

//this is the missing line
NetworkStatsManager networkStatsManager = context.getSystemService(NetworkStatsManager.class)

networkStats = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI, "", 0, System.currentTimeMillis(),stat);