我制作了这个程序,我的TrafficStats.getUidRxBytes()方法一直给我0。我检查我的应用程序和有数据使用记录,但我的代码没有显示收到的数据使用量和发送数据量。 此外,当我打印这个“System.out.println(”收到的字节:“+ re +”\ n“+”发送字节:“+ sd +”\ n“);”然后logcat显示它只打印pid一直都是。
解决这个问题的任何方法???
这是我的MainActivity类: -
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;
}