스레드를 사용하여 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;
}
}
}
그래서 기본적으로 내가 테스트 I 지금은 프로그램 첫 번째 두 단계에 대한 스레드를 계속 사용하지만, 그것을 수준에 따라 다른 시간 제한을 통과하려면 :
여기 내 코드입니다.
나는이 모든 통찰력을 주셔서 감사하겠습니다. 감사합니다.
여기에 대한 답변에서받은 도움의 일부를 고소 내 코드의 수정 된 버전,하지만 난 여전히 데 문제 :
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,하지만 타이머가 끝난 후 버튼을 누르면 예외로 인해 강제 종료됩니다. 필자가해야 할 일은 카운터가 0으로 재설정되고 5 초 이내에 9 회 이상의 클릭이 눌러지면 레벨이 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)
이 정보가 내 문제의 해결을 돕기 바랍니다.
안드로이드와 스레드에 관해서는 매우 경험이 없습니다.이 예제를 지적 해 주시겠습니까? 감사! – deucalion0
@ deucalion0 - 일부 샘플 코드 –
으로 답변을 업데이트했습니다. 고맙습니다. 지금 사용해 보시고 잘하면 내 목표를 달성 할 것입니다. 알아 내면 알려 드리겠습니다. 감사. – deucalion0