1. 구현 결과 화면
2. activity_main.xml
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 31 32 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="현재 값 : " android:textSize="25sp" android:textStyle="bold" android:textAlignment="center" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/button" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> <Button android:id="@+id/button" android:layout_width="200dp" android:layout_height="wrap_content" android:text="스레드 시작" android:textSize="20sp" app:layout_constraintTop_toBottomOf="@id/text_view" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </android.support.constraint.ConstraintLayout> | cs |
3. MainActivity.java
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.tistory.qlyh8.pracitice; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; Button button; ValueHandler handler = new ValueHandler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { BackgroundThread thread = new BackgroundThread(); thread.start(); // run() 메서드 호출 } }); } // 1초마다 값이 1씩 증가하는 스레드 class BackgroundThread extends Thread { boolean isRunning = false; int value = 0; @Override public void run() { isRunning = true; while(isRunning){ value += 1; Message msg = handler.obtainMessage(); // 메시지 객체를 참조 Bundle bundle = new Bundle(); bundle.putInt("value", value); msg.setData(bundle); handler.sendMessage(msg); // 핸들러로 메시지를 보낸다. try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class ValueHandler extends Handler { @Override public void handleMessage(Message msg) { // 메시지 객체 처리 super.handleMessage(msg); Bundle bundle = msg.getData(); textView.setText("현재 값 : "+ bundle.getInt("value")); } } } | cs |
출처: https://www.edwith.org/boostcourse-android/lecture/17086
'Android' 카테고리의 다른 글
AsyncTask (0) | 2018.07.25 |
---|---|
스레드 (Thread) - 3. 핸들러를 이용한 스레드 구현 (2) (0) | 2018.07.24 |
스레드 (Thread) - 1. 스레드와 핸들러 (0) | 2018.07.24 |
Context (컨텍스트) (0) | 2018.07.20 |
ANR (Application Not Responding) (0) | 2018.07.17 |