Author: Dave MacLean, Satya Komatineni, Grant Allen
Pub Date: 2015
Size: 10 Mb
Pro Android 5 shows you how to build real-world and fun mobile apps using the Android 5 SDK. This book updates the best-selling Pro Android and covers everything from the fundamentals of building apps for smartphones, tablets, and embedded devices to advanced concepts such as custom components, multi-tasking, sensors/augmented reality, better accessories support and much more.
- Using the tutorials and expert advice, you’ll quickly be able to build cool mobile apps and run them on dozens of Android-based smartphones.
- You’ll explore and use the Android APIs, including those for media and sensors.
- And you’ll check out what’s new in Android, including the improved user interface across all Android platforms, integration with services, and more.
By reading this definitive tutorial and reference, you’ll gain the knowledge and experience to create stunning, cutting-edge Android apps that can make you money, while keeping you agile enough to respond to changes in the future.
Introduction to Android Application Architecture
In the first section of this chapter, a one-page calculator app will give you a bird’s eye view of writing applications using the Android SDK. Creating this app will demonstrate how to create the Ul, write Java code to control the Ul, and build and deploy the app.
In addition to demonstrating the Ul, this calculator app will introduce you to activities, resources, and intents. These concepts go to the heart of Android application architecture. We will cover these topics in detail in the second section of the chapter in order to give you a strong footing for understanding the rest of the Android SDK. We will also cover the activity life cycle and a brief overview of the persistence options for your application.
In the third section we will give you a roadmap for the rest of the book that addresses basic and advanced aspects of building Android applications. This final section breaks the chapters into a set of learning tracks. This section is a broad introduction to the entire set of Android APIs.
Furthermore, in this chapter you will find answers to the following: How can 1 create Ul with a rich set of controls? How can 1 store state persistently? How can 1 read static files that are inputs to the app? How can 1 reach out and read from or write to the web? What other APIs does Android provide to make my app functional and rich?
Without further ado, let’s drop you into the simple calculator application to open up the world of Android.
This chapter laid out everything you need to understand to create mobile applications with the Android SDK. You have seen how U1 is constructed. You know what activities are. You know the intricacies of the activity life cycle. You understood resources and intents. You know how to save state. Finally, you got to see the breadth of the Android SDK by reading the learning tracks that summarized the rest of the book. We hope these first two chapters gave you a head start for your development efforts with the Android SDK.
Advanced AsyncTask and Progress Dialogs
In this chapter, in addition to covering AsyncTask, vve have introduced you to progress dialogs, progress bars, headless retained fragments, and ADOs. Reading this chapter, you not only understood AsyncTask but also got to apply your understanding of activity life cycle and a deep understanding of fragments. We have also documented a set of key test cases that must be satisfied for a well-behaved Android application.
Exploring Android Persistence and Content Providers
Saving State Using Shared Preferences
We have covered shared preferences in Chapter 11. Shared preferences are key/value-based XML files owned by your application. Android has a framework on top of this general persistence mechanism to display/update/retrieve preferences without writing a lot of code. This latter aspect is the main topic of Chapter 11.
Chapter 11 also touched briefly on how an application can store any type of data using the shared preference API in XML files. In this approach data is converted to a string representation first and then stored in the preferences key/value store. This approach can be used to store any arbitrary state of your application as long as it is small to medium in size.
The shared preference XML files are internal to the application on your device. This data is not directly available to other applications. End user cannot directly manipulate this data by mounting on to a USB port. This data is removed automatically when the application is removed.
From simple to moderate application persistence needs, you can take advantage of shared preferences by storing various trees of Java objects directly in a shared preference file. In a given preference file you can have a key point to a serialized Java object tree. You can also use multiple preference files tor multiple Java object trees. We have used JSON/GSON library from google to do this conversion from Java objects to their equivalent JSON string values quite effectively. In this approach a Java object tree is streamed as a JSON string using the google GSON library. This tree is then stored as a value in a key/value pair of a preference file. Keep in mind that GSON and JSON conversion of a Java object may have some limitations. Read the GSON/JSON documentation to see how complex a Java object can get to make this approach work. We are fairly confident that for most data-based Java objects this will work.
This chapter has covered a lot of aspects about a vital need of your applications: persistence. We have given you a plethora of options available in Android for persistence and how to choose an appropriate option. We have covered how to use SQLite for internal persistence needs in significant detail. We have shown you an industrial-strength API pattern for persistence using SQLite which can be extended to any persistence implementation. Importantly, this pattern showed you how to externalize transactions to keep your persistence code simple. We have then covered what content providers are, the nature of content URls, MIME types, how to use SQLite to construct providers that respond to URIs, how to write a new content provider, and how to access an existing content provider.
Deploying Your Application: Google Play Store and Beyond
You are now equipped to take on the world with your Android applications! Here is a rundown of the topics we covered in this chapter:
- How to get established as a Google Play Store Publisher (that is, Developer) so you can publish to Google Play Store.
- The rules as laid out in the Google Play Developer Distribution Agreement.
- Giving Google its share of your revenue if you are selling through Google Play Store. We also discussed how Google does not want to see competition from within the Play Store.
- Your responsibility for paying taxes on revenues from your applications.
- The Google Play Store refund policy, both the published and the real one.
- How users can get copies of your application anytime in the future as long as they paid for it once.
- The Android branding rules. Make sure you don’t violate any copyright associated with Android, images, or fonts.
- The Developer Console and its features. The Developer Console collects user feedback and error reports from users.
- Preparing your application for production, including testing, LVL and ProGuard to fight piracy, and using resource variations and tags in AndroidManif est. xml to filter which devices your application will be available to.
- Advice regarding localizing your application by language and/or culture.
- The Google Play Store user interface, both on device and on the Internet/Web.
- The fact that Google Play Store is not the only game in town, and that you can sell your application in other places on the Internet, all at the same time.