Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAP 13. 서비스와 방송 수신자.

Similar presentations


Presentation on theme: "CHAP 13. 서비스와 방송 수신자."— Presentation transcript:

1 CHAP 13. 서비스와 방송 수신자

2 서비스 사용자 인터페이스 없이 백그라운드에서 실행되는 컴포넌트 배경 음악을 재생 웹 사이트에서 주기적으로 데이터를 읽는다.
사용자 인터페이스 없이 백그라운드에서 실행되는 컴포넌트 배경 음악을 재생 웹 사이트에서 주기적으로 데이터를 읽는다. 주기적으로 폰의 사용량을 계산 애플리케이션의 업데이트를 주기적으로 검사

3 실제 실행중인 서비스

4 서비스의 종류 시작 타입의 서비스(started service) 연결 타입의 서비스(bound service)
액티비티가 startService()를 호출하여서 서비스를 시작 연결 타입의 서비스(bound service) 액티비티가 bindService()를 호출하여서 서비스를 시작

5 음악을 연주하는 서비스 public class MusicService extends Service { private static final String TAG = "MusicService"; MediaPlayer player; public IBinder onBind(Intent intent) { return null; } public void onCreate() { Log.d(TAG, "onCreate()"); player = MediaPlayer.create(this, R.raw.old_pop); player.setLooping(false); // Set looping

6 리소스 준비 mp3 형식의 음악 파일을 하나 다운로드받아서 /res/raw 디렉토리에 old_pop.mp3와 같은 이름으 로 저장한다.

7 음악을 연주하는 서비스 public void onDestroy() { Toast.makeText(this, "Music Service가 중지되었습니다.", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy()"); player.stop(); } public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "Music Service가 시작되었습니다.", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart()"); player.start(); return super.onStartCommand(intent, flags, startId);

8 매니페스트 파일 <manifest ... <application <activity ... </activity> <service android:enabled="true" android:name=".MusicService“ /> </application> </manifest>

9 서비스 사용 예제 앞의 서비스를 사용하는 예제를 작성 먼저 다음과 같은 인터페이스를 XML로 작성

10 서비스 실행

11 MusicserviceTest.java ...
public class MusicServiceTest extends Activity implements OnClickListener { private static final String TAG = "MusicServiceTest"; Button start, stop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start = (Button) findViewById(R.id.start); stop = (Button) findViewById(R.id.stop); start.setOnClickListener(this); stop.setOnClickListener(this); }

12 MusicserviceTest.java public void onClick(View src) {
switch (src.getId()) { case R.id.start: Log.d(TAG, "onClick() start "); startService(new Intent(this, MusicService.class)); break; case R.id.stop: Log.d(TAG, "onClick() stop"); stopService(new Intent(this, MusicService.class)); }

13 실행 결과

14 연결 타입의 서비스

15 서비스 인터페이스를 정의하는 3가지의 방법 Binder 클래스 확장하기 Messenger 클래스 사용하기 Using AIDL

16 Binder 클래스 확장하기 예제 난수를 발생시키서 다른 컴포넌트에게 서비스하는 연결된 타입의 서비스를 작성하여 보자.

17

18 연결 타입의 서비스를 테스트하는 애플리케이션

19 연결 타입의 서비스를 테스트하는 애플리케이션

20 실행 결과

21 방송 수신자 안드로이드 장치에서는 많은 이벤트들이 발생한다. 이벤트를 받는 컴포넌트가 방송 수신자이다.

22 방송 수신자의 구조

23 방송 수신자의 인텐트 필터

24 문자 메시지를 받는 방송 수신자

25 실행 결과

26 애플리케이션이 방송하기 sendBroadcast(Intent intent)
sendBroadcast(Intent intent, String receiverPermission) sendOrderedBroadcast(Intent intent, String receiverPermission) 매개 변수 intent는 방송할 액션이다. 매개 변수 receiverPermission은 지정된 권한을 가진 수신자에게만 방송을 보내고 싶을 때 사용한다.

27 예제 간단하게 두 개의 애플리케이션을 작성하여서 한쪽 에서는 방송을 보내고 다른 쪽에서는 방송을 수신하 여 보자.

28 방송 수신자 ... public class MyBroadcastReceiver extends BroadcastReceiver public void onReceive(Contextcontext, Intent intent) { Uri uri = Uri.parse(" Intent intent1 = new Intent(Intent.ACTION_VIEW,uri); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); }

29 매니페스트 파일 <?xml version="1.0" encoding="utf-8"?> <manifest ... <application <receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="kr.co.company.START_WEB" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="9" /> </manifest>

30 테스트 애플리케이션 ... public class MyBroadcastSender extends Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button click = (Button) findViewById(R.id.click); click.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setAction("kr.co.company.START_WEB"); sendBroadcast(intent); } });

31 방송 수신자 예제 실행 결과


Download ppt "CHAP 13. 서비스와 방송 수신자."

Similar presentations


Ads by Google