Android TrafficStats.getUidRxBytes()为所有进程提供0

Android TrafficStats.getUidRxBytes()为所有进程提供0,android,Android,我制作了这个程序,我的TrafficStats.getUidRxBytes()方法一直给我0。我检查我的应用程序和有数据使用记录,但我的代码没有显示接收和发送数据量的数据使用量。 另外,当我打印这个“System.out.println”(“接收字节:“+re+”\n“+”发送字节:“+sd+”\n”);”时,logcat显示它一直只打印第一个pid 这个问题有什么解决办法吗 这是我的主要活动课:- public class MainActivity extends Activity { st

我制作了这个程序,我的TrafficStats.getUidRxBytes()方法一直给我0。我检查我的应用程序和有数据使用记录,但我的代码没有显示接收和发送数据量的数据使用量。 另外,当我打印这个“System.out.println”(“接收字节:“+re+”\n“+”发送字节:“+sd+”\n”);”时,logcat显示它一直只打印第一个pid

这个问题有什么解决办法吗

这是我的主要活动课:-

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;
}
公共类MainActivity扩展活动{
静态数组列表arr1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView1=(TextView)findViewById(R.id.textView1);
//这个.setContentView(textView1);
ActivityManager actvityManager=(ActivityManager)
这个.getSystemService(活动\服务);
List procInfos=actvityManager.getrunningappprocesss();
ArrayList a1=新的ArrayList();
对于(int i=0;i
这是一个已知的bug,一些人声称它在Android 4.4上得到了修复

一种处理方法,以及该问题的报告地点


基本上,您需要做的是解析位于“/proc/uid_stat/”上的文件。其中的每个文件夹都包含(每个应用程序的)应用程序信息的uid名称。第一行是接收的字节数,第二行是发送的字节数。

您是在仿真器上还是在真实设备上运行此操作?