スレッドを使用して5秒のタイマーを実行しています。タイマーが不足しています。いくつかの変数を変更して再起動し、一定の条件が満たされなくなるまでこのプロセスを繰り返します。私はスレッドを研究しており、再設定や再利用はできませんが、代わりに別のスレッドを作成する必要があります。私は正しい方法を理解できません。ここでAndroidでスレッドを繰り返し使用できますか?
は私のコードです:
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;
}
}
}
だから、基本的に、私は今のプログラム私は最初の2つのレベルのテストのために、スレッドを使用し続けるが、それをレベルに応じて異なる時間制限を渡したいです。
私はthis.Thanksに任意の洞察力をいただければ幸いです。
ここでの回答で受信されたヘルプの一部を訴え私のコードの改訂版が、私はまだ持っています問題:
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)
この情報が私の問題を示すのに役立ちます。
私はAndroidや、一般的なスレッドに関しては非常に経験が不足していますが、おそらくこれの例を指摘できますか?ありがとう! – deucalion0
@ deucalion0 - いくつかのサンプルコードで回答を更新しました –
ありがとう、私は今それを試して使用し、うまくいけば目標を達成します。ありがとう。 – deucalion0