Java 如何在android中获取单独运行应用程序所使用的网络数据
我只想获取在后台运行的每个应用程序的网络流量数据 经过一段固定的时间间隔。 我使用的代码给出了从设备发送和接收的总数据。 但我需要每个应用程序的单独数据。请帮我获取数据 我的代码是:Java 如何在android中获取单独运行应用程序所使用的网络数据,java,android,network-traffic,Java,Android,Network Traffic,我只想获取在后台运行的每个应用程序的网络流量数据 经过一段固定的时间间隔。 我使用的代码给出了从设备发送和接收的总数据。 但我需要每个应用程序的单独数据。请帮我获取数据 我的代码是: public class TrafficMonitorActivity extends Activity { TextView latest_rx=null; TextView latest_tx=null; TextView previous_rx=null; TextView previous_tx=null;
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());
}
}
}
}
流量快照:
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)));
}
}
}
类流量快照{
流量记录设备=空;
HashMap应用程序=
新的HashMap();
流量快照(上下文ctxt){
设备=新流量记录();
HashMap appNames=新HashMap();
对于(ApplicationInfo应用程序:
ctxt.getPackageManager().getInstalledApplication(0)){
appNames.put(app.uid,app.packageName);
}
for(整数uid:appNames.keySet()){
apps.put(uid,newtrafficrecord(uid,appNames.get(uid));
}
}
}
如何获取与应用程序名称对应的单个应用程序数据?我知道这可能是旧线程,但您可以使用 getUidRxBytes(UID); getUidTxBytes(UID) 例如,请参见此链接:
试试本教程:我也试过本教程,但该教程也提供了所有应用程序的总数据。我需要每个应用程序使用的单个数据。是否找到了解决问题的方法?
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)));
}
}
}