2

Here is the code

Variable declared

 public String gpu2dcurent = "1234567";

Asynctask, after finished it should update variable gpu2dcurent, but it doesnt

 private class readgpu2d extends AsyncTask<String, Void, String> {


    protected String doInBackground(String... args) {
         Log.i("MyApp", "Background thread starting");

         String aBuffer = "";

         try {

            File myFile = new File("/sys/devices/platform/kgsl-2d0.0/kgsl/kgsl-2d0/gpuclk");
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(
                    new InputStreamReader(fIn));
            String aDataRow = "";
            //String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) {
                aBuffer += aDataRow + "\n";
            }

            ;
            myReader.close();

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();

        }

         return aBuffer.trim();
     }

     protected void onPostExecute(String result) {
         // Pass the result data back to the main activity
        gpu2dcurent = result;
         //Toast.makeText(getBaseContext(), result,
                //Toast.LENGTH_SHORT).show();
         gpu.this.data = result;

         if (gpu.this.pd != null) {
             //gpu.this.pd.dismiss();
         }
     }

    }

Test to see if a variable has a new value. It doesn't, it displays 1234567

 TextView tx = (TextView)findViewById(R.id.textView3);
tx.setText(gpu2dcurent);

Am i missing something? When i update textView from inside onPostExecute method it works fine but when Asynctask finishes variable value resets to default

 public class gpu extends Activity{

public String gpu2dcurent = "1234567";
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
pedja
  • 3,285
  • 5
  • 36
  • 48

4 Answers4

2

In gpu class declare it as followin:

public String gpu2dcurent = "1234567";

In AsyncTask class readgpu2d, use it as following:

gpu.gpu2dcurent = result;

Update your UI=User Interface means TextView in

onProgressUpdate()

method of AsyncTask.

And check where have you declared gpu2dcurent variable???
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
2

I have a doubt you are trying to set text of TextView before completion of your Asynctask.

Yes, either make it, static public String gpu2dcurent = "1234567";

Or, set Text of TextView in onPostExecute()

protected void onPostExecute(String result) {
         // Pass the result data back to the main activity
        gpu2dcurent = result;
         //Toast.makeText(getBaseContext(), result,
                //Toast.LENGTH_SHORT).show();
         gpu.this.data = result;  
         if (gpu.this.pd != null) {
             //gpu.this.pd.dismiss();
         }
      tx.setText(gpu2dcurent);
     }
    }

Update:

After update your code in question,

change this line,

new readgpu2d().execute("blablabla");

to

new readgpu2d().execute("blablabla").get();
user370305
  • 108,599
  • 23
  • 164
  • 151
2

try to put setText into onPostExecute

protected void onPostExecute(String result) {
     TextView tx = (TextView)findViewById(R.id.textView3);
     tx.setText(result);

     // Pass the result data back to the main activity
    gpu2dcurent = result;
     //Toast.makeText(getBaseContext(), result,
     //Toast.LENGTH_SHORT).show();
     gpu.this.data = result;

     if (gpu.this.pd != null) {
         //gpu.this.pd.dismiss();
     }
 }
Ging3r
  • 2,010
  • 2
  • 17
  • 26
  • i dont need to update TextView, i dont need it, its just so i can see if variable has been changed or not, what i need is to update variable from within AsyncTask – pedja Aug 01 '12 at 09:57
0

Here is the whole code

 package rs.pedjaapps.DualCore;

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.List;
 import java.util.concurrent.TimeoutException;
 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Spinner;
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.stericson.RootTools.RootTools;
 import com.stericson.RootTools.RootToolsException;

 public class gpu extends Activity{

public String gpu2dcurent = "1234567";

private ProgressDialog pd = null;
private Object data = null;

 private class readgpu2d extends AsyncTask<String, Void, String> {


    protected String doInBackground(String... args) {
         Log.i("MyApp", "Background thread starting");

         String aBuffer = "";

         try {

            File myFile = new File("/sys/devices/platform/kgsl-2d0.0/kgsl/kgsl-2d0/gpuclk");
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(
                    new InputStreamReader(fIn));
            String aDataRow = "";
            //String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) {
                aBuffer += aDataRow + "\n";
            }

            //gpu2dcurent = aBuffer.trim();
            myReader.close();




        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();

        }




         return aBuffer.trim();
     }

     protected void onPostExecute(String result) {
         // Pass the result data back to the main activity
        gpu2dcurent = result;
         //Toast.makeText(getBaseContext(), result,
                //Toast.LENGTH_SHORT).show();
         gpu.this.data = result;

         if (gpu.this.pd != null) {
             gpu.this.pd.dismiss();
         }

     }

    }




    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gpu);
this.pd = ProgressDialog.show(this, "Working..", "Loading...", true, false);
new readgpu2d().execute("blablabla");
TextView tx = (TextView)findViewById(R.id.textView3);
tx.setText(gpu2dcurent);
 }


}
pedja
  • 3,285
  • 5
  • 36
  • 48