로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수
학습 목표 Broadcasting Service 2
브로드캐스팅 3 BroadcastReceiver sendBroadcast Intent
브로드캐스팅 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
시스템 브로드캐스팅 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 public class MainActivity extends RobotActivity { private Device leftEyeDevice; private Device rightEyeDevice; private BroadcastReceiver br = new BroadcastReceiver() 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); } };
시스템 브로드캐스팅 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); protected void onDestroy() { super.onDestroy(); unregisterReceiver(br); public void onInitialized(Robot robot) { super.onInitialized(robot); leftEyeDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_EYE); rightEyeDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_EYE); }
서비스 8 Object Context ContextWrapper Service
서비스 만들기 9 public class MyService extends Service public IBinder onBind(Intent intent) { return null; }
서비스 만들기 10 <manifest xmlns:android=" package="com.example.unit23" android:versionCode="1" android:versionName="1.0" >
서비스 호출하기 ( 명시적 인텐트 ) 11 public class MainActivity extends Activity { private Intent protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(this, MyService.class); startService(intent); protected void onDestroy() { super.onDestroy(); stopService(intent); }
서비스 호출하기 ( 명시적 인텐트 ) 12 public class MainActivity extends Activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, MyService.class)); protected void onDestroy() { super.onDestroy(); stopService(new Intent(this, MyService.class)); }
서비스 생명주기 13
서비스 생명주기 14 public class MyService extends Service public void onCreate() { Log.v("Example", "onCreate"); super.onCreate(); public void onDestroy() { Log.v("Example", "onDestroy"); super.onDestroy(); public int onStartCommand(Intent intent, int flags, int startId) { Log.v("Example", "onStartCommand"); return super.onStartCommand(intent, flags, startId); public IBinder onBind(Intent intent) { return null; }
서비스 호출하기 ( 암시적 인텐트 ) 15 <manifest xmlns:android=" package="com.example.unit23" android:versionCode="1" android:versionName="1.0" >
서비스 호출하기 ( 암시적 인텐트 ) 16 public class MainActivity extends Activity { private Intent protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent("example.android.SERVICE"); startService(intent); protected void onDestroy() { super.onDestroy(); stopService(intent); }
서비스 호출하기 ( 암시적 인텐트 ) 17 public class MainActivity extends Activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent("example.android.SERVICE")); protected void onDestroy() { super.onDestroy(); stopService(new Intent("example.android.SERVICE")); }
마이크 서비스 18 public class MyService extends Service { private MicThread public void onCreate() { super.onCreate(); thread = new MicThread(); thread.start(); public void onDestroy() { super.onDestroy(); if(thread != null) thread.cancel(); public IBinder onBind(Intent intent) { return null; }
마이크 서비스 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 = 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 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 /= ; 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 public class MainActivity extends RobotActivity { private Device leftWheelDevice; private Device rightWheelDevice; private BroadcastReceiver br = new BroadcastReceiver() 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); } };
마이크 서비스 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")); protected void onDestroy() { super.onDestroy(); unregisterReceiver(br); stopService(new Intent("example.android.SERVICE")); public void onInitialized(Robot robot) { super.onInitialized(robot); leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL); rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL); }
마이크 서비스 23 <manifest xmlns:android=" package="com.example.unit23" android:versionCode="1" android:versionName="1.0" >
숙제 가속도 센서 서비스 만들기 – 서비스에서 폰의 가속도 센서 X 축, Y 축 값 읽기 –X 축, Y 축 값을 브로드캐스팅으로 보내기 – 액티비티에서 브로드캐스팅 받아서 로봇 움직이기 24