如何让我的Android应用程序,它做点什么每隔X秒 [英] How do i make Android app which do something every X second
本文介绍了如何让我的Android应用程序,它做点什么每隔X秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我想要做的apliacation其中每1秒调用功能或做其他事。 我有这个code这是不工作,你能告诉什么是错的?
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());
}
}
推荐答案
我有按照修改code
I have alter the code by following
有3种方式个个工作(显示日志输出)。
There are 3 ways all of them working(showing Log output).
但是TextView中没有更新。为什么呢?
package cz.cvut.fel.android;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class App5_RepeatedAction extends Activity {
static TextView tv1;
static class MyThread implements Runnable {
public void run() {
boolean end = false;
while (!end) {
String txt = "Vlakno id:" + Thread.currentThread().getId()+" THREAD";
Log.v("MyActivity", txt);
//tv1.setText(txt);
showTime(tv1);
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
System.err.println(ex.toString());
}
}
}
}
static 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());
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.tv);
//----------ScheduledExecutorService
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
String txt = "ScheduledExecutorService";
Log.v("MyActivity", txt);
//tv1.setText(txt);
showTime(tv1);
}
}, 0, 5, TimeUnit.SECONDS);
//----------TIMER
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
String txt = "Timer";
Log.v("MyActivity", txt);
//tv1.setText(txt);
showTime(tv1);
}
}, 0, 1000);
//-----------THREAD
try {
boolean quit = false;
Thread t = new Thread(new MyThread());
//t.setDaemon(true);
t.start();
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
这篇关于如何让我的Android应用程序,它做点什么每隔X秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文