2014-04-07 3 views
0

고정 간격으로 백그라운드에서 실행중인 각 앱의 네트워크 트래픽 데이터를 백그라운드에서 으로 가져 오려고합니다. 코드를 사용하면 총 데이터 전송 장치에서 &을 전송합니다. 하지만 각 앱의 개별 데이터가 필요합니다. 데이터를 얻도록 도와주세요.앱을 실행하여 네트워크 데이터를 얻는 방법 android에서 개별적으로

내 코드는 다음과 같습니다

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

}

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

가 어떻게 대응 개별 응용 프로그램 데이터를 얻을 수 있습니다 앱 이름에?

+0

이 튜토리얼을 시도해보십시오 http://www.techrepublic.com/blog/software-engineer/create-a-network-monitor-using-androids-trafficstats-class를/774/ –

+0

또한이 방법을 시도해보십시오. 그러나 모든 응용 프로그램의 전체 데이터를 제공합니다. – Khushboo

+0

각 응용 프로그램에서 사용하는 개별 데이터가 필요합니다. – Khushboo

답변