4

Is it possible to create a application that gets network activity / statistics while running as a background process? Or just make a application that listen to traffic on a specific port?

Thanks M

Mikael
  • 5,429
  • 5
  • 30
  • 38

2 Answers2

3

It maybe hit and miss. But most devices I have used have the /proc file system. You should be able to get the stats you need out of one of the /proc/net/ entries. For example to get a netstat:

Process proc = Runtime.getRuntime().exec(new String[] {"cat", "/proc/net/netstat"});
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));  
String line = null;  

while ((line = reader.readLine()) != null)  
{
  // parse netstat output here
}
Nic Strong
  • 6,532
  • 4
  • 35
  • 50
  • What should I put there to parse the data? How can I check the total download&upload rate (globally and/or of a specific app) , for example? – android developer Sep 01 '16 at 13:55
-1

No need to run "cat /proc/net/netstat" command. Instead a simpler approach can solve the problem

Process proc = Runtime.getRuntime().exec(new String[] {"netstat"});
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));  
String line = null;  
while ((line = reader.readLine()) != null)  {
// parse netstat output here
}
Ankit
  • 6,554
  • 6
  • 49
  • 71