일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 펌웨어
- 충전기
- OCPP
- 전기차충전기
- everon
- raspberry
- Android
- 서버리스
- YMODEM
- 홈어시스턴트
- lambda
- 디자인패턴
- homeassistant
- esp8266
- 전기차충전
- 급속충전기
- STM32
- flask
- 보안
- IOT Core
- thread
- 안드로이드
- dynamodb
- 라즈베리파이
- 에버온
- 파이썬
- 플라스크
- AWS
- 완속충전기
- 전기차
- Today
- Total
Louie NRT Story
[안드로이드] runOnUiThread() 본문
작성일: 21년 11월 23일
Index
1. runOnUiThread 존재하는 이유
2. Layer 코드
3. MainActivity 코드
4. 실행된 화면
1. runOnUiThread 존재하는 이유
- UI를 제어할때 여기저기에서 제어를 하게 되면 동기화 문제를 방지하기 위함임
- UI Thread에서만 제어 할 수 있도록함
2. Layer 코드
- 가운데에 시간을 표현 할 수 있도록함
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
android:text="00:00:00"
android:id="@+id/clock"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity 코드
- 현재 시간을 1초마다 가져와서 TextView에 보여줌
TextView clockTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Runnable runnable = new Runnable(){
@Override
public void run() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String strTime = sdf.format(cal.getTime());
clockTextView = findViewById(R.id.clock);
clockTextView.setText(strTime);
}
};
class NewRunnable implements Runnable{
@Override
public void run() {
while(true){
try{
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(runnable);
}
}
}
NewRunnable nr = new NewRunnable();
Thread t = new Thread(nr);
t.start();
}
4. 실행된 화면
- 현재시간을 초단위로 보여줌

Referece:
https://onlyfor-me-blog.tistory.com/54
[Android] runOnUiThread란?
참고한 사이트 : https://itmining.tistory.com/6 [안드로이드] runOnUiThread란? (개념과 사용법) 이 글은 PC 버전 TISTORY에 최적화 되어있습니다. 서론 이전 포스팅 (Thread, Handler, Looper를 이용한 백그라..
onlyfor-me-blog.tistory.com
https://sharp57dev.tistory.com/21
[안드로이드] Thread(쓰레드)에서 UI 변경하기 - runOnUiThread 사용하기
안드로이드는 UI 작업(업데이트)시에는 특정 UI Thread에서 사용하도록 되어있다. 그래서 UI Thread가 아닌 Thread에서 사용하면 오류가 발생한다. 이 때 Thread에서 UI작업을 할 수 있는 방법에 대해 알
sharp57dev.tistory.com
'전기차충전기' 카테고리의 다른 글
[안드로이드] Log 찍는 방법 (0) | 2021.12.09 |
---|---|
[Smatek] ST-RK3288-01 Root 시스템 권한 설정 - Todo (0) | 2021.11.23 |
[안드로이드] Thread와 Runnable (0) | 2021.11.23 |
[안드로이드] Handler (0) | 2021.11.23 |
[안드로이드] Thread 생성하기 (0) | 2021.11.23 |