Androidã§å¦ã¶ä¸¦åå¦çã¨GUI
Docomo製Android端æ«ã®çºå£²æ¥ãçºè¡¨ããã¾ãããæ¥æã®æå¹Javaã³ãã¥ããã£ãAndroidã¤ãã³ãã¨ãããã¨ãããããã¥ã¼ããªã¢ã«ãä½æããããã¦ãã¾ããã¡ãã£ã¨ã¹ã¬ããå¨ãã§ã¯ã¾ã£ãäºããããç°¡åãªæè¨ã¢ããªã±ã¼ã·ã§ã³ã®ä½ãæ¹ãããAndroidã®ã¹ã¬ããæç»ã¢ãã«ãç´¹ä»ãã¾ãã
ã¨ããããã½ã¼ã¹
ãã®ã¢ããªã±ã¼ã·ã§ã³ã¯ã0.5ç§æ¯ã«æ¥æãæ´æ°ããåç´ãªæè¨ã¢ããªã±ã¼ã·ã§ã³ã§ãã
package com.example.android; // import ç¥ public class Clock extends Activity { private ScheduledExecutorService service; private Handler handler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TextView textView = new TextView(this); textView.setText(new Date().toString()); setContentView(textView); service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(new Runnable() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { textView.setText(new Date().toString()); } }); } }, 0, 500, TimeUnit.MILLISECONDS); } @Override protected void onDestroy() { super.onDestroy(); service.shutdown(); } }
ãã¤ã³ãã¯ï¼ã¤ãããjava.util.concurrent.ScheduledExecutorService 㨠android.os.Handlerã§ãã
å®æçã«å¦çãå®è¡ãã
java.util.concurrent.ScheduledExecutorServiceã¯Java5ããå°å ¥ãããconcurrent ãã¬ã¼ã ã¯ã¼ã¯ã«å«ã¾ããã¤ã³ã¿ã¼ãã§ã¤ã¹ã§ããconcurrent ãã¬ã¼ã ã¯ã¼ã¯ã¯ä¸¦åå¦çãå¼·åã«ãµãã¼ãããã©ã¤ãã©ãªã§ã並åå¦çãèããä¸ã§ã¯å¿ ä¿®ãªã©ã¤ãã©ãªã§ãããã®ã³ã³ã»ããã¯ãå¦çããã人ï¼ã¹ã¬ããï¼ã¨ãã®ç®¡çãå®éã«è¡ãå¦çã¨åãããã¨ã§ããã¹ã¬ããã®çæãã¹ã¬ããã®ç®¡çã«é¢ãã¦ã¯ç¹æ®ãªãã¨ããããªãéãã¯ä¼¼ããããªç®¡çããå¿ è¦ããã¾ãããä¾ãã°ï¼ã¤ã®ã¹ã¬ãããé 次å¦çãããããæ大10åã®ã¹ã¬ãããåããã以ä¸ã®å¦çã¯å¾ æ©ãããªã©ã§ããããã«å¯¾ããå¦çã¯ããã°ã©ã ã«ãã£ã¦ããããã§ãããããã£ã¦ãconcurrentãã¬ã¼ã ã¯ã¼ã¯ã§ã¯ãããã°ã©ããå¦çãããé¨åã«éä¸ãã¦ã³ã¼ãã£ã³ã°ã§ããããã«è¨è¨ããã¦ãã¾ãã
Executorsã¯ã©ã¹
Executorsã¯ã©ã¹ã¯concurrent ãã¬ã¼ã ã¯ã¼ã¯ã§æä¾ãããã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã§ããããããããªä¸¦åå¦çãè¡ããµã¼ãã¹ãä½æãã¾ããä»åã¯ãå®æçã«å¦çãè¡ãããã®ã§newSingleThreadScheduledExecutorã使ããScheduledExecutorServiceã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¦ãã¾ããåæã«è²ã ã¨å¦çããããã§ã¯ãªãã®ã§ï¼åã®ã¹ã¬ããã§æ§ãã¾ããã
ScheduledExecutorService
ScheduledExecutorServiceã¯å®æçãªå®è¡ãä½æéå¾ã«å®è¡ãªã©ã®APIãæä¾ãããµã¼ãã¹ã§ããä»åã¯scheduleAtFixedRateã使ç¨ãã¦ãã¾ãããã®ã¡ã½ããã¯ç¬¬ï¼å¼æ°ã«Runnableã®å®è£
ã¯ã©ã¹ãä¸ããæ®ãã®å¼æ°ã§ã©ã®ãããã®æéã§å®è¡ããããæå®ãã¾ããã½ã¼ã¹ã§ã¯0ç§ã®ãã£ã¬ã¤ãã¨ã£ã¦ã500ããªç§æ¯ã«ç¹°ãè¿ãå®è¡ãããããªæå®ã«ãªã£ã¦ãã¾ãã
ä»ã«ãããã¾ã§å¦çééãæå®ãã¦å®è¡ãããscheduleWithFixedDelayããæå®æéå¾ã«ï¼åã ãå®è¡ããscheduleã¡ã½ãããããã¾ãã
GUIã®æ´æ°ãè¡ãã¹ã¬ãã
æè¿ã®GUIã¯å
¨ã¦ã·ã³ã°ã«ã¹ã¬ããã¢ãã«ã§ãã¶ã¤ã³ããã¦ãã¾ããã¤ã¾ããGUIã®æ´æ°ãªã©ãè¡ãã¹ã¬ããã¯ï¼ã¤ãããªãã®ã§ããããã¯Androidãåæ§ã§ããããã¯ä¸æã«ãã«ãã¹ã¬ããã§GUIãæ´æ°ããã¨è¨è¨ãè¤éã«ãªãã使ãæ¹ãé£ãããªãããã§ãã
ãããã£ã¦ãAndroidã®ã³ã¼ããæ¸ãå ´åããã®GUIãæ´æ°ãããããªå¦çãã©ã®ã¹ã¬ããã§å®è¡ããã¦ããããæèããªãã¦ã¯ãªãã¾ãããã¨ã¯ãããèªåã§ã¹ã¬ãããèµ·ãããªãéãã¯å
¨ã¦ã®å¦çã¯åãã¹ã¬ããã§è¡ããã¾ããåé¡ã¯ã¹ã¬ãããèªåã§ä½æããå ´åã§ããã®ä¸ã§è¡ãããå¦çã«GUIã®æç»ã«é¢é£ããå¦çã¯çµ¶å¯¾ã«å«ãã¦ã¯ãªãã¾ãã*1ã
Androidã§ã¯ãã®ãããªäºãå®ç¾ããããã«ã android.os.Handlerã¨ããã¯ã©ã¹ãç¨æããã¦ãã¾ãããã®ã¯ã©ã¹ã¯å
é¨çã«ã¤ãã³ãã®ãã¥ã¼ãæã£ã¦ããããã¥ã¼ã«æç»ã¤ãã³ããæºãã¦ããããå¾ã§ãGUIã¹ã¬ããã«å®è¡ããã¾ããå
·ä½çã«ã¯Handlerã®postã¡ã½ããã§ããHandlerã®postã¡ã½ããã¯Runnableã¤ã³ã¿ã¼ãã§ã¤ã¹ãåãåãããã¥ã¼ã«æºãã¾ãããããã£ã¦ãHandlerã«postããã ããªãã°èªä½ã¹ã¬ããã§è¡ã£ã¦ãæ§ããªãã®ã§ãã
èªä½ã¹ã¬ããã§ãã¥ã¼ãæ´æ°ããã¨ãããããã¯ãã
Swingã§ã¯èªä½ã¹ã¬ããã§setTextãªãããå¼ãã§ãã¾ã£ã¦ãã¨ããããåãã¦ãã¾ããæ¬å½ã¯ãã¡ãªãã§ãããããç¨åº¦ã¯åãã®ã§ãµã³ãã«ãªã©ã§ã¯æèãã¦ããªãã±ã¼ã¹ãããã¾ããã§ãããAndroidã§ã¯èªä½ã¹ã¬ããã§setTextãªãããå¼ã¶ã¨ã»ã¼ç¢ºå®ã«ããã¯ãã¾ãã®ã§æ³¨æãã¦ãã ããã
service.scheduleAtFixedRate(new Runnable() { // ãã£ã¨ããéã«ããã¯ãã¦ãã¾ãã¾ãã textView.setText(new Date().toString()); }, 0, 500, TimeUnit.MILLISECONDS);
Handlerã®ã¹ã³ã¼ãã«æ³¨æ
Handlerã¯è¤æ°ä½ããã¨ãå¯è½ã§ããæçµçã«ç®¡çããããã¥ã¼ã¯ï¼ã¤ããããã¾ããããHandlerã®ã³ã³ã¹ãã©ã¯ã¿ã®ä¸ã§ã·ã³ã°ã«ãã³ãªLooperã¤ã³ã¹ã¿ã³ã¹ãããã¥ã¼ãåå¾ãã¦ãããããåé¡ã«ãªãã¾ããããããã次ã®ããã«æ¸ãã¦ãã¾ãã¨ãã¯ãããã¯ãã¾ãã
service.scheduleAtFixedRate(new Runnable() { @Override public void run() { new Handler().post(new Runnable() { @Override public void run() { textView.setText(new Date().toString()); } }); } }, 0, 500, TimeUnit.MILLISECONDS);
ä¸è¦ãå¥ã¹ã¬ããããã§ãããæç»ã¹ã¬ããã¸ã®ä¾é ¼ã®ããã«Handlerã使ãå¦çããã¥ã¼ã¤ã³ã°ãã¦ãã¾ããç»é²ããããã¥ã¼ã¯ï¼ã¤ãªã®ã§ä¸æãåãã¯ãã§ããããããããã¯ãã¾ãã詳ããã¯èª¿æ»ãã¦ãã¾ããããHandlerã®åç
§ã®åé¡ãªã®ããããã¾ããã解決ããçºã«ã¯ãæåã®ãµã³ãã«ã®ããã«HandlerãActibityã®ãã£ã¼ã«ãã«ãã¾ãã
ããã§èªåã®ä½æããã¹ã¬ãããå®æçã«æç»ä¾é ¼ãè¡ããæç»ä¾é ¼ãåããGUIã¹ã¬ãããæ°ããæå»ã¸ããã¹ããå¤æ´ãã¦ããããã«ãªãã¾ãããåç´ãªä¾ãªã®ã§å§è²ã»å§è²ã¨é¢åãªãã¨ã«ãªã£ã¦ãã¾ãããGUIãä½ãä¸ã§ã¯ãããä¸çªã·ã³ãã«ãªã®ã§ãã
Messageãªãã¸ã§ã¯ãã«ããã®ãã¥ã¼ã¤ã³ã°
Handlerã¯Runnableã®ãã¥ã¼ãåãä»ããã ãã§ãªããMessageãªãã¸ã§ã¯ãã«ããã®ãã¥ã¼ã¤ã³ã°ãå¯è½ã§ãã
private ScheduledExecutorService service; private Handler handler; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TextView textView = new TextView(this); textView.setText(new Date().toString()); setContentView(textView); handler = new Handler() { public void handleMessage(android.os.Message msg) { textView.setText((String) msg.obj); }; }; service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(new Runnable() { @Override public void run() { Message msg = new Message(); msg.obj = new Date().toString(); handler.sendMessage(msg); } }, 0, 500, TimeUnit.MILLISECONDS); }
注æãã¦æ¬²ããã®ã¯ãä»ã®ã¹ã¬ãããããã¥ã¼ã¤ã³ã°ããã®ã¯sendMessageã§ããããå¦çããã®ã¯handleMessageã ã¨ãããã¨ã§ããéãã¡ãã»ã¼ã¸ã¨å®éã«æç»ããå 容ãã¾ã£ããé¢ãããããªå ´åã¯æå¹ã§ãããåç´ãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯Runnableã§postããæ¹ã解ããããã§ããããåé¢ããæå³ãããã°æ¤è¨ãã¦ãã ããã
å¾å§æ«ã¯ã¤ãããã¨
Executorã¯ã¹ã¬ããã«é¢ããã©ã¤ããµã¤ã¯ã«ããã¹ã¦ä»»ãããã¨ãã§ããããã§ããããã使ããªããªã£ããªãã°å¾å§æ«ããå¿ è¦ãããã¾ããããããªãã¨ããã®ã¹ã¬ããã¯æ°¸é ã«å½·å¾¨ãç¶ãã¦ãã¾ãã¾ãã
@Override protected void onDestroy() { super.onDestroy(); service.shutdown(); }
*1:ããã¯Swingãåæ§ã§ã