gpt4 book ai didi

java - 如何获取Android中单独运行的应用程序使用的网络数据

转载 作者:行者123 更新时间:2023-12-01 13:10:30 26 4
gpt4 key购买 nike

我只想获取每个在后台运行的应用程序的网络流量数据在固定的时间间隔后。我正在使用的代码给出了设备传输和接收的总数据量。但我需要每个应用程序的单独数据。请帮我获取数据。

我的代码是:

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());
}
}
}

另一个类是: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;
}

}

交通快照:

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)));
}
}
}

如何获取与应用名称相对应的单个应用数据?

最佳答案

我知道这可能是旧线程,但你可以使用getUidRxBytes(UID);getUidTxBytes(UID);

请参阅此链接例如: How can I check my Android app's network consumption?

关于java - 如何获取Android中单独运行的应用程序使用的网络数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22904170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com