Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

multithreading - How do i make Android app which do something every X second

Hello i wanna do apliacation which every 1 second call function or do something else. I have this code which is not working can you tell what is wrong?

public class App5_Thread extends Activity implements Runnable {    
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                Thread thread = new Thread(this);    
                thread.start();
        }
        @Override                    
        public void run() {                
                TextView tv1 = (TextView) findViewById(R.id.tv);
                showTime(tv1);                                                                
                try {
                    Thread.sleep(1000);
                }catch (Exception e) {
                    tv1.setText(e.toString());
                }            
        } 
        public void showTime(TextView tv1 ){                
            String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            tv1.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis());                    
        }           

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't have a loop in run() function, try to change it like this:

    @Override                    
    public void run() {   
            TextView tv1 = (TextView) findViewById(R.id.tv);
            while(true){
               showTime(tv1);                                                                
               try {
                   Thread.sleep(1000);
               }catch (Exception e) {
                   tv1.setText(e.toString());
               }           
            } 
    }            

Be careful, it will run forever though. You should also use a handler to perform changes on UI from another thread, it can be done by example below:

Handler mHandler = new Handler();
    @Override                    
    public void run() {   
         final TextView tv1 = (TextView) findViewById(R.id.tv);
         while(true){                                                             
            try {
                mHandler.post(new Runnable(){
                   @Override
                   public void run() {
                      showTime(tv1);  
                   }
                ); 
                Thread.sleep(1000);
            }catch (Exception e) {
                //tv1.setText(e.toString());
            }           
         } 
    }      

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...