Back to project page Emmagee.
The source code is released under:
Apache License
If you think the Android project Emmagee listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (c) 2012-2013 NetEase, Inc. and other contributors *// ww w. j ava2s . com * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.netease.qa.emmagee.utils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import android.util.Log; public class TrafficInfo { private static final String LOG_TAG = "Emmagee-" + TrafficInfo.class.getSimpleName(); private String uid; public TrafficInfo(String uid){ this.uid = uid; } /** * get total network traffic, which is the sum of upload and download traffic * * @return total traffic include received and send traffic */ public long getTrafficInfo() { Log.i(LOG_TAG,"get traffic information"); String rcvPath = "/proc/uid_stat/" + uid + "/tcp_rcv"; String sndPath = "/proc/uid_stat/" + uid + "/tcp_snd"; long rcvTraffic = -1; long sndTraffic = -1; try { RandomAccessFile rafRcv = new RandomAccessFile(rcvPath, "r"); RandomAccessFile rafSnd = new RandomAccessFile(sndPath, "r"); rcvTraffic = Long.parseLong(rafRcv.readLine()); sndTraffic = Long.parseLong(rafSnd.readLine()); } catch (FileNotFoundException e) { rcvTraffic = -1; sndTraffic = -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(); } if (rcvTraffic == -1 || sndTraffic == -1) { return -1; } else return (rcvTraffic + sndTraffic); } }