[英]How to get Network data used by running apps Individually in android
I want to get network traffic data of each app only which is running in background after a fixed interval of time. 我只想获取固定时间间隔后在后台运行的每个应用程序的网络流量数据。 The code I am using gives the total data transmit & receive from the device.
我使用的代码给出了从设备发送和接收的全部数据。 But I need individual data of each app.
但是我需要每个应用程序的单独数据。 please help me to get the data.
请帮助我获取数据。
My code is: 我的代码是:
public class TrafficMonitorActivity extends Activity {
TextView latest_rx=null;
TextView latest_tx=null;
TextView previous_rx=null;
TextView previous_tx=null;
TextView delta_rx=null;
TextView delta_tx=null;
TrafficSnapshot latest=null;
TrafficSnapshot previous=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_traffic_monitor);
latest_rx=(TextView)findViewById(R.id.latest_rx);
latest_tx=(TextView)findViewById(R.id.latest_tx);
previous_rx=(TextView)findViewById(R.id.previous_rx);
previous_tx=(TextView)findViewById(R.id.previous_tx);
delta_rx=(TextView)findViewById(R.id.delta_rx);
delta_tx=(TextView)findViewById(R.id.delta_tx);
takeSnapshot(null);
}
public void takeSnapshot(View v) {
previous=latest;
latest=new TrafficSnapshot(this);
latest_rx.setText(String.valueOf(latest.device.rx));
latest_tx.setText(String.valueOf(latest.device.tx));
if (previous!=null) {
previous_rx.setText(String.valueOf(previous.device.rx));
previous_tx.setText(String.valueOf(previous.device.tx));
delta_rx.setText(String.valueOf(latest.device.rx-previous.device.rx));
delta_tx.setText(String.valueOf(latest.device.tx-previous.device.tx));
}
ArrayList<String> log=new ArrayList<String>();
HashSet<Integer> intersection=new HashSet<Integer>(latest.apps.keySet());
if (previous!=null) {
intersection.retainAll(previous.apps.keySet());
}
for (Integer uid : intersection) {
TrafficRecord latest_rec=latest.apps.get(uid);
TrafficRecord previous_rec=
(previous==null ? null : previous.apps.get(uid));
emitLog(latest_rec.tag, latest_rec, previous_rec, log);
}
Collections.sort(log);
for (String row : log) {
Log.d("TrafficMonitor", row);
}
}
private void emitLog(CharSequence name, TrafficRecord latest_rec,
TrafficRecord previous_rec,
ArrayList<String> rows) {
if (latest_rec.rx>-1 || latest_rec.tx>-1) {
StringBuilder buf=new StringBuilder(name);
buf.append("=");
buf.append(String.valueOf(latest_rec.rx));
buf.append(" received");
if (previous_rec!=null) {
buf.append(" (delta=");
buf.append(String.valueOf(latest_rec.rx-previous_rec.rx));
buf.append(")");
}
buf.append(", ");
buf.append(String.valueOf(latest_rec.tx));
buf.append(" sent");
if (previous_rec!=null) {
buf.append(" (delta=");
buf.append(String.valueOf(latest_rec.tx-previous_rec.tx));
buf.append(")");
}
rows.add(buf.toString());
}
}
}
Another class is: TrafficRecord 另一类是:TrafficRecord
class TrafficRecord {
long tx=0;
long rx=0;
String tag=null;
TrafficRecord() {
tx=TrafficStats.getTotalTxBytes();
rx=TrafficStats.getTotalRxBytes();
}
TrafficRecord(int uid, String tag) {
tx=TrafficStats.getUidTxBytes(uid);
rx=TrafficStats.getUidRxBytes(uid);
this.tag=tag;
}
} }
TrafficSnapshot: TrafficSnapshot:
class TrafficSnapshot {
TrafficRecord device=null;
HashMap<Integer, TrafficRecord> apps=
new HashMap<Integer, TrafficRecord>();
TrafficSnapshot(Context ctxt) {
device=new TrafficRecord();
HashMap<Integer, String> appNames=new HashMap<Integer, String>();
for (ApplicationInfo app :
ctxt.getPackageManager().getInstalledApplications(0)) {
appNames.put(app.uid, app.packageName);
}
for (Integer uid : appNames.keySet()) {
apps.put(uid, new TrafficRecord(uid, appNames.get(uid)));
}
}
}
How can I get individual app data with correspond to app name? 如何获取与应用名称相对应的单个应用数据?
I know this is perhaps old thread, but you could use getUidRxBytes(UID); 我知道这也许是旧线程,但是您可以使用getUidRxBytes(UID); getUidTxBytes(UID);
getUidTxBytes(UID);
See this link for example: How can I check my Android app's network consumption? 例如,请参见以下链接: 如何检查Android应用程序的网络消耗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.