getting_started.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. .. _getting-started:
  2. Getting started
  3. ===============
  4. Before diving into ROS enabled Android application development, you should be
  5. familiar with :ref:`rosjava <rosjava-core:getting-started>` and `Android
  6. application development`_ in general. Note that any information regarding
  7. command-line adt and eclipse development is depracating - we have moved early
  8. to a gradle-`android studio`_ environment.
  9. .. _Android application development: http://developer.android.com/training/index.html
  10. .. _android studio: http://wiki.ros.org/android/Android Studio
  11. Creating a new Android application
  12. ----------------------------------
  13. Refer to the `RosWiki`_ for tutorials.
  14. .. _RosWiki: http://wiki.ros.org/android
  15. .. _life-of-a-rosactivity:
  16. Using RosActivity
  17. -----------------
  18. The :javadoc:`org.ros.android.RosActivity` class is the base class for all of
  19. your ROS enabled Android applications. Let's consider the following example
  20. from the android_tutorial_pubsub package. In this example, we create a
  21. :javadoc:`org.ros.node.topic.Publisher` and a
  22. :javadoc:`org.ros.node.topic.Subscriber` that will exchange "Hello, World"
  23. messages.
  24. .. literalinclude:: ../../../../android_tutorial_pubsub/src/org/ros/android/android_tutorial_pubsub/MainActivity.java
  25. :language: java
  26. :linenos:
  27. :lines: 17-
  28. :emphasize-lines: 14,22,28-30,42
  29. On line 14, we extend :javadoc:`org.ros.android.RosActivity`. When our
  30. `activity`_ starts, the :javadoc:`org.ros.android.RosActivity` super class will:
  31. * start the :javadoc:`org.ros.android.NodeMainExecutorService` as a `service`_
  32. in the `foreground`_,
  33. * launch the :javadoc:`org.ros.android.MasterChooser` activity to prompt the
  34. user to configure a master URI,
  35. * and display an ongoing `notification`_ informing the user that ROS nodes are
  36. running in the background.
  37. .. _activity: http://developer.android.com/reference/android/app/Activity.html
  38. .. _service: http://developer.android.com/reference/android/app/Service.html
  39. .. _foreground: http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
  40. .. _notification: http://developer.android.com/reference/android/app/Notification.html
  41. On line 22 we call the super constructor with two strings that become the title
  42. and ticker message of an Android `notification`_. The user may tap on the
  43. notification to shut down all ROS nodes associated with the application.
  44. Lines 28-30 should look familiar to Android developers. We load the `activity`_
  45. layout and get a reference to our
  46. :javadoc:`org.ros.android.view.RosTextView` (more on that later).
  47. On line 42 we define the abstract method
  48. :javadoc:`org.ros.android.RosActivity#init(org.ros.node.NodeMainExecutor)`.
  49. This is where we kick off our :javadoc:`org.ros.node.NodeMain`\s and other
  50. business logic.
  51. And that's it. :javadoc:`org.ros.android.RosActivity` handles the rest of the
  52. application's lifecycle management including:
  53. * acquiring and releasing `WakeLocks`_ and `WifiLocks`_,
  54. * binding and unbinding the :javadoc:`org.ros.android.NodeMainExecutorService`,
  55. * and shutting down :javadoc:`org.ros.node.NodeMain`\s when the application exits.
  56. .. _WakeLocks: http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
  57. .. _WifiLocks: http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html
  58. Nodes and Views
  59. ---------------
  60. The android_core stack provides a number of Android `Views`_ which implement
  61. :javadoc:`org.ros.node.NodeMain`. For example, let's look at the implementation
  62. of :javadoc:`org.ros.android.view.RosTextView`. The intent of this view is
  63. to display the textual representation of published messages.
  64. .. literalinclude:: ../../../../android_gingerbread_mr1/src/org/ros/android/view/RosTextView.java
  65. :language: java
  66. :linenos:
  67. :lines: 17-36,50-
  68. :emphasize-lines: 40,49,56
  69. The view is configured with a topic name, message type, and a
  70. :javadoc:`org.ros.android.MessageCallable`. On line 40, in the
  71. :javadoc:`org.ros.node.NodeMain#onStart(Node)` method, we create a new
  72. :javadoc:`org.ros.node.topic.Subscriber` for the configured topic and message
  73. type.
  74. When a new message arrives, we either use the configured callable to transform
  75. the incoming message to a string (line 49), or we use the default
  76. ``toString()`` method if no callable was configured (line 56). We then set the
  77. text of the view to the string representation of the incoming message.
  78. As with any other :javadoc:`org.ros.node.NodeMain`, the
  79. :javadoc:`org.ros.android.view.RosTextView` must be executed by the
  80. :javadoc:`org.ros.node.NodeMainExecutor`. In the :ref:`life-of-a-rosactivity`
  81. example, we execute it in
  82. :javadoc:`org.ros.android.RosActivity#init(NodeMainExecutor)` and use the it to
  83. display incoming messages from the
  84. :javadoc:`org.ros.rosjava_tutorial_pubsub.Talker` node.
  85. .. _Views: http://developer.android.com/reference/android/view/View.html
  86. .. _TextView: http://developer.android.com/reference/android/widget/TextView.html