更新线程中的 UI Activity
这是我的问题。我正在编写一个android应用程序,它需要显示它正在运行的活动的时间。因此,我创建了一个从活动中调用的新线程。但是,当我在模拟器中启动应用程序时,似乎活动从未加载。也许你们可以在这里帮助我。这是相关的代码:
public class ActivityStartTracker extends ActivityBasic {
TimerThread timer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
timer = new TimerThread();
}
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(
ServiceLocator.BROADCAST_ACTION));
timer.run();
}
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
if (timer.isRunning()) {
timer.interrupt();
}
}
class TimerThread extends Thread {
boolean isRunning = false;
public TimerThread() {
}
@Override
public void run() {
int seconds = 0;
isRunning = true;
while (isRunning) {
try {
Log.d(TAG, "Timer is running for : " + seconds);
seconds++;
Thread.sleep(1000);
} catch (InterruptedException e) {
isRunning = false;
}
}
}
public boolean isRunning() {
return isRunning;
}
}
}
就像我说的,这些是相关的代码片段。 问题是,如果我运行这个,活动保持黑色 - 我只是得到带有应用程序名称的标题,仅此而已。通过日志记录,我可以确认线程正在运行,但没有显示其他内容。
你能帮我一下吗,我对 Android 很陌生。
非常感谢 - 已经提前了。
here is my problem. I am programming an android app, which needs to display the time the activity it is running. Therefor I created a new Thread which is called from the activity. However when I start the app in the emulator, it seems the Activity is never loaded. Maybe you guys can help me here. Here is the relevant code:
public class ActivityStartTracker extends ActivityBasic {
TimerThread timer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
timer = new TimerThread();
}
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(
ServiceLocator.BROADCAST_ACTION));
timer.run();
}
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
if (timer.isRunning()) {
timer.interrupt();
}
}
class TimerThread extends Thread {
boolean isRunning = false;
public TimerThread() {
}
@Override
public void run() {
int seconds = 0;
isRunning = true;
while (isRunning) {
try {
Log.d(TAG, "Timer is running for : " + seconds);
seconds++;
Thread.sleep(1000);
} catch (InterruptedException e) {
isRunning = false;
}
}
}
public boolean isRunning() {
return isRunning;
}
}
}
Like I said, these are the relevant code fragments.
The problem is, if I run this, the Activity stays black - I just get the header with the name of the app, thats all. With the logging I can confirm that the thread is running, but nothing else is displayed.
Can you please help me out, I am rather new to android.
Thank you very much - already in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用timer#run()就可以做到这一点,在UI线程中调用#run()方法。您需要通过调用 #start() 来启动线程 - 然后新线程将执行您期望的操作。
进一步:永远不要调用 Thread#start() 两次,它实际上不会重新启动线程,只会使您的应用程序崩溃。如果您打算从 onResume 启动线程,则需要在那里分配一个新线程。
顺便说一句,请考虑使用 AsyncTask 代替。
兄弟,
延斯
Calling timer#run() will do just that, call the #run() method in the UI thread. You need to start your thread by calling #start() on it instead - the new thread will then do what you expect.
Further: Do not ever call Thread#start() twice, it won't actually restart the thread, just crash your application. If you intend to start the Thread from your onResume you need to allocate a new thread there.
Btw, consider using an AsyncTask instead.
Br,
Jens
请记住,唯一可以更新 UI 的线程是
UIThread
。因此,如果你想在另一个线程中修改 UI,你应该使用方法:
runOnUIThread()
,但是当你有一个线程要更新 UI 时,最好的方法是使用
AsyncTask
;参考 这个keep on your mind that the only thread that can update the UI is the
UIThread
.So if you want to modify the UI in another thread, you should the method :
runOnUIThread()
, but when you have a thread that will update the UI , the best way is to use
AsyncTask
; refer this