107
서비스/스레드/DB

콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

  • Upload
    ledat

  • View
    222

  • Download
    4

Embed Size (px)

Citation preview

Page 1: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스/스레드/DB

최 민

Page 2: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Service

안드로이드는 서비스에게 비활성 액티비티보다 높은 우선순위

부여

시스템이 리소스를 필요로 할 때 서비스가 종료될 가능성은 적

서비스가 종료되었더라도 리소스가 충분해지면 즉시 재시작

GUI 없이 실행

Activity, Broadcast receiver와 같이 애플리케이션 프로세스의 메

인 쓰레드 내에서 실행

• 좋은 반응성을 가지려면 시간이 많이 드는 처리(네트워크 조회 등)

를 백그라운드 쓰레드로 옮겨야 함

UI 없이 알림 Notification, Toast를 이용하여 정보 제공

서비스

Page 3: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Toast

활성 애플리케이션의 포커스를 빼앗지 않은 채 사용자에게 정보

를 표시하기 위해 사용되는 일시적 성격의 non-modal 다이얼로

그 박스 메커니즘

Notification

보다 견고한 사용자 경보 메커니즘

사용자의 주의를 끄는 모든 동작 (벨 울림, 진동, 불빛 깜박임)

이 안드로이드에서는 Notification을 통해 이용 가능

Alarm

애플리케이션 수명 주기의 제어 바깥에서 정해진 시간에 Intent

를 발생시키기 위한 메커니즘

서비스

Page 4: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스 소개

두 가지 기능

• 백그라운드 작업 수행

• IPC(Inter-Process Communication)을 위한 원격접속 가능한 오브젝

트를 만들어내는 것

백그라운드에서 실행되면서 Content Provider를 업데이트하고,

Intent를 발생시키며, Notification을 만들어 냄

• 애플리케이션에 속한 Activity가 화면에 보이지 않거나, 비활성 상태

가 되거나, 혹은 종료된 이후에도 정규 처리나 이벤트 처리를 수행

하기 위한 방법

• Service는 GUI 없이 다른 Service, Activity, Broadcast Receiver를 포

함한 다른 애플리케이션 컴포넌트로부터 시작, 중지, 제어됨

서비스

Page 5: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스 소개

특징

• 비활성 Activity나 눈에 보이지 않는 Activiti보다 높은 우선순위 부

• 런타임의 리소스 관리에 의해 종료될 가능성 낮음

• Foreground 서비스가 필요한 리소스를 얻지 못할 때만 종료

• 사용자 상호작용이 거의 드물거나 간헐적으로 필요한 애플리케이

– MP3 플레이어, 스포츠 점수 모니터

• 안드로이드 내

– 위치 관리자, 미디어 컨트롤러, 알림 관리자

서비스

Page 6: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스 Lifecycle

OnCreate()

OnStart()

OnDestroy()

Service is Shut down

Service is Running

Service started by startService()

The service is

stopped (no callback)

OnCreate()

OnBind ()

OnDestroy()

Service is Shut down

Service created by bindService()

Client interacts

with the service

onUnbind()

OnRebind()

startService() : onCreate() onStart() 메소드 호출bindService() : onCreate() onBind() 메소드 호출 : 이 경우 onStart()는 호출되지 않음을주의

Page 7: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스 만들기

public class MyService extends Service {@Overridepublic void onCreate() {

}

@Overridepublic IBinder onBind (Intent intent) {

}

@Overridepublic void onStart(Intent intent, int startId) {

}}

Page 8: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Service 시작, 제어, 그리고 상호작용

Service 등록

• 만일 Service가 애플리케이션이 갖지 않은 권한을 요구하면

SecurityException 발생

암시적인 서비스 실행

• startService (new Intent(MyService.MY_ACTION));

명시적인 서비스 실행

• startService(new Intent(this, MyService.class));

서비스 제어

Page 9: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스 실습

Page 10: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 생성

프로젝트 명 : ServicesDemo

서비스 실습

1. 클릭

Page 11: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

MyService.java

서비스 실습

Page 12: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Raw

Mp3 파일 등록

서비스 실습

Page 13: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

MyService.java

Page 14: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ServicesDemo.java

서비스 실습

Page 15: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ServicesDemo.java

서비스 실습

Page 16: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스 실습

Page 17: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

서비스 실습

Page 18: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

응답없는 애플리케이션 (ANR : Application Not Responding) 이

벤트

5초 동안 사용자의 입력에 대해서 반응을 보이지 않는 것

강제 종료

메인 UI 쓰레드는 5초라는 지정된 시간안에 무조건 반응해야 함

긴 계산시간이 요구되는 작업들은 반드시 별도의 쓰레드로 수행

쓰레드

Page 19: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

쓰레드를 사용해야 할 때

애플리케이션이 좋은 반응성을 가지게 하기 위해

• 입력 이벤트에 대해 5초 이내, onReceive 핸들러를 10초 이내에 완

료하지 않으면 Application Unresponsive 메시지 발생

• 느리고 시간이 많이 걸리는 모든 작업을 메인 애플리케이션 쓰레드

에서 자식 쓰레드로 옮긴다.

Activity, Service, Broadcast Receiver 등을 포함한 모든 안드로이

드 애플리케이션 컴포넌트는 메인 애플리케이션 쓰레드 위에서

동작

어떤 한 컴포넌트 내에서 시간이 많이 드는 처리 다른 모든

컴포넌트를 블록시킴

쓰레드

Page 20: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

쓰레드

Page 21: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

멀티쓰레딩(multi-threading)의 장점

프로세스의 자원을 공유면서 독립적으로 실행되는 실행흐름

병행실행 (Concurrent execution)을 실현

메인 애플리케이션은 사용자 인터페이스/입력을 관리

시간이 오래 걸리는 계산은 background thread로 수행

다중 프로세서, 멀티코어 CPU의 시스템에서 특히 유용(성능향

상)

쓰레드

Page 22: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

쓰레드 예제

Private void mainProcessing() {// 이는 시간이 많이 드는 작업을 자식 쓰레드로 옮긴다.Thread thread = new Thread(null, doBackgroundThreadProcessing, “Background”);thread.start();

}Private Runnable doBackgroundThreadProcessing =

new Runnable() {public void run() {

backgroundThreadProcessing() ;}

};Private void backgroundThreadProcessing() {

[ … 시간 소모적인 작업들 … ]}

Page 23: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Thread 클래스 상속

Class MyThread extends Thread

Runnable 인터페이스 구현

Class MyThread extends A implements Runnable

Page 24: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 생성

프로젝트 명 : ThreadTestPrj

서비스 실습

1. 클릭

Page 25: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

쓰레드 실습

Page 26: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

쓰레드 실습

Page 27: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

쓰레드 실습

Page 28: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

핸들러

핸들러는 메시지 큐에 Message 와 Runnable 객체를 send 하거

나 process 한다

핸들러는 메인 UI 쓰레드와 자동으로 연결됨

스케쥴링

메시지 큐에 있는 작업들을 여러 쓰레드에서 처리

Message와 Runnable 객체를 스케쥴링

• Message는 ‘send’로, runnable 객체는 ‘post’로 전달함

별도의 쓰레드에서 작업한 결과는 핸들러가 있는 메인 UI 쓰레

드로 리턴되어 클래스들과 함께 수행

쓰레드와 핸들러

Page 29: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

핸들러의 두가지 활용

Message와 runnable 객체를 스케쥴링

한 쓰레드가 다른 쓰레드에서 수행할 작업을 메시지 큐를 통해

전달

쓰레딩과 UI

[주의사항]

Background 쓰레드는 UI를 건드릴 수 없음

메인 쓰레드만이 Activity의 View를 접근할 수 있음

쓰레드와 핸들러

Page 30: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

핸들러의 메시지큐 사용

메인 쓰레드와 통신하기를 희망하는 secondary thread는

obtainMessage() 메소드를 호출하여 message token을 요청함

• to get the Message object out of the pool.

Token을 획득하면, background thread는 메시지 토큰에 내용을

채운뒤 sendMessage() 핸들러의 메시지 큐로 전달함

핸들러는 handleMessage() 메소드를 호출하여 main thread에

도착하는 새로운 메시지를 지속적으로 체크한다.

메시지 큐에서 꺼내어진 메시지는 메인 쓰레드에게 데이터를 리

턴하거(by send)나 runnable 객체의 실행을 요청(by post) 할 수

있음

쓰레드와 핸들러

Page 31: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

시스템에 부하를 줄 수 있으므로 장시간 동안 서비스를 해야 하

는 프로그램은 추천하지 않는다.

만약 사용자가 더 사용할 가능성이 없는 경우라면 프로그램을 종

료할 때 함께 종료하는 것이 바람직함

필요하다면 사용자에게 옵션으로 제공하여 선택할 수 있게 만드

는 것이 좋음

장시간 서비스 프로그램에 대한 주의사항

Page 32: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

GUI 작업을 위한 쓰레드 동기화

// 메인 쓰레드에서 핸들러를 초기화한다.private Handler handler = new Handler();

private void mainProcessing() {Thread thread = new Thread (null, doBackgroundThreadProcessing, “Background”);Thread.start();

}

private Runnable doBackgroundProcessing = new Runnable() {public void run() {

backgroundThreadProcessing();}

};

Page 33: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

GUI 작업을 위한 쓰레드 동기화

private void backgroundThreadProcessing() {[ … 시간 소모적인 작업들 … ]handler.post(doUIpdateGUI);

}

private Runnable doUpdateGUI = new Runnable {public void run() {

updateGUI();}

};

private void updateGUI() {[ … 다이얼로그를 오픈하거나 GUI요소를 수정할 수

있다 … ]}

Page 34: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Using Messages

Page 35: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

AndroidManifest.xml

<application … > tag에서 android:debuggable=“true” 속성 추

라인 단위 디버깅

7. 디버깅 및 이벤트핸들링

Page 36: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

android:debuggable="true”를 설정하지 않는 경우

Wating For Debugger화면에서 더 이상 진행되지 않음

개발시에만 넣어두고 실제 릴리즈시에는 삭제

라인 단위 디버깅

7. 디버깅 및 이벤트핸들링

Page 37: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

라인 단위 디버깅

7. 디버깅 및 이벤트핸들링

Page 38: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

라인 단위 디버깅

7. 디버깅 및 이벤트핸들링

Page 39: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ADB

7. 디버깅 및 이벤트핸들링

Page 40: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ADB

7. 디버깅 및 이벤트핸들링

Page 41: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

UI 설계

CheckBox/RadioButton 구현

9. 위젯

8. UI 설계 및 속성 지

Page 42: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

속성 지정

TextView

9. 위젯

컨트롤 속성 지정

TextView01

• android:id="@+id/TextView01"

• android:layout_height="wrap_content"

• android:layout_width="wrap_content”

• android:text=“Color Setting Mode"

TextView02

• android:id="@+id/TextView02"

• android:layout_width="wrap_content"

• android:layout_height="wrap_content"

• android:text=“Color Setting"

Page 43: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

속성 지정

TextView

9. 위젯

컨트롤 속성 지정

TextView03

• android:id="@+id/TextView03"

• android:layout_height="wrap_content"

• android:layout_width="fill_parent"

• android:gravity="center"

• android:background="#FFFFFF"

• android:text="화면 설정"

Page 44: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

RadioGroup/RadioButton

9. 위젯

컨트롤 속성 지정

RadioGroup01

• android:id="@+id/RadioGroup01"

• android:layout_width="wrap_content"

• android:layout_height="wrap_content"

• android:orientation="horizontal"

RadioButton01

• android:id="@+id/RadioButton01"

• android:layout_width="wrap_content"

• android:layout_height="wrap_content"

• android:text="Set"

• android:width="100dip"

• android:checked="true"

Page 45: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

CheckBox

RadioGroup/RadioButton

9. 위젯

컨트롤 속성 지정

CheckBox03

• android:id="@+id/CheckBox03"

• android:layout_width="wrap_content"

• android:layout_height="wrap_content"

• android:text=“Blue"

Page 46: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Main.xml

CheckBox/RadioButton 구현

9. 위젯

12. XML code 추가 확인

Page 47: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

R.java

CheckBox/RadioButton 구현

9. 위젯

13. id 클래스 변수 추가 확인

Page 48: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Import

CheckBox/RadioButton 구현

9. 위젯

14. Coding

Page 49: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

onCreate() Method

9. 위젯

15. Coding

Page 50: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

onClickListener rdoScreen_listener 구현

9. 위젯

16. Coding

Page 51: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

onClickListener setColor_listener 구현

CheckBox/RadioButton 구현

9. 위젯

17. Coding

Page 52: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 실행

CheckBox/RadioButton 구현

9. 위젯

18. 클릭

19. 클릭

20. 클릭

Page 53: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

CheckBox/RadioButton 구현

9. 위젯

21. 클릭

22. 결과 확인

Page 54: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android_CheckBox/RadioButton 프로젝트

Android_CheckBoxRadioButton.java 파일을 수정하

여 아래 그림과 같이 바탕색을 변경하도록 프로그래밍

하시오.

실습 3 : CheckBox/RadioButton 구현

9. 위젯

Page 55: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

프로젝트 구현 방법

Main.xml 의 LinearLayout 속성 추가

① android:id="@+id/LinearLayout01"

Android_CheckBoxRadioButton.Java 파일 수정

② private LinearLayout objLayout; //LinearLayout

객체 선언

③ import android.widget.LinearLayout;

④ objLayout =

(LinearLayout)findViewById(R.id.LinearLayout01);

⑤ objLayout.setBackgroundColor(bkColor);

실습 3 : CheckBox/RadioButton 구현

9. 위젯

Page 56: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

기본 버튼 사용하기

Android.widget.Button 클래스는 안드로이드 SDK의 기본적인

버튼 구현을 제공

버튼 클릭 이벤트(event) 핸들러 추가하기

토스트 메시지 나타내기

• 토스트는 간단한 메시지를 화면에 잠깐 동안 표시하는 수단

• 안드로이드에서 매우 자주 유용하게 활용됨

Button.setOnClickListener() 메소드에 구현

실습 3 : CheckBox/RadioButton 구현

9. 위젯

Page 57: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

버튼, 체크박스, 라디오버튼

9. 위젯

setOnClickListener() 인스턴스를 정의할때 반드시 onClick() 메소드를 정의해야 함

Page 58: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

버튼, 체크박스, 라디오버튼

9. 위젯

Page 59: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

버튼, 체크박스, 라디오버튼

9. 위젯

Page 60: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

체크박스 (CheckBox)

한 목록에서 한번에 여러 개 항목들을 선택가능

체크 버튼과 오른쪽의 텍스트로 구성

토글버튼 (ToggleButton)

체크상자와 행동 방식이 유사

체크/해제에 따른 체크 상자의 모습 변화는 운영체제가 처리

개발자는 버튼 상태 변경에 따른 응용 프로그램의 행동에만 집

textOn : 켜진 상태의 텍스트에 해당하는 특성

textOff : 꺼진 상태의 텍스트에 해당하는 특성

버튼, 체크박스, 라디오버튼

9. 위젯

Page 61: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

버튼, 체크박스, 라디오버튼

9. 위젯

Page 62: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

버튼, 체크박스, 라디오버튼

9. 위젯

Page 63: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

RadioGroup & RadioButton

기본적인 동작은 역시 안드로이드 플랫폼이 처리함

라디오 버튼 상태 변경에 따라 응용 프로그램이 해야할 일만 고

라디오 버튼 선택이 변할 때마다 호출될 콜백 메소드를 등록

RadioGroup 전체 해제

RadioGroup의 모든 RadioButton 객체를 체크되지 않은 상태로

외부 한 버튼이 클릭되면 RadioGroup 전체가 해제되도록 함

라디오버튼 (RadioButton)

9. 위젯

Page 64: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

라디오버튼 (RadioButton)

9. 위젯

Page 65: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

라디오버튼 (RadioButton)

9. 위젯

Page 66: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

라디오버튼 (RadioButton)

9. 위젯

Page 67: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Toggle Button

기능

• Toggle 스위치(On/Off) 기능 제공

Toggle Button

9. 위젯

Page 68: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 생성

프로젝트 명 : Android_ToggleButton

Toggle Button 구현

9. 위젯

1. 클릭

Page 69: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Toggle Button 구현

9. 위젯

3. 클릭

2. Android_ToggleButton 입

4. Android_ToggleButton 입력

5. com.inhatc.Android_ToggleButton

입력6. Android_ToggleButton 입

력7. 클릭

Page 70: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

UI 설계

Toggle Button 구현

9. 위젯

8. UI 설계 및 속성 지정

Page 71: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

TextView, ToggleButton 속성 지정

Toggle Button 구현

9. 위젯

컨트롤 속성 지정

TextView

• android:layout_height="wrap_content"

• android:id="@+id/txtView"

• android:text="Toggle Button"

• android:layout_width="fill_parent"

• android:gravity="center_horizontal"

• android:background="#FFFFFFFF"

ToggleButto

n

• android:layout_width="wrap_content"

• android:layout_height="wrap_content"

• android:id="@+id/tglOnOff"

• android:text="Off"

Page 72: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

TextView, ToggleButton 속성 지정 결과

Toggle Button 구현

9. 위젯

9. 속성 지정 결과 확인

Page 73: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Main.xml 저장

Toggle Button 구현

9. 위젯

10. 클릭

11. 클릭

Page 74: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Main.xml

Toggle Button 구현

9. 위젯

12. XML code 추가 확인

Page 75: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

onCreate() Method 구현

Toggle Button 구현

9. 위젯

14. Coding

Page 76: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 실행

Toggle Button 구현

9. 위젯

15. 클릭

16. 클릭

17. 클릭

Page 77: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Toggle Button 구현

9. 위젯

18. 클릭

19. 클릭

20. 실행 결과 확인

Page 78: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Spinner

하나의 TextView가 아닌 다수의 TextView 객체들의 목록

선택된 텍스트를 Spinner에서 직접 가져올 수는 없음

대신, 선택된 View를 추출하고 그것에서 텍스트를 가져와야 함

스피너 (Spinner)

9. 위젯

Page 79: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Spinner

스피너 (Spinner)

9. 위젯

Page 80: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 생성

프로젝트 명 : Android_Spinner

스피너 (Spinner)

9. 위젯

1. 클릭

Page 81: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

스피너 (Spinner)

9. 위젯

3. 클릭

2. Android_Spinner 입

4. Android_Spinner 입력

5. com.inhatc.android_Spinner 입

력6. Android_Spinner 입력

7. 클릭

Page 82: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

UI 설계

스피너 (Spinner)

9. 위젯

8. UI 설계 및 속성

지정

Page 83: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

TextView01, Spinner01 속성 지정

스피너 (Spinner)

9. 위젯

컨트롤 속성 지정

TextView01

• android:layout_height="wrap_content"

• android:id="@+id/txtTitle"

• android:layout_marginTop="5dip"

• android:layout_width="fill_parent"

• android:text="Select a mobile phone"

Spinner01

• android:layout_height="wrap_content"

• android:layout_width="wrap_content"

• android:minWidth="300dip"

• android:id="@+id/spnDevice"

Page 84: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

TextView01, Spinner01 속성 지정 결과

스피너 (Spinner)

9. 위젯

9. 속성 지정 결과 확인

Page 85: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Main.xml 저장

스피너 (Spinner)

9. 위젯

10. 클릭

11. 클릭

Page 86: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

main.xml

스피너 (Spinner)

9. 위젯

12. XML code 추가 확인

Page 87: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

itemlist.xml 생성

스피너 (Spinner)

9. 위젯

13. 마우스 오른쪽 버튼 클

15. 클릭

14. 클릭

Page 88: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

스피너 (Spinner)

9. 위젯

16. 클

릭17. 클

Page 89: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

스피너 (Spinner)

9. 위젯

18.

“itemlist.xml”

입력

Page 90: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

itemlist.xml

• Item 지정

스피너 (Spinner)

9. 위젯

19. XML code 추가 확인

Page 91: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android_Spinner.java

스피너 (Spinner)

9. 위젯

21. Coding

Page 92: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 실행

스피너 (Spinner)

9. 위젯

22. 클릭

23. 클릭

24. 클릭

Page 93: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

스피너 (Spinner)

9. 위젯

25. 클릭

26. 클릭

Page 94: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ListView

기능

• Scrolling되는 하나의 column list를 출력하는 기능 제공

ListView

9. 위젯

Page 95: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ArrayList – ArrayAdapter - List View 관계

ListView 구현

9. 위젯

iPhone 3GS

Nexus One

Motoroi

Array

Adapter

List 내용

List View 내용 update

Page 96: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 생성

프로젝트 명 : Android_ListView

ListView 구현

9. 위젯

1. 클릭

Page 97: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ListView 구현

9. 위젯

3. 클릭

2. Android_ListView 입력

4. Android_ListView 입력

5. com.inhatc.android_ListView

입력

6. Android_ListView 입력

7. 클릭

Page 98: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

UI 설계

ListView 구현

9. 위젯

8. UI 설계 및 속성 지정

Page 99: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

EditText, ListView 속성 지정

ListView 구현

9. 위젯

컨트롤 속성 지정

EditText

•android:layout_height="wrap_content"

•android:id="@+id/etxtInput"

•android:hint="Input your item"

•android:width="300dip"

•android:layout_width="fill_parent"

•android:singleLine="true"

ListView

• android:layout_width="fill_parent"

• android:layout_height="wrap_content"

• android:id="@+id/lstPhone"

Page 100: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

EditText, ListView 속성 지정 결과

9. 위젯

9. 속성 지정 결과 확

Page 101: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Main.xml 저장

ListView 구현

9. 위젯

10. 클릭

11. 클릭

Page 102: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Main.xml

ListView 구현

9. 위젯

12. XML code 추가 확인

Page 103: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android_ListView.java

객체 선언

ListView 구현

9. 위젯

14. Coding

Page 104: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ListView 구현

9. 위젯

15. Coding

Page 105: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android 프로젝트 실행

ListView 구현

9. 위젯

16. 클릭

17. 클릭

18. 클릭

Page 106: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

ListView 구현

9. 위젯

19. 클릭

20. “Xperia X1” 입력 후

Enter Key 누름

21. ListView에

“Xperia X1”추가 확인

Page 107: 콘텐츠 PowerPoint 디자인keris2.dl.cdn.cloudn.co.kr/KOCW/document/2016/chung… ·  · 2017-03-24멀티쓰레딩(multi-threading) ... Android_CheckBoxRadioButton.java 파일을

Android_ListViewPractice1

AndroidPractice_Listview.java 파일을 프로그래밍하여 아래 그림과 같이 구현하시오.

• 단계 1: ListView에서 다중 선택 가능

실습 1 : ListView 구현

9. 위젯