2016-01-22 39 views

回答

0

您可以發佈任務到主線程以在特定時間之後更新文本視圖。請參考以下代碼:

public class HistoryActivity extends ActionBarActivity { 
TextView mTextView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_history); 

    String history = getIntent().getStringExtra("History"); 

    mTextView = (TextView)findViewById(R.id.txtHistory); 
    mTextView .setText(history); 

    Handler handler=new Handler(); 

    final Runnable runnable=new Runnable() { 
     @Override 
     public void run() { 

      //put your textview update code here 
      mTextView .setText(history); 
      handler.postDelayed(this,30*1000);//reschedule this runnable 
     } 
    }; 

    handler.postDelayed(runnable,30*1000); 

    } 
} 
1

你可以做到這一點,以達到預期的效果:

private static final ScheduledExecutorService worker = 
Executors.newSingleThreadScheduledExecutor(); 

void someMethod() { 
    Runnable task = new Runnable() { 
    public void run() { 
     /* Do something… */ 
    } 
    }; 
    worker.schedule(task, 30, TimeUnit.SECONDS); 
}