getting_started.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Getting started &mdash; android_core 0.0.0-SNAPSHOT documentation</title>
  7. <link rel="stylesheet" href="_static/haiku.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <link rel="stylesheet" href="_static/print.css" type="text/css" />
  10. <script type="text/javascript">
  11. var DOCUMENTATION_OPTIONS = {
  12. URL_ROOT: '',
  13. VERSION: '0.0.0-SNAPSHOT',
  14. COLLAPSE_INDEX: false,
  15. FILE_SUFFIX: '.html',
  16. HAS_SOURCE: true
  17. };
  18. </script>
  19. <script type="text/javascript" src="_static/jquery.js"></script>
  20. <script type="text/javascript" src="_static/underscore.js"></script>
  21. <script type="text/javascript" src="_static/doctools.js"></script>
  22. <script type="text/javascript" src="_static/theme_extras.js"></script>
  23. <link rel="top" title="android_core 0.0.0-SNAPSHOT documentation" href="index.html" />
  24. <link rel="prev" title="Building android_core" href="building.html" />
  25. </head>
  26. <body>
  27. <div class="header"><h1 class="heading"><a href="index.html">
  28. <span>android_core 0.0.0-SNAPSHOT documentation</span></a></h1>
  29. <h2 class="heading"><span>Getting started</span></h2>
  30. </div>
  31. <div class="topnav">
  32. <p>
  33. «&#160;&#160;<a href="building.html">Building android_core</a>
  34. &#160;&#160;::&#160;&#160;
  35. <a class="uplink" href="index.html">Contents</a>
  36. </p>
  37. </div>
  38. <div class="content">
  39. <div class="section" id="getting-started">
  40. <span id="id1"></span><h1>Getting started<a class="headerlink" href="#getting-started" title="Permalink to this headline">¶</a></h1>
  41. <p>Before diving into ROS enabled Android application development, you should be
  42. familiar with <em class="xref std std-ref">rosjava</em> and <a class="reference external" href="http://developer.android.com/training/index.html">Android
  43. application development</a> in general.</p>
  44. <div class="section" id="creating-a-new-android-application">
  45. <h2>Creating a new Android application<a class="headerlink" href="#creating-a-new-android-application" title="Permalink to this headline">¶</a></h2>
  46. <div class="admonition note">
  47. <p class="first admonition-title">Note</p>
  48. <p class="last">This is still a work in progress. There are many obvious limitations
  49. and the process will be improved in the near future.</p>
  50. </div>
  51. <p>Currently, the easiest way to create a new application is to create your own
  52. package in the android_core stack by copying one of the tutorial packages (e.g.
  53. android_tutorial_pubsub).</p>
  54. <div class="highlight-bash"><div class="highlight"><pre>roscd android_core
  55. cp -a android_tutorial_pubsub my_package
  56. </pre></div>
  57. </div>
  58. <p>After that, modify android_core/settings.gradle to include your new package.</p>
  59. <div class="highlight-bash"><div class="highlight"><pre>rosed android_core/settings.gradle
  60. ./gradlew my_package:clean my_package:debug
  61. </pre></div>
  62. </div>
  63. <p>At this point, you may interact with your Android projects as described in the
  64. <a class="reference external" href="http://developer.android.com/guide/developing/building/building-cmdline.html">Android documentation</a>. Please start there if the following quick start
  65. instructions are insufficient for you.</p>
  66. <p>Use <a class="reference external" href="http://ant.apache.org/">Apache Ant</a> to install your new Android application:</p>
  67. <div class="highlight-bash"><div class="highlight"><pre>roscd my_package
  68. ant installd
  69. </pre></div>
  70. </div>
  71. <p>You can also use ant to build the application. However, if you add, remove, or
  72. modify a dependency in the build.gradle file, you will need to execute the
  73. <a class="reference external" href="http://gradle.org/docs/current/userguide/gradle_wrapper.html">gradle wrapper</a> as described above in order to update the Android
  74. application&#8217;s external dependencies (located in the <tt class="docutils literal"><span class="pre">my_package/libs</span></tt>
  75. directory).</p>
  76. <div class="admonition note">
  77. <p class="first admonition-title">Note</p>
  78. <p class="last">You may also build and run your application from Eclipse. For more
  79. information, see <a class="reference internal" href="building.html"><em>Building android_core</em></a>.</p>
  80. </div>
  81. </div>
  82. <div class="section" id="using-rosactivity">
  83. <span id="life-of-a-rosactivity"></span><h2>Using RosActivity<a class="headerlink" href="#using-rosactivity" title="Permalink to this headline">¶</a></h2>
  84. <p>The <a class="reference external" href="javadoc/org/ros/android/RosActivity.html#">RosActivity</a> class is the base class for all of
  85. your ROS enabled Android applications. Let&#8217;s consider the following example
  86. from the android_tutorial_pubsub package. In this example, we create a
  87. <a class="reference external" href="javadoc/org/ros/node/topic/Publisher.html#">Publisher</a> and a
  88. <a class="reference external" href="javadoc/org/ros/node/topic/Subscriber.html#">Subscriber</a> that will exchange &#8220;Hello, World&#8221;
  89. messages.</p>
  90. <div class="highlight-java"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
  91. 2
  92. 3
  93. 4
  94. 5
  95. 6
  96. 7
  97. 8
  98. 9
  99. 10
  100. 11
  101. 12
  102. 13
  103. 14
  104. 15
  105. 16
  106. 17
  107. 18
  108. 19
  109. 20
  110. 21
  111. 22
  112. 23
  113. 24
  114. 25
  115. 26
  116. 27
  117. 28
  118. 29
  119. 30
  120. 31
  121. 32
  122. 33
  123. 34
  124. 35
  125. 36
  126. 37
  127. 38
  128. 39
  129. 40
  130. 41
  131. 42
  132. 43
  133. 44
  134. 45
  135. 46
  136. 47
  137. 48
  138. 49
  139. 50
  140. 51
  141. 52
  142. 53</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">package</span> <span class="n">org</span><span class="o">.</span><span class="na">ros</span><span class="o">.</span><span class="na">android</span><span class="o">.</span><span class="na">android_tutorial_pubsub</span><span class="o">;</span>
  143. <span class="kn">import</span> <span class="nn">android.os.Bundle</span><span class="o">;</span>
  144. <span class="kn">import</span> <span class="nn">org.ros.android.MessageCallable</span><span class="o">;</span>
  145. <span class="kn">import</span> <span class="nn">org.ros.android.RosActivity</span><span class="o">;</span>
  146. <span class="kn">import</span> <span class="nn">org.ros.android.view.RosTextView</span><span class="o">;</span>
  147. <span class="kn">import</span> <span class="nn">org.ros.node.NodeConfiguration</span><span class="o">;</span>
  148. <span class="kn">import</span> <span class="nn">org.ros.node.NodeMainExecutor</span><span class="o">;</span>
  149. <span class="kn">import</span> <span class="nn">org.ros.rosjava_tutorial_pubsub.Talker</span><span class="o">;</span>
  150. <span class="cm">/**</span>
  151. <span class="cm"> * @author [email protected] (Damon Kohler)</span>
  152. <span class="cm"> */</span>
  153. <span class="hll"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MainActivity</span> <span class="kd">extends</span> <span class="n">RosActivity</span> <span class="o">{</span>
  154. </span>
  155. <span class="kd">private</span> <span class="n">RosTextView</span><span class="o">&lt;</span><span class="n">std_msgs</span><span class="o">.</span><span class="na">String</span><span class="o">&gt;</span> <span class="n">rosTextView</span><span class="o">;</span>
  156. <span class="kd">private</span> <span class="n">Talker</span> <span class="n">talker</span><span class="o">;</span>
  157. <span class="kd">public</span> <span class="nf">MainActivity</span><span class="o">()</span> <span class="o">{</span>
  158. <span class="c1">// The RosActivity constructor configures the notification title and ticker</span>
  159. <span class="c1">// messages.</span>
  160. <span class="hll"> <span class="kd">super</span><span class="o">(</span><span class="s">&quot;Pubsub Tutorial&quot;</span><span class="o">,</span> <span class="s">&quot;Pubsub Tutorial&quot;</span><span class="o">);</span>
  161. </span> <span class="o">}</span>
  162. <span class="nd">@SuppressWarnings</span><span class="o">(</span><span class="s">&quot;unchecked&quot;</span><span class="o">)</span>
  163. <span class="nd">@Override</span>
  164. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onCreate</span><span class="o">(</span><span class="n">Bundle</span> <span class="n">savedInstanceState</span><span class="o">)</span> <span class="o">{</span>
  165. <span class="hll"> <span class="kd">super</span><span class="o">.</span><span class="na">onCreate</span><span class="o">(</span><span class="n">savedInstanceState</span><span class="o">);</span>
  166. </span><span class="hll"> <span class="n">setContentView</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">layout</span><span class="o">.</span><span class="na">main</span><span class="o">);</span>
  167. </span><span class="hll"> <span class="n">rosTextView</span> <span class="o">=</span> <span class="o">(</span><span class="n">RosTextView</span><span class="o">&lt;</span><span class="n">std_msgs</span><span class="o">.</span><span class="na">String</span><span class="o">&gt;)</span> <span class="n">findViewById</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">id</span><span class="o">.</span><span class="na">text</span><span class="o">);</span>
  168. </span> <span class="n">rosTextView</span><span class="o">.</span><span class="na">setTopicName</span><span class="o">(</span><span class="s">&quot;chatter&quot;</span><span class="o">);</span>
  169. <span class="n">rosTextView</span><span class="o">.</span><span class="na">setMessageType</span><span class="o">(</span><span class="n">std_msgs</span><span class="o">.</span><span class="na">String</span><span class="o">.</span><span class="na">_TYPE</span><span class="o">);</span>
  170. <span class="n">rosTextView</span><span class="o">.</span><span class="na">setMessageToStringCallable</span><span class="o">(</span><span class="k">new</span> <span class="n">MessageCallable</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">std_msgs</span><span class="o">.</span><span class="na">String</span><span class="o">&gt;()</span> <span class="o">{</span>
  171. <span class="nd">@Override</span>
  172. <span class="kd">public</span> <span class="n">String</span> <span class="nf">call</span><span class="o">(</span><span class="n">std_msgs</span><span class="o">.</span><span class="na">String</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
  173. <span class="k">return</span> <span class="n">message</span><span class="o">.</span><span class="na">getData</span><span class="o">();</span>
  174. <span class="o">}</span>
  175. <span class="o">});</span>
  176. <span class="o">}</span>
  177. <span class="nd">@Override</span>
  178. <span class="hll"> <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">init</span><span class="o">(</span><span class="n">NodeMainExecutor</span> <span class="n">nodeMainExecutor</span><span class="o">)</span> <span class="o">{</span>
  179. </span> <span class="n">talker</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Talker</span><span class="o">();</span>
  180. <span class="n">NodeConfiguration</span> <span class="n">nodeConfiguration</span> <span class="o">=</span> <span class="n">NodeConfiguration</span><span class="o">.</span><span class="na">newPrivate</span><span class="o">();</span>
  181. <span class="c1">// At this point, the user has already been prompted to either enter the URI</span>
  182. <span class="c1">// of a master to use or to start a master locally.</span>
  183. <span class="n">nodeConfiguration</span><span class="o">.</span><span class="na">setMasterUri</span><span class="o">(</span><span class="n">getMasterUri</span><span class="o">());</span>
  184. <span class="n">nodeMainExecutor</span><span class="o">.</span><span class="na">execute</span><span class="o">(</span><span class="n">talker</span><span class="o">,</span> <span class="n">nodeConfiguration</span><span class="o">);</span>
  185. <span class="c1">// The RosTextView is also a NodeMain that must be executed in order to</span>
  186. <span class="c1">// start displaying incoming messages.</span>
  187. <span class="n">nodeMainExecutor</span><span class="o">.</span><span class="na">execute</span><span class="o">(</span><span class="n">rosTextView</span><span class="o">,</span> <span class="n">nodeConfiguration</span><span class="o">);</span>
  188. <span class="o">}</span>
  189. <span class="o">}</span>
  190. </pre></div>
  191. </td></tr></table></div>
  192. <p>On line 14, we extend <a class="reference external" href="javadoc/org/ros/android/RosActivity.html#">RosActivity</a>. When our
  193. <a class="reference external" href="http://developer.android.com/reference/android/app/Activity.html">activity</a> starts, the <a class="reference external" href="javadoc/org/ros/android/RosActivity.html#">RosActivity</a> super class will:</p>
  194. <ul class="simple">
  195. <li>start the <a class="reference external" href="javadoc/org/ros/android/NodeMainExecutorService.html#">NodeMainExecutorService</a> as a <a class="reference external" href="http://developer.android.com/reference/android/app/Service.html">service</a>
  196. in the <a class="reference external" href="http://developer.android.com/reference/android/app/Service.html#startForeground(int,android.app.Notification)">foreground</a>,</li>
  197. <li>launch the <a class="reference external" href="javadoc/org/ros/android/MasterChooser.html#">MasterChooser</a> activity to prompt the
  198. user to configure a master URI,</li>
  199. <li>and display an ongoing <a class="reference external" href="http://developer.android.com/reference/android/app/Notification.html">notification</a> informing the user that ROS nodes are
  200. running in the background.</li>
  201. </ul>
  202. <p>On line 22 we call the super constructor with two strings that become the title
  203. and ticker message of an Android <a class="reference external" href="http://developer.android.com/reference/android/app/Notification.html">notification</a>. The user may tap on the
  204. notification to shut down all ROS nodes associated with the application.</p>
  205. <p>Lines 28-30 should look familiar to Android developers. We load the <a class="reference external" href="http://developer.android.com/reference/android/app/Activity.html">activity</a>
  206. layout and get a reference to our
  207. <a class="reference external" href="javadoc/org/ros/android/view/RosTextView.html#">RosTextView</a> (more on that later).</p>
  208. <p>On line 42 we define the abstract method
  209. <a class="reference external" href="javadoc/org/ros/android/RosActivity.html#init(org.ros.node.NodeMainExecutor)">RosActivity.init</a>.
  210. This is where we kick off our <a class="reference external" href="javadoc/org/ros/node/NodeMain.html#">NodeMain</a>s and other
  211. business logic.</p>
  212. <p>And that&#8217;s it. <a class="reference external" href="javadoc/org/ros/android/RosActivity.html#">RosActivity</a> handles the rest of the
  213. application&#8217;s lifecycle management including:</p>
  214. <ul class="simple">
  215. <li>acquiring and releasing <a class="reference external" href="http://developer.android.com/reference/android/os/PowerManager.WakeLock.html">WakeLocks</a> and <a class="reference external" href="http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html">WifiLocks</a>,</li>
  216. <li>binding and unbinding the <a class="reference external" href="javadoc/org/ros/android/NodeMainExecutorService.html#">NodeMainExecutorService</a>,</li>
  217. <li>and shutting down <a class="reference external" href="javadoc/org/ros/node/NodeMain.html#">NodeMain</a>s when the application exits.</li>
  218. </ul>
  219. </div>
  220. <div class="section" id="nodes-and-views">
  221. <h2>Nodes and Views<a class="headerlink" href="#nodes-and-views" title="Permalink to this headline">¶</a></h2>
  222. <p>The android_core stack provides a number of Android <a class="reference external" href="http://developer.android.com/reference/android/view/View.html">Views</a> which implement
  223. <a class="reference external" href="javadoc/org/ros/node/NodeMain.html#">NodeMain</a>. For example, let&#8217;s look at the implementation
  224. of <a class="reference external" href="javadoc/org/ros/android/view/RosTextView.html#">RosTextView</a>. The intent of this view is
  225. to display the textual representation of published messages.</p>
  226. <div class="highlight-java"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
  227. 2
  228. 3
  229. 4
  230. 5
  231. 6
  232. 7
  233. 8
  234. 9
  235. 10
  236. 11
  237. 12
  238. 13
  239. 14
  240. 15
  241. 16
  242. 17
  243. 18
  244. 19
  245. 20
  246. 21
  247. 22
  248. 23
  249. 24
  250. 25
  251. 26
  252. 27
  253. 28
  254. 29
  255. 30
  256. 31
  257. 32
  258. 33
  259. 34
  260. 35
  261. 36
  262. 37
  263. 38
  264. 39
  265. 40
  266. 41
  267. 42
  268. 43
  269. 44
  270. 45
  271. 46
  272. 47
  273. 48
  274. 49
  275. 50
  276. 51
  277. 52
  278. 53
  279. 54
  280. 55
  281. 56
  282. 57
  283. 58
  284. 59
  285. 60
  286. 61
  287. 62
  288. 63
  289. 64
  290. 65
  291. 66
  292. 67
  293. 68
  294. 69
  295. 70
  296. 71
  297. 72
  298. 73
  299. 74
  300. 75
  301. 76</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">package</span> <span class="n">org</span><span class="o">.</span><span class="na">ros</span><span class="o">.</span><span class="na">android</span><span class="o">.</span><span class="na">view</span><span class="o">;</span>
  302. <span class="kn">import</span> <span class="nn">android.content.Context</span><span class="o">;</span>
  303. <span class="kn">import</span> <span class="nn">android.util.AttributeSet</span><span class="o">;</span>
  304. <span class="kn">import</span> <span class="nn">android.widget.TextView</span><span class="o">;</span>
  305. <span class="kn">import</span> <span class="nn">org.ros.android.MessageCallable</span><span class="o">;</span>
  306. <span class="kn">import</span> <span class="nn">org.ros.message.MessageListener</span><span class="o">;</span>
  307. <span class="kn">import</span> <span class="nn">org.ros.namespace.GraphName</span><span class="o">;</span>
  308. <span class="kn">import</span> <span class="nn">org.ros.node.ConnectedNode</span><span class="o">;</span>
  309. <span class="kn">import</span> <span class="nn">org.ros.node.Node</span><span class="o">;</span>
  310. <span class="kn">import</span> <span class="nn">org.ros.node.NodeMain</span><span class="o">;</span>
  311. <span class="kn">import</span> <span class="nn">org.ros.node.topic.Subscriber</span><span class="o">;</span>
  312. <span class="cm">/**</span>
  313. <span class="cm"> * @author [email protected] (Damon Kohler)</span>
  314. <span class="cm"> */</span>
  315. <span class="kd">public</span> <span class="kd">class</span> <span class="nc">RosTextView</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="kd">extends</span> <span class="n">TextView</span> <span class="kd">implements</span> <span class="n">NodeMain</span> <span class="o">{</span>
  316. <span class="kd">private</span> <span class="n">String</span> <span class="n">topicName</span><span class="o">;</span>
  317. <span class="kd">private</span> <span class="n">String</span> <span class="n">messageType</span><span class="o">;</span>
  318. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setTopicName</span><span class="o">(</span><span class="n">String</span> <span class="n">topicName</span><span class="o">)</span> <span class="o">{</span>
  319. <span class="k">this</span><span class="o">.</span><span class="na">topicName</span> <span class="o">=</span> <span class="n">topicName</span><span class="o">;</span>
  320. <span class="o">}</span>
  321. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setMessageType</span><span class="o">(</span><span class="n">String</span> <span class="n">messageType</span><span class="o">)</span> <span class="o">{</span>
  322. <span class="k">this</span><span class="o">.</span><span class="na">messageType</span> <span class="o">=</span> <span class="n">messageType</span><span class="o">;</span>
  323. <span class="o">}</span>
  324. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setMessageToStringCallable</span><span class="o">(</span><span class="n">MessageCallable</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">T</span><span class="o">&gt;</span> <span class="n">callable</span><span class="o">)</span> <span class="o">{</span>
  325. <span class="k">this</span><span class="o">.</span><span class="na">callable</span> <span class="o">=</span> <span class="n">callable</span><span class="o">;</span>
  326. <span class="o">}</span>
  327. <span class="nd">@Override</span>
  328. <span class="kd">public</span> <span class="n">GraphName</span> <span class="nf">getDefaultNodeName</span><span class="o">()</span> <span class="o">{</span>
  329. <span class="k">return</span> <span class="k">new</span> <span class="nf">GraphName</span><span class="o">(</span><span class="s">&quot;android_gingerbread/ros_text_view&quot;</span><span class="o">);</span>
  330. <span class="o">}</span>
  331. <span class="nd">@Override</span>
  332. <span class="hll"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onStart</span><span class="o">(</span><span class="n">ConnectedNode</span> <span class="n">connectedNode</span><span class="o">)</span> <span class="o">{</span>
  333. </span> <span class="n">Subscriber</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="n">subscriber</span> <span class="o">=</span> <span class="n">connectedNode</span><span class="o">.</span><span class="na">newSubscriber</span><span class="o">(</span><span class="n">topicName</span><span class="o">,</span> <span class="n">messageType</span><span class="o">);</span>
  334. <span class="n">subscriber</span><span class="o">.</span><span class="na">addMessageListener</span><span class="o">(</span><span class="k">new</span> <span class="n">MessageListener</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;()</span> <span class="o">{</span>
  335. <span class="nd">@Override</span>
  336. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onNewMessage</span><span class="o">(</span><span class="kd">final</span> <span class="n">T</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
  337. <span class="k">if</span> <span class="o">(</span><span class="n">callable</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
  338. <span class="n">post</span><span class="o">(</span><span class="k">new</span> <span class="n">Runnable</span><span class="o">()</span> <span class="o">{</span>
  339. <span class="nd">@Override</span>
  340. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
  341. <span class="hll"> <span class="n">setText</span><span class="o">(</span><span class="n">callable</span><span class="o">.</span><span class="na">call</span><span class="o">(</span><span class="n">message</span><span class="o">));</span>
  342. </span> <span class="o">}</span>
  343. <span class="o">});</span>
  344. <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
  345. <span class="n">post</span><span class="o">(</span><span class="k">new</span> <span class="n">Runnable</span><span class="o">()</span> <span class="o">{</span>
  346. <span class="nd">@Override</span>
  347. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
  348. <span class="hll"> <span class="n">setText</span><span class="o">(</span><span class="n">message</span><span class="o">.</span><span class="na">toString</span><span class="o">());</span>
  349. </span> <span class="o">}</span>
  350. <span class="o">});</span>
  351. <span class="o">}</span>
  352. <span class="n">postInvalidate</span><span class="o">();</span>
  353. <span class="o">}</span>
  354. <span class="o">});</span>
  355. <span class="o">}</span>
  356. <span class="nd">@Override</span>
  357. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onShutdown</span><span class="o">(</span><span class="n">Node</span> <span class="n">node</span><span class="o">)</span> <span class="o">{</span>
  358. <span class="o">}</span>
  359. <span class="nd">@Override</span>
  360. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onShutdownComplete</span><span class="o">(</span><span class="n">Node</span> <span class="n">node</span><span class="o">)</span> <span class="o">{</span>
  361. <span class="o">}</span>
  362. <span class="nd">@Override</span>
  363. <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onError</span><span class="o">(</span><span class="n">Node</span> <span class="n">node</span><span class="o">,</span> <span class="n">Throwable</span> <span class="n">throwable</span><span class="o">)</span> <span class="o">{</span>
  364. <span class="o">}</span>
  365. <span class="o">}</span>
  366. </pre></div>
  367. </td></tr></table></div>
  368. <p>The view is configured with a topic name, message type, and a
  369. <a class="reference external" href="javadoc/org/ros/android/MessageCallable.html#">MessageCallable</a>. On line 40, in the
  370. <a class="reference external" href="javadoc/org/ros/node/NodeMain.html#onStart(Node)">NodeMain.onStart</a> method, we create a new
  371. <a class="reference external" href="javadoc/org/ros/node/topic/Subscriber.html#">Subscriber</a> for the configured topic and message
  372. type.</p>
  373. <p>When a new message arrives, we either use the configured callable to transform
  374. the incoming message to a string (line 49), or we use the default
  375. <tt class="docutils literal"><span class="pre">toString()</span></tt> method if no callable was configured (line 56). We then set the
  376. text of the view to the string representation of the incoming message.</p>
  377. <p>As with any other <a class="reference external" href="javadoc/org/ros/node/NodeMain.html#">NodeMain</a>, the
  378. <a class="reference external" href="javadoc/org/ros/android/view/RosTextView.html#">RosTextView</a> must be executed by the
  379. <a class="reference external" href="javadoc/org/ros/node/NodeMainExecutor.html#">NodeMainExecutor</a>. In the <a class="reference internal" href="#life-of-a-rosactivity"><em>Using RosActivity</em></a>
  380. example, we execute it in
  381. <a class="reference external" href="javadoc/org/ros/android/RosActivity.html#init(NodeMainExecutor)">RosActivity.init</a> and use the it to
  382. display incoming messages from the
  383. <a class="reference external" href="javadoc/org/ros/rosjava_tutorial_pubsub/Talker.html#">Talker</a> node.</p>
  384. </div>
  385. </div>
  386. </div>
  387. <div class="bottomnav">
  388. <p>
  389. «&#160;&#160;<a href="building.html">Building android_core</a>
  390. &#160;&#160;::&#160;&#160;
  391. <a class="uplink" href="index.html">Contents</a>
  392. </p>
  393. </div>
  394. <div class="footer">
  395. &copy; Copyright 2012, Google, Inc..
  396. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
  397. </div>
  398. </body>
  399. </html>