Following is my code.In this, I just get the list of all running processes and get their various attributes like process name, process id and I store process ids into an ArrayList
. Then,I found the data traffics used by all process ids using trafficstats()
.Now, i want to show these values in the TextView
. But, mu code is not showing these values.
Can anyone suggest me that were i am doing wrong?
I use 3 TextView
in activity_main.xml
.First TextView
is showing my values but another 2 are not working.
The whole code is in the MainActivity.java
class.
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);
}
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);
TextView textView2 = (TextView) findViewById(R.id.textView2);
TextView textView3 = (TextView) findViewById(R.id.textView3);
textView2.setText(textView2.getText().toString()+String.valueOf(re));
textView3.setText("ABAABABBA");
}
Here is the activity_main.xml class:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="98dp"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="93dp"
android:text="TextView" />
This is my whole MainActivity.java 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;
}
}
Thanks all the people who gave me suggestions.My problem is solved now. Thanks everyone for this quick response.