활동에 타이머를 표시하는 방법은 무엇입니까?
-
16-09-2019 - |
문제
응용 프로그램 내의 몇 가지 활동에 시간 기간을 표시해야합니다. 활동 중 하나가 시작될 때 타이머가 시작됩니다.
- 타이머에 서비스를 사용해야합니까?
- 이것이 가장 좋은 방법입니까?
- 아니면 활동 중 하나에서 스레드를 시작해야합니까?
해결책
유스 케이스에서 당신이 묘사하는 것이 가장 좋을 것 같습니다. 가게 타임 스탬프 (참조 데이터 저장고) 그리고 계산하다 GUI 사용을위한 델타. 활동 중 하나에 실시간 시계를 표시 해야하는 경우 해당 활동에서 시계를 업데이트하기 위해 별도의 스레드를 만들 수 있습니다.
다른 팁
글쎄, 진행 상황에 따라 진행 상황에 따라 활동 내에서 스레드를 시작한 다음 스레드 진행 상황을 확인하고 필요에 따라 인터페이스를 업데이트하는 타이머를 만듭니다. 서비스는 인터페이스 알림/업데이트가 많이 필요하지 않은 배경 작업에 적합합니다.
다음은 현재 작업중 인 프로젝트의 예입니다 (내 목록 어댑터에서 "NotifyDataSetchanged ()"호출 "UpdatelStrUnnable Just Call"Call "CODE에서 여러 번 수행하여 클래스에서 캡슐화했습니다. 또한 UpdateHandler는 일반 핸들러입니다. 사례):
@Override
public void run() {
Timer updateProgressTimer = null;
UpdateItem currentItem = null;
for(int i = 0; i < items.size(); i++) {
currentItemIndex = i;
currentItem = items.get(i);
if (currentItem.isSelected() == true) {
updateProgressTimer = new Timer();
updateProgressTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateHandler.post(new UpdateListRunnable());
}
}, 0, 2000); // check every 2 seconds
lookupDb.downloadUpdate(currentItem);
currentItem.setUpToDate(true);
currentItem.setStatusCode(UpdateItem.UP_TO_DATE);
currentItem.setProgress(0);
updateProgressTimer.cancel();
updateHandler.post(new UpdateListRunnable());
} // end if its the database we are hosting on our internal server
} // end for loop through update items
currentItemIndex = -1;
} // end updateThread run
제휴하지 않습니다 StackOverflow