Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

红米2和华为荣耀流量不好使 #40

Closed
salomon1184 opened this issue Mar 18, 2015 · 2 comments
Closed

红米2和华为荣耀流量不好使 #40

salomon1184 opened this issue Mar 18, 2015 · 2 comments

Comments

@salomon1184
Copy link

public long getTrafficInfo() {

    RandomAccessFile rafTaffic = null;
    long traffic = 0;

    try {
        rafTaffic = new RandomAccessFile("/proc/net/xt_qtaguid/stats", "r");
        Log.i(LOG_TAG, "get traffic information");

        String line = "";
        while ((line = rafTaffic.readLine()) != null) {
            Log.d(LOG_TAG, "line===" + line);
            String[] linesplits = line.split(" ");
            Log.d(LOG_TAG, "uid===" + uid);
            Log.d(LOG_TAG, linesplits[3]);
            if (linesplits[3].endsWith(uid)) {
                traffic = traffic + Long.parseLong(linesplits[5])
                        + Long.parseLong(linesplits[7]);
            }
        }

    } catch (FileNotFoundException e) {
        traffic = -1;

    } catch (NumberFormatException e) {
        Log.e(LOG_TAG, "NumberFormatException: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e(LOG_TAG, "IOException: " + e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (rafTaffic != null)
                rafTaffic.close();
        } catch (IOException e) {
            Log.i(LOG_TAG,
                    "close randomAccessFile exception: " + e.getMessage());
        }
    }

    Log.d(LOG_TAG, "Traffic===" + traffic);
    return traffic;
}
@salomon1184
Copy link
Author

应该是4.4以后都不好使了,/proc/uid_stat/" + uid路径不存在,访问不到

改为从/proc/net/xt_qtaguid/stats取数据,个人认为这得数据比上边那个准确

@andrewleo
Copy link
Member

首先十分感谢反馈,帮我们发现了Emmagee在高版本上流量获取的问题!

我们也看了源码,发现在 4.3.1 及之后的版本,确实如你所说,流量的统计都放到了/proc/net/xt_qtaguid/stats这个文件下,流量需要通过读取这个文件获取。

不过 Android API8 以后提供了getUidRxBytes以及getUidTxBytes方法,底层调用也是读取这个文件。所以通过这个方法更简单而且也同样准确,使用该方法获取的数据和你的算法应该是相同的。我们考虑下个版本使用该API来获取流量。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants