Recipe Utilizing the SEARCH Key

If an activity in the in-focus application is defined to be searchable, the SEARCH key invokes it. A menu choice or equivalent should always be a redundant way to call the searchable activity to accommodate devices without a SEARCH key. The menu choice simply needs a call to onSearchRequested . The searchable activity ideally should be declared as singleTop launch mode, as discussed in Chapter 2,Application Basics Activities and Intents.This enables multiple searches to take place without...

Recipe Creating an App Widget

This recipe creates a simple App Widget that displays some text on the home screen. The text is configured to update every second, but note that by default, the Android system forces the minimum update time to be 30 minutes.This helps avoid poorly written widgets from draining the battery. Listing 3.17 implements an AppWidgetProvider, which is a subclass of BroadcastReceiver.The main method to override is the onUpdate function, which gets called when the system determines it is time to update...

Recipe Using a SeekBar

A seek bar is similar to a progress bar that can take user input to change the amount of progress. Current progress is indicated by a small sliding box called a thumb. A user can click and drag the thumb to visually indicate the new place to set the progress.The main activity is shown in Listing 4.23. Listing 4.23 import android.app.Activity import android.os.Bundle import android.widget.SeekBar public class SeekBarEx extends Activity public void onCreate Bundle savedInstanceState...

Sensors

Smartphones are becoming sensor hubs in a way, opening a rich experience for users. Other than the microphone that every phone has, the first additional sensor introduced on phones was the camera. Different phone cameras have varying capabilities, and this is an important factor for people in selecting a device.The same type of diversity is now seen with the additional sensors. Most smartphones have at least three basic sensors a three-axis accelerometer to measure gravity, a three-axis...

Recipe Using Sound Resources Efficiently

To keep the smaller memory requirements of compressed audio files but also the benefit of lower-latency playback of raw audio files, the SoundPool class can be used.This uses the MediaPlayer service to decode audio and provides methods to repeat sound buffers and also speed them up or slow them down. Usage is similar to other sound recipes initialize, load a resource, play, and release. However, note that the SoundPool launches a background thread, so a play right after a load might not produce...

Recipe Scheduling a Runnable Task from the Main Thread

This recipe implements a clock timer, which is often needed in applications. For example, it can be used in a game to keep track of how long a player takes to complete a level. This provides a simple way to handle user interaction while a background thread continues to run. The timer is run in a background thread so it does not block the UI thread, but it needs to update the UI whenever the time changes.As shown in Listing 3.5, the TextView text starts with a welcome message and the button text...

Recipe Listening for Phone States

The PhoneStateListener class provides information about the different telephony states on the device, including network service state, signal strength, and message waiting indicator voicemail . Some require an explicit permission as shown in Table 7.2. Table 7.2 The Possible Phone State Listener Events and Required Permissions LISTEN_CELL_LOCATION LISTEN_DATA_ACTIVITY Listen for call forward indicator changes Listen for direction of data traffic on cellular changes Listen for data connection...

Listing 917 srccomcookbookdatastorageDisplayDiariesjava

import java.text.DateFormat import java.util.ArrayList import java.util.Date import android.content.Context import android.database.Cursor import android.view.ViewGroup import android.widget.BaseAdapter import com.cookbook.data.Constants import com.cookbook.data.MyDB public class DisplayDiaries extends ListActivity MyDB dba DiaryAdapter myAdapter private class MyDiary public MyDiary String t, String c, String r title t content c recorddate r public String title public String content public...

Recipe Using Implicit Intents for Creating an Activity

Implicit intents do not specify an exact component to use. Instead, they specify the functionality required through a filter, and the Android system must determine the best component to utilize. An intent filter can be either an action, data, or a category. The most commonly used intent filter is an action, and the most common action is ACTION_VIEW.This mode requires a uniform resource identifier URI to be specified and then displays the data to the user. It does the most reasonable action for...

Recipe Using a Countdown Timer

The previous recipe is an example of handlers and a functional timer.Another timer is provided with the built-in class CountDownTimer.This encapsulates the creation of a background thread and the handler queuing into a convenient class call. The countdown timer takes two arguments the number of milliseconds until the countdown is done and how often in milliseconds to process onTick callbacks.The onTick method is used to update the countdown text. Note that otherwise the recipe is identical to...

Recipe Creating a Form

A form is a graphical layout with areas that can take text input or selection. For text input, an EditText object can be used.After it is declared, some Java code needs to capture the text entry at run-time.This is done as shown in Listing 4.14. Note that the content of the text entry textResult in this example should not be modified.A copy of the content can be made in case modification is needed. Listing 4.14 Capturing Text from an EditText Object EditText textResult EditText findViewById...

Threads

BAD USAGE function call to time-consuming function causes main thread to hang Figure 3.1 An example of the message that displays when a thread hangs. Figure 3.1 An example of the message that displays when a thread hangs. This means any user request such as navigating back to the home screen or multiple pushes of an onscreen button are not registered until the music is completely finished playing. The unresponsive UI might even cause the Android system to show an error such as the previous one...

Recipe Using an HTTP GET

Besides launching a browser or using the Webview widget to include a WebKit-based browser control in an activity, developers might also want to create native Internet-based applications. This means the application relies on only the raw data from the Internet, such as images, media files, and XML data. Just the data of relevance can be loaded.This is important for creating social networking applications.Two packages are useful in Android to handle network communication java.net and android.net....

Recipe Manipulating Raw Audio

The MediaRecorder MediaPlayer framework is useful for most audio uses, but to manipulate raw audio straight from the microphone, process it without saving to a file, and or play back raw audio, use AudioRecord AudioTrack instead. First, set the permission in the AndroidManifest XML file lt uses-permission gt Then, the steps to record are 1. Create an AudioRecord instance, specifying the following to the constructor Audio source Use one of the MediaRecorder.AudioSource choices described in the...

Recipe Adding Markers on a Map

The itemizedOverlay class provides a way to draw markers and layovers on top of a MapView. It manages a set of OverlayItem elements, such as an image, in a list and handles the drawing, placement, click handling, focus control, and layout optimization for each element. Create a class that extends ItemizedOverlay and override the following addOverlay Adds an Overlayltem to the ArrayList.This calls populate , which reads the item and prepares it to be drawn. createItem Called by populate to...

Recipe Using Multitouch

A multitouch event is when more than one pointer such as a finger touches the screen at the same time. This is identified by using a touch listener OnTouchListener, which receives multiple types of motion events action_down A press gesture has started with a primary pointer finger . action_pointer_down A secondary pointer finger has gone down. action_move A change in press location has changed during a press gesture. action_pointer_up A secondary pointer was released. action_up A primary...