TrafficStats.getUidRxBytes()所有进程给予0 [英] TrafficStats.getUidRxBytes() giving 0 for all processes

查看:439
本文介绍了TrafficStats.getUidRxBytes()所有进程给予0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做这个节目,我的TrafficStats.getUidRxBytes()方法给我0所有的时间。我检查我的应用程序和数据有使用记录,但我的code未显示,接收和发送数据量的数据使用量。
此外,当我打印本的System.out.println(收到字节:+重+\\ n+发送字节:+ SD +\\ n);然后logcat中表明,它打印的第一只PID所有的时间。

有关这个问题的任何解决方案?

下面是我的MainActivity类别: -

 公共类MainActivity延伸活动{静态的ArrayList<整数GT; ARR1;
@覆盖
保护无效的onCreate(捆绑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);
        清单< RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
        ArrayList的<整数GT; A1 =新的ArrayList<整数GT;();
        的for(int i = 0; I< procInfos.size();我++)
        {
                   textView1.setText(textView1.getText()。的toString()+ procInfos.get(I).processName ++ procInfos.get(我).pid +
                            +将String.valueOf(procInfos.get(I).processName.length())+\\ n);
                //if(procInfos.get(i).processName.equals(\"com.android.camera)){
                    //Toast.makeText(getApplicationContext(),摄像头应用程序正在运行,Toast.LENGTH_LONG).show();
                //}
                a1.add(procInfos.get㈠.pid);
        }        TextView的textView2 =(的TextView)findViewById(R.id.textView2);
        TextView的textView3 =(的TextView)findViewById(R.id.textView3);
        对于(整数A2:A1){
            长重= TrafficStats.getUidRxBytes(A2);
            长SD = TrafficStats.getUidTxBytes(A2);            //arr1.add(a2);            的System.out.println(收到字节:+ RE / 1000 +发送字节:+ SD / 1000);
            textView2.append(+ Long.toString(重新));
            textView3.append(ABAABABBA);
            textView3.invalidate();        }    }@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}


解决方案

这是一个已知的bug,其中一些人声称,得到固定的Andr​​oid 4.4系统。

这里 一种方法来处理它,<一个href=\"https://$c$c.google.com/p/android/issues/detail?id=58210&q=label%3aReportedBy-Developer&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars\"相对=nofollow> 这里 在哪里这个问题得到了报道。

基本上,你需要做的是解析位于的/ proc / uid_stat /的文件。每个文件夹那里applicationInfo的UID(每个应用程序)的名称。第一行是接收的字节数,以及第二行是发送的字节数。

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

解决方案

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.

这篇关于TrafficStats.getUidRxBytes()所有进程给予0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆