我只想获取在固定时间间隔后在后台运行的每个应用程序的网络流量数据。我使用的代码给出了从设备发送和接收的总数据。但我需要每个应用程序的个人数据。请帮我获取数据。
我的代码是:
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)));
}
}
}
如何获取与应用名称相对应的单个应用数据?