-1

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.

salih kallai
  • 879
  • 2
  • 13
  • 34
Iqbal Singh
  • 95
  • 1
  • 10

6 Answers6

0

You need to call the method invalidate() after each setText("") to see the changes in the UI

Example:-

TextView text1.setText("dummy text");
text1.invalidate();

I hope it helps

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
0

Move

TextView textView2 = (TextView) findViewById(R.id.textView2);
TextView textView3 = (TextView) findViewById(R.id.textView3);

outside your for loop. No need to use findViewById() in each iteration.

By the way, better to use Log.d() than System.out.println() in Android.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
0

Try This:

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.setText(textView2.getText().toString()+Long.toString(re));
    textView3.setText("ABAABABBA");

}
Vamshi
  • 1,495
  • 1
  • 15
  • 31
0

Try this, in every new iteration of loop you are creating new textview, so initalize both textviews outside the loop

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

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

it may help

Anil kumar
  • 1,534
  • 3
  • 14
  • 20
0
TextView textView2 = (TextView) findViewById(R.id.textView2);
TextView textView3 = (TextView) findViewById(R.id.textView3);

is not in right place, just move them to this first line. TextView textView1 = (TextView) findViewById(R.id.textView1);

Alex
  • 51
  • 1
  • 3
0

Thanks Everyone for this quick response. In actual, all the textViews were placed so far from each other thats why i was not able to see all the values and also i used

textView3.append(Long.toString(sd)+"KB"); 

to show the values.

ann
  • 576
  • 1
  • 10
  • 19
Iqbal Singh
  • 95
  • 1
  • 10