开发者

how to update textview using thread?

开发者 https://www.devze.com 2023-03-08 10:53 出处:网络
I am trying to update a textview after 3 seconds, and i want to do that in a loop for which i am using thread .....what should i do in order to start background thread so that exactly after 3 seconds

I am trying to update a textview after 3 seconds, and i want to do that in a loop for which i am using thread .....what should i do in order to start background thread so that exactly after 3 seconds background1 thread starts ... and it should be done in a loop ?

     package com.edu.math;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.widget.TextView;

    public class EduMath extends Activity {
        TextView txt;
        TextView tv;
        TextView num;
        String x;
        int time = 3000;
        float z ;


    int random;
            int random1;
            int random_operator =1;

    /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            txt=(TextView)findViewById(R.id.txt);
            tv = (TextView)findViewById(R.id.randomNumber);

            background.start();
        }

        // our handler
        Handler handler = new Handler() {
        public void handleMessage(Message msg) {//display each item in a single line

            random = (int)Math.ceil(Math.random()*10);
            random1 = (int)Math.ceil(Math.random()*10);

            if(random1>random)
            {
                txt.setText(/*txt.getText()+*/""+random1+" "+>+" "+random+" "+
System.getProperty("line.separator"));
            }
            else
            {
 开发者_开发问答               txt.setText(/*txt.getText()+*/""+random+" "+>+" "+random1+
System.getProperty("line.separator"));
            }
        }
    };

    Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {//display each item in a single line


                txt.setText(/*txt.getText()+*/""+random1+" "+x+" "+random+System.getProperty("line.separator"));

        }
    };
    Thread background1 =new Thread(new Runnable() {

        @Override
        public void run() {

            for(int i=0;i<10;i++)
            {
                try {

                    Thread.sleep(time);
                    // send message to the handler with the current message handler          

                    handler1.sendMessage(handler1.obtainMessage());
                    background.stop();
                    Log.d("EduMath-enclosing_method", "thread started");
                } catch (Exception e) {
                    Log.v("Error", e.toString());
                }
            }

        }
    });


    Thread background=new Thread(new Runnable() {

        @Override
        public void run() {
            for(int i=0;i<10;i++)
            {
                try {
                    Thread.sleep(time);

        // send message to the handler with the current message handler          
                    handler.sendMessage(handler.obtainMessage());
                                        background1.start();
                } catch (Exception e) {
                    Log.v("Error", e.toString());
                }
            }
             }
    });
}


You must use AsyncTask for changing any views in layout, but at now I have very little experience with AsyncTask.


Try to use a java.util.Timer ( http://download.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html )


public class HandlerDemo extends Activity {

ProgressBar bar;

Handler handler=new Handler() {
@Override
public void handleMessage(Message msg) {
    //add your text
}
};

AtomicBoolean isRunning=new AtomicBoolean(false);


public void onStart() {
   super.onStart();

   Thread background=new Thread(new Runnable() {
   public void run() {
    try {

           Thread.sleep(3000);
           handler.sendMessage(handler.obtainMessage());

}
catch (Throwable t) {
// just end the background thread
}
}
});
isRunning.set(true);
background.start();
}
public void onStop() {
super.onStop();
isRunning.set(false);
}
}


Try the solution on this post...seems to be what you looking for http://androidgenuine.com/?p=707

0

精彩评论

暂无评论...
验证码 换一张
取 消