getting_started.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.
  7. .. _Android application development: http://developer.android.com/training/index.html
  8. Creating a new Android application
  9. ----------------------------------
  10. .. note:: This is still a work in progress. There are many obvious limitations
  11. and the process will be improved in the near future.
  12. Currently, the easiest way to create a new application is to create your own
  13. package in the android_core stack by copying one of the tutorial packages (e.g.
  14. android_tutorial_pubsub).
  15. .. code-block:: bash
  16. roscd android_core
  17. cp -a android_tutorial_pubsub my_package
  18. After that, modify android_core/settings.gradle to include your new package.
  19. .. code-block:: bash
  20. rosed android_core/settings.gradle
  21. ./gradlew my_package:clean my_package:debug
  22. At this point, you may interact with your Android projects as described in the
  23. `Android documentation`_. Please start there if the following quick start
  24. instructions are insufficient for you.
  25. Use `Apache Ant`_ to install your new Android application:
  26. .. code-block:: bash
  27. roscd my_package
  28. ant installd
  29. You can also use ant to build the application. However, if you add, remove, or
  30. modify a dependency in the build.gradle file, you will need to execute the
  31. `gradle wrapper`_ as described above in order to update the Android
  32. application's external dependencies (located in the ``my_package/libs``
  33. directory).
  34. .. note:: You may also build and run your application from Eclipse. For more
  35. information, see :doc:`building`.
  36. .. _Android documentation: http://developer.android.com/guide/developing/building/building-cmdline.html
  37. .. _Apache Ant: http://ant.apache.org/
  38. .. _gradle wrapper: http://gradle.org/docs/current/userguide/gradle_wrapper.html
  39. .. _life-of-a-rosactivity:
  40. Using RosActivity
  41. -----------------
  42. The :javadoc:`org.ros.android.RosActivity` class is the base class for all of
  43. your ROS enabled Android applications. Let's consider the following example
  44. from the android_tutorial_pubsub package. In this example, we create a
  45. :javadoc:`org.ros.node.topic.Publisher` and a
  46. :javadoc:`org.ros.node.topic.Subscriber` that will exchange "Hello, World"
  47. messages.
  48. .. literalinclude:: ../../../../android_tutorial_pubsub/src/org/ros/android/android_tutorial_pubsub/MainActivity.java
  49. :language: java
  50. :linenos:
  51. :lines: 17-
  52. :emphasize-lines: 14,22,28-30,42
  53. On line 14, we extend :javadoc:`org.ros.android.RosActivity`. When our
  54. `activity`_ starts, the :javadoc:`org.ros.android.RosActivity` super class will:
  55. * start the :javadoc:`org.ros.android.NodeMainExecutorService` as a `service`_
  56. in the `foreground`_,
  57. * launch the :javadoc:`org.ros.android.MasterChooser` activity to prompt the
  58. user to configure a master URI,
  59. * and display an ongoing `notification`_ informing the user that ROS nodes are
  60. running in the background.
  61. .. _activity: http://developer.android.com/reference/android/app/Activity.html
  62. .. _service: http://developer.android.com/reference/android/app/Service.html
  63. .. _foreground: http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
  64. .. _notification: http://developer.android.com/reference/android/app/Notification.html
  65. On line 22 we call the super constructor with two strings that become the title
  66. and ticker message of an Android `notification`_. The user may tap on the
  67. notification to shut down all ROS nodes associated with the application.
  68. Lines 28-30 should look familiar to Android developers. We load the `activity`_
  69. layout and get a reference to our
  70. :javadoc:`org.ros.android.view.RosTextView` (more on that later).
  71. On line 42 we define the abstract method
  72. :javadoc:`org.ros.android.RosActivity#init(org.ros.node.NodeMainExecutor)`.
  73. This is where we kick off our :javadoc:`org.ros.node.NodeMain`\s and other
  74. business logic.
  75. And that's it. :javadoc:`org.ros.android.RosActivity` handles the rest of the
  76. application's lifecycle management including:
  77. * acquiring and releasing `WakeLocks`_ and `WifiLocks`_,
  78. * binding and unbinding the :javadoc:`org.ros.android.NodeMainExecutorService`,
  79. * and shutting down :javadoc:`org.ros.node.NodeMain`\s when the application exits.
  80. .. _WakeLocks: http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
  81. .. _WifiLocks: http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html
  82. Nodes and Views
  83. ---------------
  84. The android_core stack provides a number of Android `Views`_ which implement
  85. :javadoc:`org.ros.node.NodeMain`. For example, let's look at the implementation
  86. of :javadoc:`org.ros.android.view.RosTextView`. The intent of this view is
  87. to display the textual representation of published messages.
  88. .. literalinclude:: ../../../../android_gingerbread/src/org/ros/android/view/RosTextView.java
  89. :language: java
  90. :linenos:
  91. :lines: 17-36,50-
  92. :emphasize-lines: 40,49,56
  93. The view is configured with a topic name, message type, and a
  94. :javadoc:`org.ros.android.MessageCallable`. On line 40, in the
  95. :javadoc:`org.ros.node.NodeMain#onStart(Node)` method, we create a new
  96. :javadoc:`org.ros.node.topic.Subscriber` for the configured topic and message
  97. type.
  98. When a new message arrives, we either use the configured callable to transform
  99. the incoming message to a string (line 49), or we use the default
  100. ``toString()`` method if no callable was configured (line 56). We then set the
  101. text of the view to the string representation of the incoming message.
  102. As with any other :javadoc:`org.ros.node.NodeMain`, the
  103. :javadoc:`org.ros.android.view.RosTextView` must be executed by the
  104. :javadoc:`org.ros.node.NodeMainExecutor`. In the :ref:`life-of-a-rosactivity`
  105. example, we execute it in
  106. :javadoc:`org.ros.android.RosActivity#init(NodeMainExecutor)` and use the it to
  107. display incoming messages from the
  108. :javadoc:`org.ros.rosjava_tutorial_pubsub.Talker` node.
  109. .. _Views: http://developer.android.com/reference/android/view/View.html
  110. .. _TextView: http://developer.android.com/reference/android/widget/TextView.html