|
Cyril Sermon (@admin) |
Using Internet Resources:
With Internet connectivity and WebKit browser, you might well ask if there’s any reason to create native Internet-based applications when you could make a web-based version instead.
There are several benefits to creating thick- and thin-client native applications rather than relying on entirely web-based solutions:
❑Bandwidth Static resources like images, layouts, and sounds can be expensive data consumers on devices with limited and often expensive bandwidth restraints. By creating a native applica-tion, you can limit the bandwidth requirements to only data updates.
❑Caching Mobile Internet access has not yet reached the point of ubiquity. With a browser-based solution, a patchy Internet connection can result in intermittent application availability. A native application can cache data to provide as much functionality as possible without a live connection.
❑Native Features Android devices are more than a simple platform for running a browser; they include location-based services, camera hardware, and accelerometers. By creating a native application, you can combine the data available online with the hardware features available on the device to provide a richer user experience.
Modern mobile devices offer various alternatives for accessing the Internet. Looked at broadly, Android provides three connection techniques for Internet connectivity. Each is offered transparently to the application layer.
❑GPRS, EDGE, and 3G Mobile Internet access is available through carriers that offer mobile data plans.
❑Wi-Fi Wi-Fi receivers and mobile hotspots are becoming increasingly more common.
While the details of working with specific web services aren’t covered within this post, it’s useful to know the general principles of connecting to the Internet and getting an input stream from a remote data source.
Before you can access Internet resources, you need to add an INTERNET uses-permission node to your application manifest, as shown in the following XML snippet:
<uses-permission android:name=”android.permission.INTERNET”/>
The following skeleton code shows the basic pattern for opening an Internet data stream:
String myFeed = getString(R.string.my_feed);
try {
URL url = new URL(myFeed);
URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
[ ... Process the input stream as required ... ]
}
}
catch (MalformedURLException e) { }
catch (IOException e) { }
Android includes several classes to help you handle network communications. They are available in the java.net.* and android.net.* packages.
Later in this chapter, there is a fully worked example that shows how to obtain and process an Internet feed to get a list of earthquakes felt in the last 24 hours.
Chapter 9 includes further details on Internet-based communications using the GTalk Service. Chapter 10 features more information on managing specific Internet connections, including monitoring connec-tion status and configuring Wi-Fi access point connections.
Android offers several ways to leverage Internet resources.
At one extreme, you can use the WebView widget to include a WebKit-based browser control within an Activity. At the other extreme, you can use client-side APIs such as Google’s GData APIs to interact directly with server processes. Somewhere in between, you can process remote XML feeds to extract and process data using a Java-based XML parser such as SAX or javax.
Detailed instructions for parsing XML and interacting with specific web services are outside the scope of this book. That said, the Earthquake example, included later in this chapter, gives a fully worked example of parsing an XML feed using the javax classes.
If you’re using Internet resources in your application, remember that your users’ data connections depend on the communications technology available to them. EDGE and GSM connections are notori-ously low bandwidth, while a Wi-Fi connection may be unreliable in a mobile setting.
Optimize the user experience by limiting the quantity of data being transmitted, and ensure that your application is robust enough to handle network outages and bandwidth limitations.
Dialog boxes are a common UI metaphor in desktop and web applications. They’re used to help users answer questions, make selections, confirm actions, and read warning or error messages. An Android Dialog is a floating window that partially obscures the Activity that launched it.
As you can see in Figure 5-5, Dialog boxes are not full screen and can be partially transparent. They generally obscure the Activities behind them using a blur or dim filter.
There are three ways to implement a Dialog box in Android:
❑Using a Dialog-Class Descendent As well as the general-purpose AlertDialog class, Android includes several specialist classes that extend Dialog. Each is designed to provide specific Dialog-box functionality. Dialog-class-based screens are constructed entirely within their calling Activity, so they don’t need to be registered in the manifest, and their life cycle is controlled entirely by the calling Activity.
❑Dialog-Themed Activities You can apply the Dialog theme to a regular Activity to give it the appearance of a Dialog box.
❑Toasts Toasts are special non-modal transient message boxes, often used by Broadcast Receiv-ers and background services to notify users of events. You learn more about Toasts in Chapter 8.
The Dialog class implements a simple floating window that is constructed entirely within an Activity.
To use the base Dialog class, you create a new instance and set the title and layout as shown below:
Dialog d = new Dialog(MyActivity.this);
Have the new window tint and blur the window it obscures.
Window window = d.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d.setTitle(“Dialog Title”);
d.setContentView(R.layout.dialog_view);
TextView text = (TextView)d.findViewById(R.id.dialogTextView); text.setText(“This is the text in my dialog”);