나는 Android Studio에서 간단한 게임을 만들려고 노력 중이며 특정 이벤트를 지연시킬 수 있어야합니다.Java에서 지연된 이벤트를 처리하는 가장 좋은 방법은 무엇입니까
Google 검색을 시작하면서 Timer
과 TimerTask
이 꽤 좋은 옵션 인 것으로 나타났습니다. 그러나 내 활동이 으로 전화 할 경우에는 Timer.pause
이 없으므로 모든 것을 취소해야합니다.
그래서 나는 나를 위해 이벤트를 처리하고 일시 중지를 지원하는 자신 만의 클래스를 생성하기로 결정했습니다.
나는 10 밀리 초마다이 경우System.currentTimeMillis
> =
eventFinishedTime
을보고 이벤트의 ArrayList를 통해 간단한 사용자 명령에 "이벤트 (들)"를 만들어 클래스 (
EventHandler
) 및주기를했다. 이벤트가 완료되면 inteface 메서드를 호출하고 이벤트는 ArrayList에서 자신을 제거합니다.
하지만 이제 새로운 문제가 생깁니다.
EventHandler
은 이벤트가 완료되면 인터페이스 메서드 (onFinished
)를 호출하므로 onFinished
에 마지막으로 선언되지 않은 변수를 사용할 수 없습니다. 이 문제를 해결할 수있는 유일한 방법은 사건을 연기하고 싶을 때마다 새로운 방법을 만드는 것입니다. 이는 나쁜 습관처럼 보입니다.
제 궁금한 점은 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?
하는 것은 당신이 내 코드를보고 싶은 경우 단지 은 또한 자세한 내용은 문의 주시기 어느 부분 : 지정, 물어 주시기 바랍니다 ... 나는 꽤 요약하고, 시도 할 의사가 이상 해요 예제와 함께 더 자세히 설명하십시오.
감사합니다. 내가 그것을 사용하려고 경우
다음public class EventHandler extends Thread {
//Constants
final String TAG = "EventHandler";
final long WAIT_TIME = 10;
public ArrayList<Event> events = new ArrayList<>(); //Every WAIT_TIME the run() funtion cycles through this list and checks if any events are complete
public boolean runChecks = true; //If true, the run() function goes (It's only false while the DoDayActivity tells it to pause
public long pauseStartTime; //This value tags the System.currentTimeMillis() @pauseCheck
public long totalPausedTime = 0; //This value contains how long the EventHandler was paused
Activity activity;
public EventHandler(Activity activity) {
this.activity = activity;
}
public void run() {
//checking the listeners
while (true) {
if (runChecks) {//Making sure the timer isn't paused
checkListeners();
}
try {
Thread.sleep(WAIT_TIME); //Yes I am using Thread.sleep(), kill me
} catch (Exception ignore) {
}
}
}
public interface OnEventListener {
void onFinished();
}
public void createEvent(String name, long milliseconds, OnEventListener eventListener) {//This is how an event is created, see the private Event class below
new Event(this, name, milliseconds, eventListener);
}
public void checkListeners() {
for (Event event : events) {
event.amIFinished();//A method that checks if the event has reached its end time
}
}
public void pauseCheck() { //"Pauses" the timer (Probably not the best way, but it is simple and does what I need it to
runChecks = false;
pauseStartTime = System.currentTimeMillis();
}
public void resumeCheck() {//Resumes the timer by adding the amount of time the EventHandler was paused for to the end if each event
try {
if ((pauseStartTime > 99999999)) {//For some reason, when an activity is created, onResume is called, so I added this in there to prevent glicthes
totalPausedTime = System.currentTimeMillis() - pauseStartTime;
Log.d(TAG, "Resuming, adding " + String.valueOf(totalPausedTime) + " milliseconds to each event");
for (Event e : events) {
e.end += totalPausedTime;
}
}
} catch (Exception e) {
Log.w(TAG, "During resume, EventHandler tried to add time to each of the events, but couldn't!");
e.printStackTrace();
}
runChecks = true;
}
private class Event { //Here is the class for the event
public long start;
public long end;
OnEventListener listener;
EventHandler parent;
String name;
public Event(EventHandler parent, String name, long milliseconds, OnEventListener listener) {
start = System.currentTimeMillis();
end = start + milliseconds;
this.listener = listener;
this.parent = parent;
this.name = name;
//Adding itself to the ArrayList
parent.events.add(this);
}
public void amIFinished() {//Method that checks if the event is completed
if (System.currentTimeMillis() >= end) {//Removes itself from the arraylist and calls onFinished
Log.d(TAG, "Completed " + name);
parent.events.remove(this);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
listener.onFinished(); //This is where the interface method is called!
}
});
}
}
}
}
은 다음과 같습니다
(나는 스크롤 인터페이스 방법이라고 볼 수 아래로 수입을 ... 포함하지 않았다)이 EventHandler.class입니다int x = 0;
eventHandler = new EventHandler(this);
eventHandler.start();
eventHandler.createEvent("Change X Value", 800, new EventHandler.OnEventListener() {
@Override
public void onFinished() {
//x is not declared final so it will not work
x = 5;
}
});
게시물 업데이트 – Pythogen
cuncurrent programm에서 'ArrayList'를 사용하면 안됩니다. 대체 클래스'CopyOnWriteArrayLis','ConcurrentLinkedQueue', ... –