Ten Tips for Android Application Development

来源:互联网 发布:北京ds数据精灵 编辑:程序博客网 时间:2024/04/18 18:49

Beginner tips

Learn SQL and put your data models in a database

Android has an excellent persistence system; use it to make your applications more robust in case of failure or shutdown, and to make suspending and resuming more efficient. Android's SQLite-based storage system is thoroughly integrated with Android's user interface classes.

Android supports the observer pattern in Cursor, Adapter, and View classes, enabling the user interface to connect directly to the results of database queries. Using these capabilities means less work when Android wants to stop your application because you have little or no data that needs to be to be explicitly saved, and more reliability in running on mobile devices that can be out of battery at any time.

The database classes in the Android framework manage caching and are more efficient than reading a whole data model into objects in memory.

But, in oder to take maximum advantage of the Android way of designing data models, you will need to know some SQL.

Learn XML

You don't have to know much about XML to use the visual editing tools for defining layouts and other resources. You are, in theory, hardly ever required to edit XML "by hand." But these tools are imperfect. The more you know about how Android uses XML, and about the Android XML schema, the easier it is to fix problems that occur in XML in Android applications, and the more comfortable you will be using XML to maximum effect in developing Android applications. A general XML book, like XML in a Nutshell (O'Reilly), or Learning XML (O'Reilly) can be useful.

Learn Eclipse's features and foibles

Java can be a verbose language, and without code completion, Javadoc pop-ups, and advanced refactoring, it can be a drag compared to dynamic languages. In other words, Java without Eclispe's productivity features would be no fun at all.

Eclipse, however, can have a steep learning curve for beginners. Getting comfortable with this big, powerful, and sometimes perplexing IDE is a big part of being productive in Android application development. Steve Holzner's book Eclipse (O'Reilly) is an excellent introduction and will get you up to speed.

Within the Eclipse documentation pages, you can get a good start by reading the pages covering Eclipse concepts. Get to know these concepts early on, otherwise the Eclipse menus and documentation will seem full of jargon. Then go on to the basic tutorial in the Java development section.

Use automated testing

Use the Monkey to stress test your application. It generates a stream of events, simulating random inputs, and reports application crashes or lack of response.

Use the Instrumentation Framework and JUnit to write unit tests that can be run from Eclipse using adb and the InstrumentationTestRunner.

Be a good citizen in the society of Android applications

Learn and use the application and service lifecycles effectively. Enable your application to be efficiently stopped and restarted by the Android system. Unlike many runtime environments with almost trivial application lifecycles, Android's runtime environment frequently stops or terminates processes to keep application resource consumption low, being prepared to restart them if the user navigates back to an application.

Android expects applications to override and respond to a set of application lifecycle methods and cooperate with the way Android manages applications as they become visible on the screen or are obscured by other applications. Get this code into your application early on in its development, so that you do not end up retro-fitting these method overrides. Use the debugger to set breakpoints in the application lifecycle methods so that you get a feel for how the Android application lifecycle works.

Advanced tips

Use static analyzers

Java lends itself well to meaningful compiler warnings, and to warnings and "advice" based on static analysis. Every potential bug you kill before your application leaves development brings you closer to a 4-star user rating instead of a 2-star one. Use the compiler warnings, FindBugs, and try other static analyzers, like PMD available as Eclipse plug-ins to find the tools that find the most problems and the fewest false positives.

Use and be used

Use Intent objects and StartActivity (and related methods) to "borrow" functionality from other applications, and write your application to respond to Intent filter matches and provide functionality to other applications. Appications can also share data through the ContentProvider system and through remote method interfaces.

Divide large applications

If you are writing a large application, consider dividing it into a suite of applications and services. Smaller applications load faster and use fewer resources. Making a suite of applications, content providers, and services makes your code more open to incorporation into other applications as described the "Use and be used" tip.

Design for low power consumption

Things that are benign on a personal computer, like polling a server every 10 minutes, can cut a handset's battery life in half or worse. Code your application to do as little as possible until the user brings it to the foreground or some external information arrives that requires action.

Use the "Battery use" in the "About phone" menu in "Settings" to find the applications and other system functions using the battery.

Use the NDK to access library functions

The Android NDK is an optional companion to the Android SDK that enables use of native compiled code. Use the NDK to incorporate existing C and C++ libraries where they provide useful functionality. Code CPU-intensive computations, if they must be done on the handset, in C or C++. Use the NDK's tools to call that code from Java using the Java Native Interface (JNI).

Tips for good user interfaces

In addition to the tens tips above, here are some tips for creating a better user experience on Android:

Avoid depending on specific dimensions

Avoid assuming anything about screen size and screen resolution. Specify layouts so that they adapt to changes in screen size. Specify dimensions in resolution-independent units, not in pixels, which may be much bigger or smaller on screens with different resolutions. Make sure your application works equally well in horizontal and veritcal orientations, and with or without an on-screen keyboard displayed. Configure the emulator to test different screen sizes and resolutions.

If your application includes bitmap graphics that don't look good resized - e.g., highly detailed game sprites - include small, medium, and large variants for different screen resolutions, and if your application needs specific screen sizes, use manifest attributes to indicate these requirements to the Android system.

Enable direct manipulation

Direct manipulation means enabling the users to perform operations in an intuitive way directly on what they see. If the user can trigger an action on an object by dragging it to a place on the screen, or if a menu appears in the context of an object when the user long-presses it, that is direct manipulation.

The opposite of direct manipulation is a rigid sequence of operations, or a cascading model where the user descends into a hierarchy of dialogs and is trapped until he or she completes a sequence of operations.

Which would you rather use? Be kind to your users and enable direct manipulation wherever you can. This can be challenging on a small screen, but the reward is correspondingly large. Direct manipulation is your most powerful tool for keeping a user interface simple and friendly, and minimizing the number of different screens a user must visit in your application.

Avoid hierarchy

Don't design your application around a central location, "home" screen, or fixed starting place. Mobile device users want instant access to functionality. They won't tolerate moving up and down menu hierarchies. Furthermore, you want to provide stand-alone activities that are easy for other applications to invoke through the Intent interface. Allow each activity to be used independently, and include a menu in it that allows access to every other activity. In short, embed your application into a seamless Android user experience.

Internationalize early

This will prevent you from getting lazy and embedding strings in your code. Android supports 26 Locales, and will provide more support for internationalization in the future - externalize your strings now and save yourself the pain later.

These tips were compiled by the authors of Android Application Development and Andy Oram.