1page.title=Common Tasks and How to Do Them in Android 2parent.title=FAQs, Tips, and How-to 3parent.link=index.html 4@jd:body 5 6<ul> 7 <li><a href="#neweclipseandroidproject">Creating an Android Application using 8 the Eclipse plugin</a></li> 9 <li><a href="#newandroidprojectnoeclipse">Creating an Android Application without 10 the Eclipse plugin</a></li> 11 <li><a href="#addexternallibrary">Adding an External Library (.jar) using Eclipse</a></li> 12 <li><a href="#implementcallbacks">Implementing Activity callbacks</a> (Android 13 calls your activity at various key moments in its life cycle. You must know 14 how to handle each of these to draw your screen, initialize class members, 15 and acquire data.)</li> 16 <li><a href="#opennewscreen">Opening a new screen</a></li> 17 <li><a href="#listening">Listening for button clicks </a></li> 18 <li><a href="#configurewindowproperties">Configuring general window properties </a></li> 19 <li><a href="#localhostalias">Referring to localhost from the emulated environment</a></li> 20 <li><a href="#appstate">Storing and retrieving state</a></li> 21 <li><a href="{@docRoot}guide/topics/data/data-storage.html#preferences">Storing and retrieving preferences</a></li> 22 <li><a href="#storingandretrieving">Storing and retrieving larger or more complex 23 persistent data</a> (files and data) </li> 24 <li><a href="#playback">Playing audio, video, still, or other media files</a></li> 25 <li><a href="#broadcastreceivers">Listening for and broadcasting global messages 26 and setting alarms</a></li> 27 <li><a href="#alerts">Displaying alerts </a></li> 28 <li><a href="#progressbar">Displaying a progress bar</a> </li> 29 <li><a href="#addmenuitems">Adding items to the screen menu</a> </li> 30 <li><a href="#webpage">Display a web page</a> </li> 31 <li><a href="#binding">Binding to data</a></li> 32 <li><a href="#handle">Getting a Handle to a Screen Element</a></li> 33 <li><a href="#captureimages">Capture images from the phone camera </a></li> 34 <li><a href="#threading">Handling expensive operations in the UI thread</a></li> 35 <li><a href="#selectingtext">Selecting, highlighting, or styling portions of 36 text</a></li> 37 <li><a href="#querymap">Utilizing attributes in a Map query</a></li> 38 <li><a href="#filelist">List of files for an Android application</a></li> 39 <li><a href="#logging">Print messages to a log file</a></li> 40</ul> 41<p>The ApiDemos sample application includes many, many examples of common 42tasks and UI features. See the code inside 43<code><sdk>samples/ApiDemos</code> and the other sample applications 44under the <code>samples/</code> folder in the SDK.</p> 45 46 47<h2 id="neweclipseandroidproject">Creating an Android Application using the Eclipse Plugin</h2> 48 49<p>Using the Android Eclipse plugin is the fastest and easiest way 50to start creating a new Android application. The plugin automatically generates 51the correct project structure for your application, and keeps the resources 52compiled for you automatically.</p> 53 54<p>It is still a good idea to know what is going on though. Take a look at <a 55href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> 56to understand the basics of how an Android application works.</p> 57 58<p>You should also take a look at the ApiDemos application and the other sample 59applications included in the SDK, in the <code><sdk>/samples/</code> 60folder in the SDK.</p> 61 62<p>Finally, a great way to started with Android development in Eclipse is to 63follow both the <a href="{@docRoot}guide/tutorials/hello-world.html">Hello, 64World</a> and <a 65href="{@docRoot}guide/tutorials/notepad/index.html">Notepad</a> code 66tutorials. In particular, the start of the Hello Android tutorial is an 67excellent introduction to creating a new Android application in Eclipse.</p> 68 69<h2 id="newandroidprojectnoeclipse">Creating an Android Application without the Eclipse Plugin</h2> 70 71<p>This topic describes the manual steps in creating an Android application. 72Before reading this, you should read <a 73href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a> 74to understand the basics of how an Android application works. You might also 75want to look at the sample code included with the Android SDK, in the 76<code><sdk>/samples/</code> directory. </p> 77 78<p>Here is a list of the basic steps in building an application.</p> 79<ol> 80 <li><strong>Create your required resource files</strong> This includes 81 the AndroidManifest.xml global description file, string files that your application 82 needs, and layout files describing your user interface. A full list of optional 83 and required files and syntax details for each is given in <a href="#filelist">File 84 List for an Android Application</a>. </li> 85 <li><strong>Design your user interface</strong> See <a 86 href="{@docRoot}guide/topics/ui/index.html">User Interface</a> for 87 details on elements of the Android screen. </li> 88 <li><strong>Implement your Activity </strong>(this page)<strong> </strong> You 89 will create one class/file for each screen in your application. Screens will 90 inherit from an {@link android.app android.app} class, typically {@link android.app.Activity 91 android.app.Activity} for basic screens, {@link android.app.ListActivity 92 android.app.ListActivity} for list screens, or {@link android.app.Dialog 93 android.app.Dialog} for dialog boxes. You will implement the required callbacks 94 that let you draw your screen, query data, and commit changes, and also perform 95 any required tasks such as opening additional screens or reading data from 96 the device. Common tasks, such as opening a new screen or reading data from 97 the device, are described below. 98 The list of files you'll need for your application are described in <a href="#filelist">List 99 of Files for an Android Application</a>. </li> 100 <li><strong><a href="{@docRoot}guide/developing/other-ide.html#buildingwithant">Build and install your 101 package</a>.</strong> The Android SDK has some nice tools for generating 102 projects and debugging code. </li> 103</ol> 104 105<h2 id="addexternallibrary">Adding an External Library (.jar) using Eclipse</h2> 106<p> 107You can use a third party JAR in your application by adding it to your Eclipse project as follows: 108</p> 109<ol> 110<li> 111In the <strong>Package Explorer</strong> panel, right-click on your project and select <strong>Properties</strong>. 112<li> 113Select <strong>Java Build Path</strong>, then the tab <strong>Libraries</strong>. 114<li> 115Press the <strong>Add External JARs...</strong> button and select the JAR file. 116</ol> 117<p> 118Alternatively, if you want to include third party JARs with your package, create a new directory for them within your project and select <strong>Add Library...</strong> instead.</p> 119<p> 120It is not necessary to put external JARs in the assets folder. 121</p> 122 123<a name="implementcallbacks" id="implementcallbacks"></a> 124<h2>Implementing Activity Callbacks</h2> 125<p>Android calls a number of callbacks to let you draw your screen, store data before 126 pausing, and refresh data after closing. You must implement at least some of 127 these methods. See <a href="{@docRoot}guide/topics/fundamentals.html#lcycles">Lifecycles</a> 128 discussion in Application Fundamentals to learn when and in what order these methods 129 are called. Here are some of the standard types of screen classes that Android provides:</p> 130<ul> 131 <li>{@link android.app.Activity android.app.Activity} - This is a standard screen, 132 with no specialization.</li> 133 <li>{@link android.app.ListActivity android.app.ListActivity} - This is a screen 134 that is used to display a list of something. It hosts a ListView object, 135 and exposes methods to let you identify the selected item, receive callbacks 136 when the selected item changes, and perform other list-related actions. </li> 137 <li>{@link android.app.Dialog android.app.Dialog} - This is a small, popup dialog-style 138 window that isn't intended to remain in the history stack. (It is not resizeable 139 or moveable by the user.)</li> 140</ul> 141 142<a name="opennewscreen" id="opennewscreen"></a><h2>Opening a New Screen</h2> 143<p>Your Activity will often need to open another Activity screen as it progresses. 144 This new screen can be part of the same application or part of another application, 145 the new screen can be floating or full screen, it can return a result, and you 146 can decide whether to close this screen and remove it from the history stack 147 when you are done with it, or to keep the screen open in history. These next 148 sections describe all these options. </p> 149<h3>Floating or full?<a name="floatingorfull" id="floatingorfull"></a></h3> 150<p>When you open a new screen you can decide whether to make it transparent or floating, 151 or full-screen. The choice of new screen affects the event sequence of events 152 in the old screen (if the new screen obscures the old screen, a different 153 series of events is called in the old screen). See <a 154 href="{@docRoot}guide/topics/fundamentals.html#lcycles">Lifecycles</a> discussion 155 in Application Fundamentals for details. </p> 156<p>Transparent or floating windows are implemented in three 157 standard ways: </p> 158<ul> 159 <li>Create an {@link android.app.Dialog app.Dialog} class </li> 160 <li>Create an {@link android.app.AlertDialog app.AlertDialog} class </li> 161 <li>Set the {@link android.R.style#Theme_Dialog} <em>theme</em> attribute to <code>@android:style/Theme.Dialog</code> 162 in your AndroidManifest.xml file. For example: 163</ul> 164<blockquote> 165 <pre><activity class="AddRssItem" android:label="Add an item" android:theme="@android:style/Theme.Dialog"/> 166</pre> 167</blockquote> 168<p>Calling startActivity() or startActivityForResult() will open a new screen in whatever 169 way it defines itself (if it uses a floating theme it will be floating, 170 otherwise it will be full screen). </p> 171<h3>Opening a Screen </h3> 172<p>When you want to open a new screen, you can either explicitly specify the activity 173 class to open, or you can let the operating system decide which screen to open, 174 based upon the data and various parameters you pass in. A screen is opened by 175 calling {@link android.app.Activity#startActivity(android.content.Intent) startActivity} 176 and passing in an {@link android.content.Intent Intent} object, which specifies 177 the criteria for the handling screen. To specify a specific screen, call Intent.setClass 178 or setClassName with the exact activity class to open. Otherwise, set a variety 179 of values and data, and let Android decide which screen is appropriate to open. 180 Android will find one or zero Activities that match the specified requirements; 181 it will never open multiple activities for a single request. More information 182 on Intents and how Android resolves them to a specific class is given in the 183 {@link android.content.Intent Intent} topic. </p> 184<a name="intentexamples" id="intentexamples"></a><h3>Some Intent examples </h3> 185<p>The following snippet loads the com.android.samples.Animation1 class, and 186 passes it some arbitrary data.:</p> 187<pre>Intent myIntent = new Intent(); 188myIntent.setClassName("com.android.samples", "com.android.samples.Animation1"); 189myIntent.putExtra("com.android.samples.SpecialValue", "Hello, Joe!"); // key/value pair, where key needs current package prefix. 190startActivity(myIntent); </pre> 191<p>The next snippet requests that a Web page be opened by specifying the VIEW action, 192 and a URI data string starting with "http://" schema:</p> 193<pre>Intent myIntent = new Intent(Intent.VIEW_ACTION, Uri.parse("http://www.google.com"));</pre> 194<p>Here is the intent filter from the AndroidManifest.xml file for com.android.browser:</p> 195<pre><intent-filter> 196 <action android:name="android.intent.action.VIEW" /> 197 <category android:name="android.intent.category.DEFAULT" /> 198 <scheme android:name="http" /> 199 <scheme android:name="https" /> 200 <scheme android:name="file" /> 201</intent-filter> </pre> 202<p>Android defines a number of standard values, for instance the action constants 203 defined by {@link android.content.Intent}. You can define custom values, but 204 both the caller and handler must use them. See the <intent-filter> 205 tag description in <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">The AndroidManifest.xml 206 File</a> for more information on the manifest syntax for the handling 207 application. </p> 208<a name="returningaresult" id="returningaresult"></a><h3>Returning a Result from a Screen</h3> 209<p>A window can return a result after it closes. This result will be passed back 210 into the calling Activity's {@link android.app.Activity#onActivityResult(int,int,android.content.Intent) 211 onActivityResult()} method, which can supply an Intent containing arbitrary data, along with 212 the request code passed to startActivityForResult(). Note that you must call the {@link 213 android.app.Activity#startActivityForResult(android.content.Intent,int) startActivityForResult()} 214 method that accepts a request code parameter to get this callback. The following 215 code demonstrates opening a new screen and retrieving a result. </p> 216<pre>// Open the new screen. 217public void onClick(View v){ 218 // Start the activity whose result we want to retrieve. The 219 // result will come back with request code GET_CODE. 220 Intent intent = new Intent(this, com.example.app.ChooseYourBoxer.class); 221 startActivityForResult(intent, CHOOSE_FIGHTER); 222} 223 224// Listen for results. 225protected void onActivityResult(int requestCode, int resultCode, Intent data){ 226 // See which child activity is calling us back. 227 switch (resultCode) { 228 case CHOOSE_FIGHTER: 229 // This is the standard resultCode that is sent back if the 230 // activity crashed or didn't doesn't supply an explicit result. 231 if (resultCode == RESULT_CANCELED){ 232 myMessageboxFunction("Fight cancelled"); 233 } 234 else { 235 myFightFunction(data); 236 } 237 default: 238 break; 239 } 240} 241 242// Class SentResult 243// Temporary screen to let the user choose something. 244 private OnClickListener mLincolnListener = new OnClickListener(){ 245 public void onClick(View v) { 246 Bundle stats = new Bundle(); 247 stats.putString("height","6\'4\""); 248 stats.putString("weight", "190 lbs"); 249 stats.putString("reach", "74\""); 250 setResult(RESULT_OK, "Lincoln", stats); 251 finish(); 252 } 253 }; 254 255 private OnClickListener mWashingtonListener = new OnClickListener() { 256 public void onClick(View v){ 257 Bundle stats = new Bundle(); 258 stats.putString("height","6\'2\""); 259 stats.putString("weight", "190 lbs"); 260 stats.putString("reach", "73\""); 261 setResult(RESULT_OK, "Washington", Bundle); 262 finish(); 263 } 264 }; 265 </pre> 266<h3>Lifetime of the new screen </h3> 267<p>An activity can remove itself from the history stack by calling {@link android.app.Activity#finish() 268 Activity.finish()} on itself, or the activity that opened the screen can call 269 {@link android.app.Activity#finishActivity(int) Activity.finishActivity()} 270 on any screens that it opens to close them. </p> 271<a name="listening" id="listening"></a><h2>Listening for Button Clicks</h2> 272<p>Button click and other UI event capturing are covered in <a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a> on the UI Design page.</p> 273<a name="configurewindowproperties" id="configurewindowproperties"></a><h2>Configuring General Window Properties</h2> 274<p>You can set a number of general window properties, such as whether to display 275 a title, whether the window is floating, and whether it displays an icon, by 276 calling methods on the {@link android.view.Window Window} member 277 of the underlying View object for the window. Examples include calling {@link 278 android.app.Activity#getWindow() getWindow().requestFeature()} (or the convenience 279 method {@link android.app.Activity#requestWindowFeature(int) requestWindowFeature(<em>some_feature</em>)}) 280 to hide the title. Here is an example of hiding the title bar:</p> 281<pre>//Hide the title bar 282requestWindowFeature(Window.FEATURE_NO_TITLE); 283</pre> 284<p>A better way to achieve the same end is to specify a theme in your Android 285Manifest file:</p> 286<pre><application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar"> 287</pre> 288<p>This is preferable because it tells the system not to show a title bar while 289your application is starting up. With the explicit method call, your application 290will have a title bar visible to the user until <code>onCreate</code> runs.</p> 291<p>(Note that this can be applied to either the <code><application></code> 292tag or to individual <code><activity></code> tags.)</p> 293<a name="localhostalias" id="localhostalias"></a><h2>Referring to localhost from the emulated environment</h2> 294<p> 295If you need to refer to your host computer's <em>localhost</em>, such as when you 296want the emulator client to contact a server running on the same host, use the alias 297<code>10.0.2.2</code> to refer to the host computer's loopback interface. 298From the emulator's perspective, localhost (<code>127.0.0.1</code>) refers to its own 299loopback interface. 300</p> 301<a name="appstate" id="appstate"></a><h2>Storing and Retrieving State</h2> 302<p>If your application is dumped from memory because of space concerns, it will lose 303 all user interface state information such as checkbox state and text box values 304 as well as class member values. Android calls {@link android.app.Activity#onSaveInstanceState(android.os.Bundle) 305 Activity.onSaveInstanceState} before it pauses the application. This method hands in a {@link 306 android.os.Bundle Bundle} that can be used to store name/value pairs that will 307 persist and be handed back to the application even if it is dropped from memory. 308 Android will pass this Bundle back to you when it calls {@link android.app.Activity#onCreate(android.os.Bundle) 309 onCreate()}. This Bundle only exists while the application is still in the history 310 stack (whether or not it has been removed from memory) and will be lost when 311 the application is finalized. See the topics for {@link android.app.Activity#onSaveInstanceState} and 312 {@link android.app.Activity#onCreate} for 313 examples of storing and retrieving state.</p> 314<p>Read more about the lifecycle of an application in <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a>.</p> 315<h3>Storing and Retrieving Larger or More Complex Persistent Data<a name="storingandretrieving" id="storingandretrieving"></a></h3> 316<p>Your application can store files or complex collection objects, and reserve them 317 for private use by itself or other activities in the application, or it can expose 318 its data to all other applications on the device. See <a href="{@docRoot}guide/topics/data/data-storage.html">Storing, 319 Retrieving, and Exposing Data</a> to learn how to store and retrieve private data, 320 how to store and retrieve common data from the device, and how to expose your 321 private data to other applications.</p> 322<a name="playback" id="playback"></a><h2>Playing Media Files</h2> 323<p>Please see the document <a href="{@docRoot}guide/topics/media/index.html">Audio and Video</a> for more details.</p> 324<a name="broadcastreceivers" id="broadcastreceivers"></a><h2>Listening For and Broadcasting Global Messages, and Setting Alarms</h2> 325<p>You can create a listening class that can be notified or even instantiated whenever 326 a specific type of system message is sent. 327</p> 328<p>The listening classes, called broadcast receivers, extend {@link android.content.BroadcastReceiver 329 BroadcastReceiver}. If you want Android to instantiate the object whenever an appropriate 330 intent notification is sent, define the receiver with a <code><receiver></code> element 331 in the AndroidManifext.xml file. If the caller is expected to instantiate the 332 object in preparation to receive a message, this is not required. The receiver 333 will get a call to their {@link android.content.BroadcastReceiver#onReceive(android.content.Context,android.content.Intent) 334 BroadcastReceiver.onReceive()} method. A receiver can define an <code><intent-filter></code> tag 335 that describes the types of messages it will receive. Just as Android's IntentResolver 336 will look for appropriate Activity matches for a startActivity() call, it will 337 look for any matching Receivers (but it will send the message to all matching 338 receivers, not to the "best" match). </p> 339<p>To send a notification, the caller creates an {@link android.content.Intent Intent} 340 object and calls {@link android.app.Activity#sendBroadcast(android.content.Intent) 341 Context.sendBroadcast()} with that Intent. Multiple recipients can receive 342 the same message. You can broadcast an Intent message to an intent receiver in 343 any application, not only your own. If the receiving class is not registered 344 using <code><receiver></code> in its manifest, you can dynamically instantiate 345 and register a receiver by calling {@link android.content.Context#registerReceiver(android.content.BroadcastReceiver,android.content.IntentFilter) 346 Context.registerReceiver()}. </p> 347<p>Receivers can include intent filters to specify what kinds of intents they are 348 listening for. Alternatively, if you expect a single known caller to contact 349 a single known receiver, the receiver does not specify an intent filter, and 350 the caller specifies the receiver's class name in the Intent by calling {@link 351 android.content.Intent#setClassName(java.lang.String, java.lang.String) Intent.setClassName()} 352 with the recipient's class name. The recipient receives a {@link android.content.Context 353 Context} object that refers to its own package, not to the package of the sender.</p> 354<p><em><strong>Note:</strong></em> If a receiver or broadcaster 355 enforces permissions, your application might need to request permission 356 to send or receive messages from that object. You can request permission by using 357 the <uses-permission> tag in the manifest. </p> 358<p>Here is a code snippet of a sender and receiver. This example does not demonstrate 359 registering receivers dynamically. For a full code example, see the AlarmService 360 class in the ApiDemos project.</p> 361<h3>Sending the message</h3> 362<pre>// We are sending this to a specific recipient, so we will 363// only specify the recipient class name. 364Intent intent = new Intent(this, AlarmReceiver.class); 365intent.putExtra("message","Wake up."); 366sendBroadcast(intent); 367</pre> 368<h3>Receiving the message</h3> 369<p><strong>Receiver AndroidManifest.xml </strong>(because there is no intent filter 370 child, this class will only receive a broadcast when the receiver class is specified 371 by name, as is done in this example):</p> 372<pre> 373<receiver class=".AlarmReceiver" /></pre> 374<p><strong>Receiver Java code: </strong></p> 375<pre> 376public class AlarmReceiver extends BroadcastReceiver{ 377 // Display an alert that we've received a message. 378 @Override 379 public void onReceive(Context context, Intent intent){ 380 // Send a text notification to the screen. 381 NotificationManager nm = (NotificationManager) 382 context.getSystemService(Context.NOTIFICATION_SERVICE); 383 nm.notifyWithText(R.id.alarm, 384 "Alarm!!!", 385 NotificationManager.LENGTH_SHORT, 386 null); 387 } 388} </pre> 389<h3>Other system messages</h3> 390<p>You can listen for other system messages sent by Android as well, such as USB 391 connection/removal messages, SMS arrival messages, and timezone changes. See 392 {@link android.content.Intent} for a list of broadcast messages to listen for. 393 Messages are marked "Broadcast Action" in the documentation. </p> 394<h3>Listening for phone events<a name="phoneevents" id="phoneevents"></a></h3> 395<p>The {@link android.telephony android.telephony} package overview page describes how to 396 register to listen for phone events. </p> 397<a name="alarms" id="alarms"></a><h3>Setting Alarms </h3> 398<p>Android provides an {@link android.app.AlarmManager AlarmManager} service that 399 will let you specify an Intent to send at a designated time. This intent is typically 400 used to start an application at a preset time. (Note: If you want to send 401 a notification to a sleeping or running application, use {@link android.os.Handler 402 Handler} instead.)</p> 403<a name="alerts" id="alerts"></a><h2>Displaying Alerts</h2> 404<p>There are two major kinds of alerts that you may display to the user: 405(1) Normal alerts are displayed in response to a user action, such as 406trying to perform an action that is not allowed. (2) Out-of-band alerts, 407called notifications, are 408displayed as a result of something happening in the background, such as the 409user receiving new e-mail.</p> 410 411<a name="dialogsandalerts" id="dialogsandalerts"></a><h3>Normal Alerts</h3> 412 413<p>Android provides a number of ways for you to show popup notifications to your 414 user as they interact with your application. </p> 415<table width="100%" border="1"> 416 <tr> 417 <th scope="col">Class</th> 418 <th scope="col">Description</th> 419 </tr> 420 <tr> 421 <td>{@link android.app.Dialog app.Dialog}</td> 422 <td>A generic floating dialog box with a layout that you design. </td> 423 </tr> 424 <tr> 425 <td><p>{@link android.app.AlertDialog app.AlertDialog}</p></td> 426 <td>A popup alert dialog with two buttons (typically OK and Cancel) that 427 take callback handlers. See the section after this table for more details. </td> 428 </tr> 429 <tr> 430 <td>{@link android.app.ProgressDialog ProgressDialog} </td> 431 <td>A dialog box used to indicate progress of an operation with a known progress 432 value or an indeterminate length (setProgress(bool)). See <strong>Views</strong> > <strong>Progress Bar</strong> in 433 ApiDemos for examples. </td> 434 </tr> 435 <tr> 436 <td>Activity</td> 437 <td>By setting the theme of an activity to 438 {@link android.R.style#Theme_Dialog 439 android:theme="@android:style/Theme.Dialog"}, 440 your activity will take on 441 the appearance of a normal dialog, floating on top of whatever was 442 underneath it. You usually set the theme through the 443 {@link android.R.attr#theme android:theme} attribute in your AndroidManifest.xml. 444 The advantage of this 445 over Dialog and AlertDialog is that Application has a much better managed 446 life cycle than dialogs: if a dialog goes to the background and is killed, 447 you cannot recapture state, whereas Application exposes a {@link android.os.Bundle 448 Bundle} of saved values in <code>onCreate()</code> to help you maintain state.</td> 449 </tr> 450</table> 451<h3>AlertDialog</h3> 452<p>This is a basic warning dialog box that lets you configure a message, button text, 453 and callback. You can create one by calling using the {@link 454 android.app.AlertDialog.Builder} class, as shown here. </p> 455<pre>private Handler mHandler = new Handler() { 456 public void handleMessage(Message msg) { 457 switch (msg.what) { 458 case ACCEPT_CALL: 459 answer(msg.obj); 460 break; 461 462 case BOUNCE_TO_VOICEMAIL: 463 voicemail(msg.obj); 464 break; 465 466 } 467 } 468}; 469 470 471private void IncomingMotherInlawCall(Connection c) { 472 String Text; 473 474 // "Answer" callback. 475 Message acceptMsg = Message.obtain(); 476 acceptMsg.target = mHandler; 477 acceptMsg.what = ACCEPT_CALL; 478 acceptMsg.obj = c.getCall(); 479 480 // "Cancel" callback. 481 final Message rejectMsg = Message.obtain(); 482 rejectMsg.target = mHandler; 483 rejectMsg.what = BOUNCE_TO_VOICEMAIL; 484 rejectMsg.obj = c.getCall(); 485 486 new AlertDialog.Builder(this) 487 .setMessage("Phyllis is calling") 488 .setPositiveButton("Answer", acceptMsg) 489 .setOnCanceListener(new OnCancelListener() { 490 public void onCancel(DialogInterface dialog) { 491 rejectMsg.sendToTarget(); 492 }}); 493 .show(); 494} </pre> 495 496<h3>Notifications</h3> 497 498<p>Out-of-band alerts should always be displayed using the 499{@link android.app.NotificationManager}, which allows you to tell the user 500about something they may be interested in without disrupting what they are 501currently doing. A notification can be anything from a brief pop-up box 502informing the user of the new information, through displaying a persistent 503icon in the status bar, to vibrating, playing sounds, or flashing lights to 504get the user's attention. In all cases, the user must explicitly shift their 505focus to the notification before they can interact with it.</p> 506 507<p>The following code demonstrates using NotificationManager to display a basic text 508 popup when a new SMS message arrives in a listening service, and provides the 509 current message count. You can see several more examples in the ApiDemos application, 510 under app/ (named <em>notification</em>*.java).</p> 511<pre>static void setNewMessageIndicator(Context context, int messageCount){ 512 // Get the static global NotificationManager object. 513 NotificationManager nm = NotificationManager.getDefault();</p> 514 515 // If we're being called because a new message has been received, 516 // then display an icon and a count. Otherwise, delete the persistent 517 // message. 518 if (messageCount > 0) { 519 nm.notifyWithText(myApp.NOTIFICATION_GUID, // ID for this notification. 520 messageCount + " new message" + messageCount > 1 ? "s":"", // Text to display. 521 NotificationManager.LENGTH_SHORT); // Show it for a short time only. 522 } 523}</pre> 524<p>To display a notification in the status bar and have it launch an intent when 525 the user selects it (such as the new text message notification does), call {@link 526 android.app.NotificationManager#notify(int, android.app.Notification) NotificationManager.notify()}, 527 and pass in vibration patterns, status bar icons, or Intents to associate with 528 the notification. </p> 529<a name="progressbar" id="progressbar"></a><h2>Displaying a Progress Bar</h2> 530<p>An activity can display a progress bar to notify the user that something is happening. 531 To display a progress bar in a screen, call {@link android.app.Activity#requestWindowFeature(int) 532 Activity.requestWindowFeature(Window.FEATURE_PROGRESS)}. To set the value 533 of the progress bar, call {@link android.view.Window#setFeatureInt(int,int) 534 Activity.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, <em>level</em>)}. 535 Progress bar values are from 0 to 9,999, or set the value to 10,000 to make the 536 progress bar invisible. </p> 537<p>You can also use the {@link android.app.ProgressDialog ProgressDialog} class, 538 which enables a dialog box with an embedded progress bar to send a "I'm working 539 on it" notification to the user. </p> 540<a name="addmenuitems" id="addmenuitems"></a><h2>Adding Items to the Screen Menu</h2> 541<p>See <a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>.</p> 542 543<a name="webpage" id="webpage"></a><h2>Display a Web Page</h2> 544<p>Use the {@link android.webkit.WebView webkit.WebView} object. </p> 545<a name="binding" id="binding"></a><h2>Binding to Data</h2> 546<p>You can bind a ListView to a set of underlying data by using a shim class called 547 {@link android.widget.ListAdapter ListAdapter} (or a subclass). ListAdapter subclasses 548 bind to a variety of data sources, and expose a common set of methods such as 549 getItem() and getView(), and uses them to pick View items to display in its list. 550 You can extend ListAdapter and override getView() to create your own custom list 551 items. There are essentially only two steps you need to perform to bind to data: </p> 552<ol> 553 <li>Create a ListAdapter object and specify its data source</li> 554 <li>Give the ListAdapter to your ListView object.</li> 555</ol> 556<p>That's it!</p> 557<p>Here's an example of binding a ListActivity screen to the results from a cursor 558 query. (Note that the setListAdapter() method shown is a convenience method that 559 gets the page's ListView object and calls setAdapter() on it.)</p> 560<pre>// Run a query and get a Cursor pointing to the results. 561Cursor c = People.query(this.getContentResolver(), null); 562startManagingCursor(c); 563 564// Create the ListAdapter. A SimpleCursorAdapter lets you specify two interesting things: 565// an XML template for your list item, and 566// The column to map to a specific item, by ID, in your template. 567ListAdapter adapter = new SimpleCursorAdapter(this, 568 android.R.layout.simple_list_item_1, // Use a template that displays a text view 569 c, // Give the cursor to the list adapter 570 new String[] {People.NAME} , // Map the NAME column in the people database to... 571 new String[] {"text1"}); // The "text1" view defined in the XML template 572setListAdapter(adapter);</pre> 573<p>See view/List4 in the ApiDemos project for an example of extending ListAdapter 574 for a new data type. </p> 575 576<a name="handle"></a> 577 578<h2>Getting a Handle to a Screen Element</h2> 579<p>You can get a handle to a screen element by calling {@link 580android.app.Activity#findViewById(int) Activity.findViewById}. You can then use 581the handle to set or retrieve any values exposed by the object. </p> 582<a name="captureimages" id="captureimages"></a><h2>Capture Images from the Phone Camera</h2> 583<p>You can hook into the device's camera onto your own Canvas object by using the 584 {@link android.hardware.Camera Camera} class. See that class's documentation, 585 and the ApiDemos project's Camera Preview application (Graphics/Camera Preview) 586 for example code. </p> 587 588 589<a name="threading" id="threading"></a><h2>Handling Expensive Operations in the UI Thread</h2> 590<p>Avoid performing long-running operations (such as network I/O) directly in the UI thread — 591the main thread of an application where the UI is run — or your application may be blocked 592and become unresponsive. Here is a brief summary of the recommended approach for handling expensive operations:</p> 593<ol> 594<li>Create a Handler object in your UI thread</li> 595<li>Spawn off worker threads to perform any required expensive operations</li> 596<li>Post results from a worker thread back to the UI thread's handler either through a Runnable or a {@link android.os.Message}</li> 597<li>Update the views on the UI thread as needed</li> 598</ol> 599 600<p>The following outline illustrates a typical implementation:</p> 601 602<pre> 603public class MyActivity extends Activity { 604 605 [ . . . ] 606 // Need handler for callbacks to the UI thread 607 final Handler mHandler = new Handler(); 608 609 // Create runnable for posting 610 final Runnable mUpdateResults = new Runnable() { 611 public void run() { 612 updateResultsInUi(); 613 } 614 }; 615 616 @Override 617 protected void onCreate(Bundle savedInstanceState) { 618 super.onCreate(savedInstanceState); 619 620 [ . . . ] 621 } 622 623 protected void startLongRunningOperation() { 624 625 // Fire off a thread to do some work that we shouldn't do directly in the UI thread 626 Thread t = new Thread() { 627 public void run() { 628 mResults = doSomethingExpensive(); 629 mHandler.post(mUpdateResults); 630 } 631 }; 632 t.start(); 633 } 634 635 private void updateResultsInUi() { 636 637 // Back in the UI thread -- update our UI elements based on the data in mResults 638 [ . . . ] 639 } 640} 641</pre> 642 643<p>For further discussions on this topic, see 644<a href="{@docRoot}guide/practices/design/responsiveness.html">Designing for Responsiveness</a> 645and the {@link android.os.Handler} documentation.</p> 646 647<a name="selectingtext" id="selectingtext"></a><h2>Selecting, Highlighting, or Styling Portions of Text</h2> 648<p>You can highlight or style the formatting of strings or substrings of text in 649 a TextView object. There are two ways to do this:</p> 650<ul> 651 <li>If you use a <a href="{@docRoot}guide/topics/resources/available-resources.html#stringresources">string resource</a>, 652 you can add some simple styling, such as bold or italic using HTML notation. 653 The currently supported tags are: <code>B</code> (bold), 654 <code>I</code> (italic), <code>U</code> (underline), 655 <code>TT</code> (monospace), <code>BIG</code>, <code>SMALL</code>, 656 <code>SUP</code> (superscript), <code>SUB</code> (subscript), 657 and <code>STRIKE</code> (strikethrough). 658 So, for example, in res/values/strings.xml you could declare this:<br /> 659 <code><resource><br /> 660 <string>id="@+id/styled_welcome_message">We 661 are <b><i>so</i></b> glad to see you.</string><br /> 662 </resources></code></li> 663 <li>To style text on the fly, or to add highlighting or more complex styling, 664 you must use the Spannable object as described next. </li> 665</ul> 666<p>To style text on the fly, you must make sure the TextView is using {@link android.text.Spannable} 667 storage for the text (this will always be true if the TextView is an EditText), 668 retrieve its text with {@link android.widget.TextView#getText}, and call {@link 669 android.text.Spannable#setSpan}, passing in a new style class from the {@link 670 android.text.style} package and the selection range. </p> 671<p>The following code snippet demonstrates creating a string with a highlighted section, 672 italic section, and bold section, and adding it to an EditText object. </p> 673<pre>// Get our EditText object. 674EditText vw = (EditText)findViewById(R.id.text); 675 676// Set the EditText's text. 677vw.setText("Italic, highlighted, bold."); 678 679// If this were just a TextView, we could do: 680// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE); 681// to force it to use Spannable storage so styles can be attached. 682// Or we could specify that in the XML. 683 684// Get the EditText's internal text storage 685Spannable str = vw.getText(); 686 687// Create our span sections, and assign a format to each. 688str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 689str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 690str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 691</pre> 692 693<a name="querymap" id="querymap"></a><h2>Utilizing attributes in a Map query</h2> 694<p> 695When using a search intent to ask the Maps activity to search for something, the Maps activity responds to the following attributes in the optional context bundle: 696</p> 697<pre> 698 float "centerLatitude" default 0.0f 699 float "centerLongitude" default 0.0f 700 float "latitudeSpan" default 0.0f 701 float "longitudeSpan" default 0.0f 702 int "zoomLevel" default 10 703</pre> 704<p> 705This context information is used to center the search result in a particular area, and is equivalent to adjusting the Map activity to the described location and zoom level before issuing the query. 706</p> 707<p> 708If the latitudeSpan, longitudeSpan, and zoomLevel attributes are not consistent, then it is undefined which one takes precedence. 709</p> 710 711<a name="filelist" id="filelist"></a><h2>List of Files for an Android Application</h2> 712<p>The following list describes the structure and files of an Android application. 713 Many of these files can be built for you (or stubbed out) by the android tool 714 shipped in the tools/ menu of the SDK. </p> 715<table width="100%" border="0"> 716 <tr> 717 <td width="28%" valign="top">MyApp/<br /></td> 718 <td width="72%" valign="top"> </td> 719 </tr> 720 <tr> 721 <td valign="top"> AndroidManifest.xml</td> 722 <td valign="top">(<em>required</em>) Advertises the screens that this application provides, 723 where they can be launched (from the main program menu or elsewhere), 724 any content providers it implements and what kind of data they handle, 725 where the implementation classes are, and other application-wide 726 information. Syntax details for this file are described in <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">The AndroidManifest.xml File</a>.</td> 727 </tr> 728 <tr> 729 <td valign="top"> src/<br /> 730 /<em>myPackagePath</em>/.../<em>MyClass</em>.java</td> 731 <td valign="top">(<em>required</em>) This folder holds all the source code files for your 732 application, inside the appropriate package subfolders. </td> 733 </tr> 734 <tr> 735 <td valign="top"> res/</td> 736 <td valign="top">(<em>required</em>) This folder holds all the <em>resources</em> for 737 your application. Resources are external data files or description files 738 that are compiled into your code at build time. Files in different folders 739 are compiled differently, so you must put the proper resource into the 740 proper folder. (See <a href="{@docRoot}guide/topics/resources/resources-i18n.html">Resources</a> for details.)</td> 741 </tr> 742 <tr> 743 <td valign="top"> anim/<br /> 744 <em>animation1</em>.xml<br /> 745 <em>...</em></td> 746 <td valign="top">(<em>optional</em>) Holds any animation XML description files that the 747 application uses. The format of these files is described in <a href="{@docRoot}guide/topics/resources/resources-i18n.html">Resources</a>. </td> 748 </tr> 749 <tr> 750 <td valign="top"> drawable/<br /> 751 <em>some_picture</em>.png<br /> 752 <em>some_stretchable</em>.9.png<br /> 753 <em>some_background</em>.xml<br /> 754 ...</td> 755 <td valign="top">(<em>optional</em>) Zero or more files that will be compiled to {@link 756 android.graphics.drawable android.graphics.drawable} resources. Files 757 can be image files (png, gif, or other) or XML files describing other 758 graphics such as bitmaps, stretchable bitmaps, or gradients. Supported 759 bitmap file formats are PNG (preferred), JPG, and GIF (discouraged), 760 as well as the custom 9-patch stretchable bitmap format. These formats 761 are described in <a href="{@docRoot}guide/topics/resources/resources-i18n.html">Resources</a>. </td> 762 </tr> 763 <tr> 764 <td valign="top"> layout/<br /> 765 <em>screen_1_layout</em>.xml<br /> 766 ...<br /></td> 767 <td valign="top">(<em>optional</em>) Holds all the XML files describing screens or parts 768 of screens. Although you could create a screen in Java, defining them 769 in XML files is typically easier. A layout file is similar in concept 770 to an HTML file that describes the screen layout and components. See <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> for more information about designing screens, and <a href="{@docRoot}guide/topics/resources/available-resources.html#layoutresources">Available Resource Types</a> for the syntax of these files.</td> 771 </tr> 772 <tr> 773 <td valign="top"> values/<br /> 774 arrays<br /> 775 classes.xml<br /> 776 colors.xml<br /> 777 dimens.xml<br /> 778 strings.xml<br /> 779 styles.xml<br /> 780 values.xml<br /></td> 781 <td valign="top"><p>(<em>optional</em>) XML files describing additional resources 782 such as strings, colors, and styles. The naming, quantity, and number 783 of these files are not enforced--any XML file is compiled, but these 784 are the standard names given to these files. However, the syntax 785 of these files is prescribed by Android, and described in <a href="{@docRoot}guide/topics/resources/resources-i18n.html">Resources</a>. </p> 786 </td> 787 </tr> 788 <tr> 789 <td valign="top"> xml/</td> 790 <td valign="top">(<em>optional</em>) XML files that can be read at run time on the device. </td> 791 </tr> 792 <tr> 793 <td valign="top"> raw/</td> 794 <td valign="top">(<em>optional</em>) Any files to be copied directly to the device. </td> 795 </tr> 796</table> 797 798 799<a name="logging" ></a> 800<h2>Print Messages to a Log File</h2> 801 802<p>To write log messages from your application:</p> 803<ol><li>Import <code>android.util.Log</code>.</li> 804 <li>Use <code>Log.v()</code>, <code>Log.d()</code>, <code>Log.i()</code>, 805 <code>Log.w()</code>, or <code>Log.e()</code> to log messages. 806 (See the {@link android.util.Log} class.)<br/> E.g., 807 <code>Log.e(this.toString(), "error: " + err.toString())</code></li> 808 <li>Launch <a href="{@docRoot}guide/developing/tools/ddms.html">DDMS</a> from a terminal 809 by executing <code>ddms</code> in your Android SDK <code>/tools</code> path.</li> 810 <li>Run your application in the Android emulator.</li> 811 <li>From the DDMS application, select the emulator 812 (e.g., "emulator-5554") and click <b>Device > Run logcat...</b> 813 to view all the log data.</li> 814</ol> 815<p class="note"><strong>Note:</strong> If you are running Eclipse and 816encounter a warning about the VM debug port when opening DDMS, you can ignore it 817if you're only interested in logs. However, if you want to further inspect and 818control your processes from DDMS, then you should close Eclipse before launching DDMS so that 819it may use the VM debugging port.</p> 820 821 822