Presentation is loading. Please wait.

Presentation is loading. Please wait.

로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Broadcasting Service 2.

Similar presentations


Presentation on theme: "로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Broadcasting Service 2."— Presentation transcript:

1 로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수

2 학습 목표 Broadcasting Service 2

3 브로드캐스팅 3 BroadcastReceiver sendBroadcast Intent

4 브로드캐스팅 BroadcastReceiver –public abstract void onReceive (Context context, Intent intent) Context –public abstract Intent registerReceiver (BroadcastReceiver rec eiver, IntentFilter filter) –public abstract void unregisterReceiver (BroadcastReceiver receiver) –public abstract void sendBroadcast (Intent intent) 4

5 시스템 브로드캐스팅 ACTION_TIME_TICK ACTION_TIME_CHANGED ACTION_TIMEZONE_CHANGED ACTION_BOOT_COMPLETED ACTION_PACKAGE_ADDED ACTION_PACKAGE_CHANGED ACTION_PACKAGE_REMOVED ACTION_PACKAGE_RESTARTED ACTION_PACKAGE_DATA_CLEARED ACTION_UID_REMOVED ACTION_BATTERY_CHANGED ACTION_POWER_CONNECTED ACTION_POWER_DISCONNECTED ACTION_SHUTDOWN 5

6 시스템 브로드캐스팅 6 public class MainActivity extends RobotActivity { private Device leftEyeDevice; private Device rightEyeDevice; private BroadcastReceiver br = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(leftEyeDevice != null && rightEyeDevice != null) { String action = intent.getAction(); if(Intent.ACTION_POWER_CONNECTED.equals(action)) { leftEyeDevice.write(0, 255); rightEyeDevice.write(0, 255); } else if(Intent.ACTION_POWER_DISCONNECTED.equals(action)) { leftEyeDevice.write(0, 0); rightEyeDevice.write(0, 0); } };

7 시스템 브로드캐스팅 7 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_POWER_CONNECTED); filter.addAction(Intent.ACTION_POWER_DISCONNECTED); registerReceiver(br, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(br); } @Override public void onInitialized(Robot robot) { super.onInitialized(robot); leftEyeDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_EYE); rightEyeDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_EYE); }

8 서비스 8 Object Context ContextWrapper Service

9 서비스 만들기 9 public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; }

10 서비스 만들기 10 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.unit23" android:versionCode="1" android:versionName="1.0" >

11 서비스 호출하기 ( 명시적 인텐트 ) 11 public class MainActivity extends Activity { private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(this, MyService.class); startService(intent); } @Override protected void onDestroy() { super.onDestroy(); stopService(intent); }

12 서비스 호출하기 ( 명시적 인텐트 ) 12 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, MyService.class)); } @Override protected void onDestroy() { super.onDestroy(); stopService(new Intent(this, MyService.class)); }

13 서비스 생명주기 13

14 서비스 생명주기 14 public class MyService extends Service { @Override public void onCreate() { Log.v("Example", "onCreate"); super.onCreate(); } @Override public void onDestroy() { Log.v("Example", "onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.v("Example", "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { return null; }

15 서비스 호출하기 ( 암시적 인텐트 ) 15 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.unit23" android:versionCode="1" android:versionName="1.0" >

16 서비스 호출하기 ( 암시적 인텐트 ) 16 public class MainActivity extends Activity { private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent("example.android.SERVICE"); startService(intent); } @Override protected void onDestroy() { super.onDestroy(); stopService(intent); }

17 서비스 호출하기 ( 암시적 인텐트 ) 17 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent("example.android.SERVICE")); } @Override protected void onDestroy() { super.onDestroy(); stopService(new Intent("example.android.SERVICE")); }

18 마이크 서비스 18 public class MyService extends Service { private MicThread thread; @Override public void onCreate() { super.onCreate(); thread = new MicThread(); thread.start(); } @Override public void onDestroy() { super.onDestroy(); if(thread != null) thread.cancel(); } @Override public IBinder onBind(Intent intent) { return null; }

19 마이크 서비스 19 private class MicThread extends Thread { private static final int FREQ = 44100; private static final int CHANNEL = AudioFormat.CHANNEL_IN_MONO; private static final int ENCODING = AudioFormat.ENCODING_PCM_16BIT; private boolean running = true; @Override public void run() { int bufferSize = AudioRecord.getMinBufferSize(FREQ, CHANNEL, ENCODING) * 3; if(bufferSize <= 0) return; AudioRecord audioRecord = new AudioRecord(AudioSource.MIC, FREQ, CHANNEL, ENCODING, bufferSize); if(audioRecord.getState() != android.media.AudioRecord.STATE_INITIALIZED) { audioRecord.release(); return; } short[] audioData = new short[bufferSize]; audioRecord.startRecording();

20 마이크 서비스 20 int len, v; long sum; while(running) { len = audioRecord.read(audioData, 0, bufferSize); if(running == false) break; sum = 0; for(int i = 0; i < len; ++i) { v = Math.abs(audioData[i]); sum += v; } sum /= 100000; if(running == false) break; Intent intent = new Intent("example.intent.action.MICROPHONE"); intent.putExtra("example.intent.extra.MIC_LEVEL", (int)sum); sendBroadcast(intent); } audioRecord.stop(); audioRecord.release(); audioRecord = null; } void cancel() { running = false; }

21 마이크 서비스 21 public class MainActivity extends RobotActivity { private Device leftWheelDevice; private Device rightWheelDevice; private BroadcastReceiver br = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if("example.intent.action.MICROPHONE".equals(action)) { int level = intent.getIntExtra("example.intent.extra.MIC_LEVEL", 0); if(level > 100) { leftWheelDevice.write(-30); rightWheelDevice.write(-30); } else { leftWheelDevice.write(0); rightWheelDevice.write(0); } };

22 마이크 서비스 22 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registerReceiver(br, new IntentFilter("example.intent.action.MICROPHONE")); startService(new Intent("example.android.SERVICE")); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(br); stopService(new Intent("example.android.SERVICE")); } @Override public void onInitialized(Robot robot) { super.onInitialized(robot); leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL); rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL); }

23 마이크 서비스 23 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.unit23" android:versionCode="1" android:versionName="1.0" >

24 숙제 가속도 센서 서비스 만들기 – 서비스에서 폰의 가속도 센서 X 축, Y 축 값 읽기 –X 축, Y 축 값을 브로드캐스팅으로 보내기 – 액티비티에서 브로드캐스팅 받아서 로봇 움직이기 24


Download ppt "로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Broadcasting Service 2."

Similar presentations


Ads by Google