Broadcast Receiver

Download Report

Transcript Broadcast Receiver

Speech Service & client(Activity)
2013.10.16
오지영
Android Application
Application
Package
Activity
BroadCast
Receiver
Content
Provider
Service
Android Service
 Service
 안드로이드 Application을 구성하는 컴포넌트 중 하나
 Activity처럼 사용자와 상호작용 하는 컴포넌트가 아니라 Background에서 동작하
는 컴포넌트
 Activity 화면에서 동작뿐만 아니라 Activity가 종료되어 있는 상태에서 동작하기 위
해 만들어진 컴포넌트
 예 :MP3 플레이어
 Service 시작과 중지 함수
 startService(Intent Service)
 stopService(Intent Service)
 두 함수는 Context class 소속(Context class 다음 장에서 소개)
Context class
 Overview
 Interface to global information about an application environment. This is an abstract
class whose implementation is provided by the Android system. It allows access to
application-specific resources and classes, as well as up-calls for application-level
operations such as launching activities, broadcasting and receiving intents, etc.
Context class – startService, stopService Method
 public abstract ComponentName startService(Intent service)
 Request that a given application service be started
 parameters
 service : Identifies the service to be started. The Intent may specify either an explicit component name to
start, or a logical description (action, category, etc) to match an IntentFilter published by a service. Additional
values may be included in the Intent extras to supply arguments along with this specific start call
 Returns
 If the service is being started or is already running, the ComponentName of the actual service that was
started is returned; else if the service does not exist null is returned.
 public abstract boolean stopService(Intent service)
 Request that a given application service be stopped. If the service is not running, nothing happens. Otherwise it
is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it
was started.
 Parameters
 service : Description of the service to be stopped. The Intent may specify either an explicit component name
to start, or a logical description (action, category, etc) to match an IntentFilter published by a service.
 Returns
 If there is a service matching the given Intent that is already running, then it is stopped and true is returned;
else false is returned.
Service 처리과정
 Service 주기
 Activity에서 startService()를 호출하면
 Service의 onCreate() -> onStartCommand() 순서대로 처리
Service 예제 : Music Player Service-Project, Activity
구현한 서비스
mp3를 play, stop함
Service 예제 : Music Player ServiceAndroidManifest.xml
프로젝트의 AndroidManifest에 service를 등록
Service 예제 : Music Player Service-MainActivity.java
Activit에서 service를 호출
Service 예제 : Music Player Service-MyService.java
Broadcast Receiver
 BroadcastReceiver
 Component들 간에 메시지를 전달
Context class – sendBroadcast Method
 public abstract void sendBroadcast(Intent intent)
 Broadcast the given intent to all interested BroadcastReceivers. This call is
asynchronous; it returns immediately, and you will continue executing while the
receivers are run. No results are propagated from receivers and receivers can not abort
the broadcast
 Parmaters
 intent : The Intent to broadcast; all receivers matching this Intent will receive the
broadcast.
Toast class
 Overview
 A toast is a view containing a quick little message for the user. The toast class helps you
create and show those.
When the view is shown to the user, appears as a floating view over the application. It will
never receive focus. The user will probably be in the middle of typing something else. The
idea is to be as unobtrusive as possible, while still showing the user the information you
want them to see. Two examples are the volume control, and the brief message saying that
your settings have been saved
 public Method
 public static Toast makeText(Coontext, CharSequence text, int duration)
 Make a standard toast that just contains a text view.
 Parameters
 context : The context to use. Usually your Application or Activity object.
 text : The text to show. Can be formatted text.
 duration : How long to display the message. Either LENGTH_SHORT or LENGTH_LONG.
Broadcast Receiver (방송하는 패키지)
Broadcast Receiver-정적 Receiver
 Receiver를 고정해서 등록하고 원하는 방송에 반응하는 Receiver
Broadcast Receiver-정적 Receiver 결과
Broadcast Receiver-동적 Receiver
 AndroidManifest.xml에 등록하지 않음
IntentFilter를 생성하여 방송과 매칭될 Action과 데이터를 등록
BroadcastReceiver 객체를 생성, onReceiver() 함수 구현
방송이 들어오면 onReceiver()함수가 호출
BroadcastReciver 객체를 등록
Broadcast Receiver-동적 Receiver 결과
Broadcast Receiver-동적 Receiver 결과
Broadcast Receiver-동적 Receiver
 동적 리시버의 특징
 자신을 등록한 Component의 생명 주기가 끝나명 사라짐
 unregisterReceiver() 함수 : 등록 해제
 Component 내에 Receiver 소스가 존재하므로 Activity 내에 모든 맴버 객체에 접근
가능
 정적 Receiver의 경우 특정 Component에 포함되어 있지 않으므로 멤버변수에 접근
불가능
처리 과정
app
Main Acitivity
1. startService(intent)
ⓒ
ⓐ
BroadcastReceiver
name : restReceiver.wins.or.kr.service
5. intent.getExtras()
6. send data to mainActivity
ⓑ
RecognitionService(background)
servicename : voice.wins.or.kr.service
2. Recognition
3. intent.putExtra(value)
4. sendBroadcast(intent)
ServiceClient-project, AndroidManifest.xml
프로젝트의 AndroidManifest에 Receiver 등록
ServiceClient-MainActivity.java
•
onResume() / onPause()
•
onStart() / onStop()
Activity 위에 다른 Activity가 올라왔거나 focus를 읽었거나 다시 얻을 때 불림
Activity가 완전히 화면을 벗어날때
예 : 홈키를 눌러 홈화면으로 이동
ServiceClient-MainActivity.java
Service 호출
ServiceClient-TestReceiver.java
testService-project, AndroidManifest.xml
RecognitionService-VoiceRecognitionService.java
RecognitionService-VoiceRecognitionService.java
RecognitionService-VoiceRecognitionService.java
RecognitionService-VoiceRecognitionService.java