로봇 모니터링 1/2 UNIT 20 로봇 SW 콘텐츠 교육원 조용수
학습 목표 Message Queue Handler 2
핸들러 메시지 큐 3 큐 메시지 핸들러 루퍼
Message public int what public int arg1 public int arg2 public Object obj 4
Handler –Message obtainMessage() –Message obtainMessage(int what) –Message obtainMessage(int what, Object obj) –Message obtainMessage(int what, int arg1, int arg2) –Message obtainMessage(int what, int arg1, int arg2, Object obj) Message –static Message obtain(Handler h) –static Message obtain(Handler h, int what) –static Message obtain(Handler h, int what, Object obj) –static Message obtain(Handler h, int what, int arg1, int arg2) –static Message objtain(Handler h, int what, int arg1, int arg2, Object obj) 5
Message 6 Message msg = handler.obtainMessage(); msg.what = 0; msg.arg1 = 1; handler.sendMessage(msg); Message msg = handler.obtainMessage(0); msg.arg1 = 1; handler.sendMessage(msg); Message msg = handler.obtainMessage(0, 1, 0); handler.sendMessage(msg); Message msg = Message.obtain(handler, 0, 1, 0); handler.sendMessage(msg); Message msg = Message.obtain(handler, 0, 1, 0); msg.sendToTarget();
핸들러 7 <LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" />
핸들러 8 public class MainActivity extends Activity { private Button showButton; private TextView textView; private Handler handler = new Handler() public void handleMessage(Message msg) { switch(msg.what) { case 0: textView.setText("received: " + msg.arg1); break; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showButton = (Button)findViewById(R.id.show); textView = (TextView)findViewById(R.id.text); showButton.setOnClickListener(new OnClickListener() public void onClick(View v) { Message msg = handler.obtainMessage(); msg.what = 0; msg.arg1 = 1; handler.sendMessage(msg); } }); }
핸들러 9 public class MainActivity extends Activity { private Button showButton; private TextView textView; private Handler handler = new Handler() public void handleMessage(Message msg) { switch(msg.what) { case 0: textView.setText("received: " + msg.arg1); break; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showButton = (Button)findViewById(R.id.show); textView = (TextView)findViewById(R.id.text); showButton.setOnClickListener(new OnClickListener() public void onClick(View v) { Message msg = handler.obtainMessage(0); msg.arg1 = 1; handler.sendMessage(msg); } }); }
핸들러 10 public class MainActivity extends Activity { private Button showButton; private TextView textView; private Handler handler = new Handler() public void handleMessage(Message msg) { switch(msg.what) { case 0: textView.setText("received: " + msg.arg1); break; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showButton = (Button)findViewById(R.id.show); textView = (TextView)findViewById(R.id.text); showButton.setOnClickListener(new OnClickListener() public void onClick(View v) { Message msg = handler.obtainMessage(0, 1, 0); handler.sendMessage(msg); } }); }
핸들러 11 public class MainActivity extends Activity { private Button showButton; private TextView textView; private Handler handler = new Handler() public void handleMessage(Message msg) { switch(msg.what) { case 0: textView.setText("received: " + msg.arg1); break; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showButton = (Button)findViewById(R.id.show); textView = (TextView)findViewById(R.id.text); showButton.setOnClickListener(new OnClickListener() public void onClick(View v) { Message.obtain(handler, 0, 1, 0).sendToTarget(); } }); }
핸들러 12 public class MainActivity extends Activity { private Button showButton; private TextView textView; private Handler handler = new Handler() public void handleMessage(Message msg) { switch(msg.what) { case 0: textView.setText("received: " + msg.obj); break; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showButton = (Button)findViewById(R.id.show); textView = (TextView)findViewById(R.id.text); showButton.setOnClickListener(new OnClickListener() public void onClick(View v) { handler.obtainMessage(0, "test").sendToTarget(); } }); }
로봇 모니터링 13 public class MainActivity extends RobotActivity { private TextView leftProximityTextView; private TextView protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); leftProximityTextView = (TextView)findViewById(R.id.leftProximity); rightProximityTextView = (TextView)findViewById(R.id.rightProximity); public void onDeviceDataChanged(Device device, Object values, long timestamp) { int[] data; switch(device.getId()) { case Albert.SENSOR_LEFT_PROXIMITY: { data = (int[])values; leftProximityTextView.setText("Left Proximity: " + data[0]); } break; case Albert.SENSOR_RIGHT_PROXIMITY: { data = (int[])values; rightProximityTextView.setText("Right Proximity: " + data[0]); } break; }
로봇 모니터링 14 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views :15:30.216: E/JavaBinder(24444): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4924) :15:30.216:
로봇 모니터링 15 public class MainActivity extends RobotActivity { private TextView leftProximityTextView; private TextView rightProximityTextView; private Handler handler = new Handler() public void handleMessage(Message msg) { int[] values; switch(msg.what) { case Albert.SENSOR_LEFT_PROXIMITY: { values = (int[])msg.obj; leftProximityTextView.setText("Left Proximity: " + values[0] + "," + values[1] + "," + values[2] + "," + values[3]); } break; case Albert.SENSOR_RIGHT_PROXIMITY: { values = (int[])msg.obj; rightProximityTextView.setText("Right Proximity: " + values[0] + "," + values[1] + "," + values[2] + "," + values[3]); } break; } };
로봇 모니터링 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); leftProximityTextView = (TextView)findViewById(R.id.leftProximity); rightProximityTextView = (TextView)findViewById(R.id.rightProximity); public void onDeviceDataChanged(Device device, Object values, long timestamp) { handler.obtainMessage(device.getId(), values).sendToTarget(); }
로봇 모니터링 public void onDeviceDataChanged(final Device device, final Object values, long timestamp) { runOnUiThread(new Runnable() public void run() { int[] data; switch(device.getId()) { case Albert.SENSOR_LEFT_PROXIMITY: { data = (int[])values; leftProximityTextView.setText("Left Proximity: " + data[0]); } break; case Albert.SENSOR_RIGHT_PROXIMITY: { data = (int[])values; rightProximityTextView.setText("Right Proximity: " + data[0]); } break; } }); }
과제 모든 센서 출력하기 on/physical/Albert.php 18