我正在使用一个线程运行一个5秒的计时器,当该计时器用完时我想更改一些变量并重新启动它,重复此过程直到不满足某个条件。我一直在研究线程,显然它们无法重置或再次使用,但必须创建另一个线程。我无法弄清楚正确的方法。
这是我的代码:
package com.deucalion0;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class FirstOneActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer ourSong;
int counter;
Button add;
Thread timer;
TextView display;
TextView lvl;
int level = 1;
int time;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ourSong = MediaPlayer.create(FirstOneActivity.this, R.raw.click);
counter = 0;
add = (Button) findViewById (R.id.bAdd);
display = (TextView) findViewById (R.id.tvDisplay);
lvl = (TextView) findViewById (R.id.lvldisplay);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
//ourSong.start();
display.setText("Your total is "+ counter);
if(counter ==1)
{
set();
}
}
});
timer = new Thread(){
public void run(){
try{
sleep(time);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
test();
}
}
};
}
public void test(){ // Method that does two things after the timer runs out, depending on the value held in counter
if(counter>= 10 && level == 1 || counter>= 15 && level == 2)
{
level++;
counter =0;
display.setText("Your total is 0");
//The timer must be reset for the next level
}
else if(counter<10 && level == 1 || counter< 15 && level == 2){
Intent openNext = new Intent("com.deucalion0.NEXT");
startActivity(openNext);
}
}
public void set(){
if(level == 1)
{ lvl.setText("Level is "+ level);
time = 5000; // The value passed to the sleep method in the thread, this is the length of the timer
}
else if (level == 2)
{lvl.setText("Level is "+level);
time = 5000;
}
}
}
所以基本上我想继续使用线程但是根据级别传递不同的时间限制,以便测试我现在只编程的前两个级别。
我很感激对此的任何见解。谢谢。
我的代码的修订版本起诉了答案中收到的一些帮助,但我仍然遇到问题:
package com.deucalion0;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class FirstOneActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer ourSong;
int counter;
Button add;
Thread timer;
TextView display;
TextView lvl;
int level = 1;
int time;
boolean completed = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ourSong = MediaPlayer.create(FirstOneActivity.this, R.raw.click);
counter = 0;
add = (Button) findViewById (R.id.bAdd);
display = (TextView) findViewById (R.id.tvDisplay);
lvl = (TextView) findViewById (R.id.lvldisplay);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
completed=false;
//ourSong.start();
display.setText("Your total is "+ counter);
if(counter ==1)
{
set();
timer.start(); // start the timer loop:
}
}
});
// create a Handler for the thread to use when interacting with the UI
final Handler handler = new Handler();
// create the timer object
timer = new Thread() {
public void run() {
// create a Runnable that will execute on the UI thread
Runnable updater = new Runnable() {
public void run() {
}
};
while (!completed) {
try {
sleep(time);
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
handler.post(updater);
test();
}
}
}
};
}
public void reset()//Resets the variables so the level increments, the counter is reset to 0
{
completed=true;
level++;
counter =0;
}
public void test(){ // Method that does two things after the timer runs out, depending on the value held in counter
if(counter>= 10 && level == 1 || counter>= 15 && level == 2)
{
reset();
}
else if(counter<10 && level == 1 || counter< 15 && level == 2){
Intent openNext = new Intent("com.deucalion0.NEXT");
startActivity(openNext);
}
}
public void set(){
if(level == 1)
{ lvl.setText("Level is "+ level);
time = 5000; // The value passed to the sleep method in the thread, thi is the length of the timer
}
else if (level == 2)
{lvl.setText("Level is "+level);
time = 5000;
}
}
}
当计数器= 1时游戏运行并启动计时器,但如果我在计时器用完后按下按钮,我会因异常而关闭一个力。我需要发生的是,如果在5秒内按下超过9次点击,计数器将重置为0并且级别变为2。以下是LogCat中的错误:
02-29 09:47:36.313: D/AndroidRuntime(279): Shutting down VM
02-29 09:47:36.313: W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-29 09:47:36.333: E/AndroidRuntime(279): FATAL EXCEPTION: main
02-29 09:47:36.333: E/AndroidRuntime(279): java.lang.IllegalThreadStateException: Thread already started.
02-29 09:47:36.333: E/AndroidRuntime(279): at java.lang.Thread.start(Thread.java:1322)
02-29 09:47:36.333: E/AndroidRuntime(279): at com.deucalion0.FirstOneActivity$1.onClick(FirstOneActivity.java:51)
02-29 09:47:36.333: E/AndroidRuntime(279): at android.view.View.performClick(View.java:2408)
02-29 09:47:36.333: E/AndroidRuntime(279): at android.view.View$PerformClick.run(View.java:8816)
02-29 09:47:36.333: E/AndroidRuntime(279): at android.os.Handler.handleCallback(Handler.java:587)
02-29 09:47:36.333: E/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:92)
02-29 09:47:36.333: E/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123)
02-29 09:47:36.333: E/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-29 09:47:36.333: E/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method)
02-29 09:47:36.333: E/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521)
02-29 09:47:36.333: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-29 09:47:36.333: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-29 09:47:36.333: E/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method)
我希望此信息有助于显示我的问题。
答案 0 :(得分:2)
作为一般规则,Thread
对象可以start()
- 编辑任意多次。每次调用start()
都会启动一个新主题。其他类似线程的对象(例如AsyncTask
,TimerTask
)通常只能执行一次。
但是,您的计时器线程将无法按写入方式工作。 test()
方法修改显示,需要在UI线程上运行。
一种方法是创建Handler
并从计时器线程发布Runnable
到处理程序,然后处理程序调用{{1}}。还有其他方法,但这可能涉及对现有代码的最少修改。
您应该考虑使用test()
来做您想做的事情。您还应该在开发时启用StrictMode
以帮助检测任何线程错误。
以下是对代码的简单修改,它将执行我认为您正在尝试执行的操作:
AsyncTask
上面的代码假设有一个方法// create a Handler for the thread to use when interacting with the UI
final Handler handler = new Handler();
// create the timer object
timer = new Thread() {
public void run() {
// create a Runnable that will execute on the UI thread
Runnable updater = new Runnable() {
public void run() {
test();
}
}
while (!exitConditionMet()) {
try {
sleep(time);
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
handler.post(updater);
}
}
}
};
// start the timer loop:
timer.start();
在线程退出时返回exitConditionMet()
。
答案 1 :(得分:1)
你应该避免创建和销毁那样的线程。对于您的用例,我会说保持相同的线程运行会更好,但是在更改变量时使用同步原语来阻止它,然后在准备好后继续工作。你可以使用从简单的锁到条件变量或任何其他同步机制的任何东西,但我会保持相同的线程。
答案 2 :(得分:1)
不要担心直接创建和重用线程。而是专注于您想要执行的任务,让Executor负责为您安排这些任务(通常使用线程池)。 Executors类有许多静态方法,用于创建不同类型的Executor实例http://developer.android.com/reference/java/util/concurrent/Executors.html。
您可以使用Runnable或AsyncTask定义任务。以下是使用AsyncTask进行操作的粗略示例:
Executor executor = ...
AsyncTask task = ...
task.executeOnExecutor(executor, ...);