Iqbal Singh
Iqbal Singh

Reputation: 115

TrafficStats.getUidRxBytes() giving 0 for all processes

I make this program and my TrafficStats.getUidRxBytes() methods are giving me 0 all the time. I check with my apps and the have data usage records, but my code is not showing the data usage amount that is received and send data amounts. Also, when i print this " System.out.println("Recieved Bytes: "+re +"\n"+ "Send Bytes: "+sd+"\n"); " then logcat shows that it is printed for only first pid all the times.

Any solution for this problem???

Here is my MainActivity class:-

public class MainActivity extends Activity {

static ArrayList<Integer> arr1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


TextView textView1 = (TextView) findViewById(R.id.textView1);

//this.setContentView(textView1);        


ActivityManager actvityManager = (ActivityManager)
        this.getSystemService( ACTIVITY_SERVICE );
        List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        for(int i = 0; i < procInfos.size(); i++)
        {
                   textView1.setText(textView1.getText().toString()+procInfos.get(i).processName+" "+procInfos.get(i).pid+ 
                            " " +    String.valueOf(procInfos.get(i).processName.length())+"\n");
                //if(procInfos.get(i).processName.equals("com.android.camera")) {
                    //Toast.makeText(getApplicationContext(), "Camera App is running", Toast.LENGTH_LONG).show();
                //}
                a1.add(procInfos.get(i).pid);


        }

        TextView textView2 = (TextView) findViewById(R.id.textView2);
        TextView textView3 = (TextView) findViewById(R.id.textView3);
        for(Integer a2 : a1){
            long re = TrafficStats.getUidRxBytes(a2);
            long sd = TrafficStats.getUidTxBytes(a2);

            //arr1.add(a2);

            System.out.println("Recieved Bytes: "+re/1000 + "Send Bytes: "+sd/1000);


            textView2.append(""+Long.toString(re));
            textView3.append("ABAABABBA");
            textView3.invalidate();

        }

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

Upvotes: 1

Views: 951

Answers (1)

android developer
android developer

Reputation: 116412

This is a known bug, which some people claim that got fixed on Android 4.4 .

Here's a way to handle it, and here's where this issue got reported.

Basically, what you need to do is to parse the files located on "/proc/uid_stat/" . each folder there the name of the UID of the applicationInfo (of each app). First row is the number of bytes received, and second row is the number of bytes sent.

Upvotes: 1

Related Questions