| Dirk Dougherty | 7b229ef | 2010-03-26 17:32:26 -0700 | [diff] [blame] | 1 | page.title=Getting the Samples |
| Scott Main | 796ce77 | 2011-02-16 10:04:45 -0800 | [diff] [blame] | 2 | parent.title=Sample Code |
| 3 | parent.link=../browser.html?tag=sample |
| Dirk Dougherty | 7b229ef | 2010-03-26 17:32:26 -0700 | [diff] [blame] | 4 | |
| 5 | @jd:body |
| 6 | |
| 7 | <p>Sometimes, the best way to learn how things are done is to look at some |
| 8 | code.</p> |
| 9 | |
| 10 | <p>To help you get started quickly, the Android SDK includes a variety of sample |
| 11 | code and tutorials that illustrate key concepts and techniques of Android |
| 12 | application development. For example, the samples show the structure of the |
| 13 | manifest file and the use of activities, services, resources, |
| 14 | intents, content providers, and permissions. They also show how to add |
| 15 | specialized capabilities to your apps, such as Bluetooth and Contacts |
| 16 | integration, multiple screens support, Live Wallpaper, and more. </p> |
| 17 | |
| 18 | <p>The SDK provides the samples both as source code and as browseable HTML, as |
| 19 | described in the sections below. All of the samples included in the SDK are |
| 20 | licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache |
| 21 | 2.0 license</a>, so feel free to use any of the code in your own applications as |
| 22 | needed!</p> |
| 23 | |
| 24 | <h3 id="downloading">Downloading the Sample Code</h3> |
| 25 | |
| 26 | <p>The SDK sample code is available to you as a set of downloadable SDK |
| 27 | components, each of which contains the samples for a specific Android platform |
| 28 | version. Once you have installed the SDK, you can download one or more samples |
| 29 | component(s) into your SDK environment using the Android SDK and AVD Manager |
| 30 | tool, which is pre-installed in the SDK. </p> |
| 31 | |
| 32 | <p>To download the samples, launch the Android SDK and AVD Manager tool and |
| 33 | select one of the samples components from the <strong>Available |
| 34 | Packages</strong> panel, for example "Samples for SDK API 7". Select |
| 35 | <strong>Install Selected</strong>, verify and accept the download, then select |
| 36 | <strong>Install Accepted</strong> to download the component into your SDK. If |
| 37 | you aren't familiar with the Android SDK and AVD Manager and how to launch or |
| 38 | use it, please read the <a href="{@docRoot}sdk/adding-components.html">Adding |
| 39 | SDK Components</a> document. </p> |
| 40 | |
| 41 | <p>When the download is complete, you can find the samples sources on your |
| 42 | computer in this location:</p> |
| 43 | |
| 44 | <p style="margin-left:2em"> |
| Dirk Dougherty | 0d1b081 | 2010-04-07 17:05:16 -0700 | [diff] [blame] | 45 | <code><em><sdk></em>/samples/android-<<em>level</em>>/</code> |
| Dirk Dougherty | 7b229ef | 2010-03-26 17:32:26 -0700 | [diff] [blame] | 46 | </p> |
| 47 | |
| 48 | <p>You can easily create new Android projects with the downloaded samples, modify them |
| 49 | if you'd like, and then run them on an emulator or device. </p> |
| 50 | |
| 51 | <p>For example, if you are developing in Eclipse with the ADT Plugin, you can |
| 52 | create a project for the "API Demos" sample app by starting a new Android |
| 53 | Project, selecting "Create project from existing source", and then browsing to |
| Dirk Dougherty | 0d1b081 | 2010-04-07 17:05:16 -0700 | [diff] [blame] | 54 | the <code><em><sdk></em>/samples/android-<<em>level</em>>/ApiDemos</code> |
| Dirk Dougherty | 7b229ef | 2010-03-26 17:32:26 -0700 | [diff] [blame] | 55 | directory (the <code>samples</code> directory for the platform version you are |
| 56 | using).</p> |
| 57 | |
| 58 | <p>If you are not working in Eclipse, you can create a project for the API Demos |
| 59 | sample using the <code>android</code> tool, by executing this command:</p> |
| 60 | |
| 61 | <pre class="no-pretty-print"> |
| Dirk Dougherty | 0d1b081 | 2010-04-07 17:05:16 -0700 | [diff] [blame] | 62 | android update project -s -n API Demos -t <em><target_ID></em> -p <<em>path</em>></em>samples/android-<<em>level</em>>/ApiDemos/ |
| Dirk Dougherty | 7b229ef | 2010-03-26 17:32:26 -0700 | [diff] [blame] | 63 | </pre> |
| 64 | |
| 65 | <h3 id="browsing">Browsing the Sample Code</h3> |
| 66 | |
| 67 | <p>For your convenience, the SDK provides browseable source code for the latest |
| 68 | versions of the samples. You can use your browser to navigate through the |
| 69 | structure of each sample and look at the source code in each of its files. </p> |
| 70 | |
| 71 | <p>To browse the samples, go to the <a |
| 72 | href="{@docRoot}resources/samples/index.html">List of Sample Apps</a> first. |
| 73 | From there you can read a short summary of each sample application and what |
| 74 | types of concepts, features, or APIs it includes. Then, use the links provided |
| 75 | to move through the directories and files of each sample. The browseable source |
| 76 | is generated from the same source code that is downloadable through the Android |
| 77 | SDK and AVD Manager, as described above. </p> |
| 78 | |
| 79 | <p>The browseable samples files are available online, at the Android Developers |
| 80 | site only and are not included in the downloadable offline documentation. |
| 81 | Note that, although samples for several platform versions are available for |
| 82 | download, only the samples for the latest platform version are browseable online. |
| 83 | </p> |
| 84 | |
| 85 | |
| 86 | <h3 id="more">More Sample Code</h3> |
| 87 | |
| 88 | <p>If you are looking for more sample code, check out |
| 89 | <a href="http://code.google.com/p/apps-for-android/">apps-for-android</a>, a |
| 90 | collection of open source applications that demonstrate various Android APIs.</p> |
| 91 | |
| 92 | |