Chapter 5. Using On-Phone Data

In the last three chapters we focused our efforts and energy on building a usable and, even more important, a marketable application. Nevertheless, users are still unable to really interact with our application. Indeed, they cannot create a profile, save preferences, or any other data. To address this shortfall, the Android platform allows the utilization of SQLite as an embedded, light, and powerful database. Moreover, in this chapter we will also take advantage of the LinQ mechanisms of the .Net framework to interact with this data.

In this chapter, we will pass through the following recipes:

  • Storing preferences
  • Simple file reading/writing
  • Serializing and deserializing objects into files
  • Using the SQLite database

Introduction

As we stated in the preamble of this chapter, data is important to your users. However, unlike normal desktop applications where a database is—more or less—always the right answer for assessing data storage: phones are different, they have low storage capacities. Thus, the amount of space users will allow you to use on their phones is ridiculously low because they cannot afford to lose 1 GB for each application they install.

Consequently, as programmers, we have to adapt our applications to this special environment. If you aim to reduce the data footprint of your applications to the minimum, you must master the weapons the Android platform offers you.

The storage mechanisms in Android are oriented around three different methods.

  • The first one is the persistence of preferences, a.k.a user profiles or similar. The good news here is that Android has a built-in mechanism for storing key-value relationships. Therefore, if you are looking to store Application_theme = blue, drop the database thing and turn on the built-in mechanism we will see during the first recipe in this chapter.
  • The second one is text-based files. We can do a lot with these good old text files. Indeed, we can store notes that users are taking or web pages they want to cache. Also, if we use a little more than plain text files, let's say, XML or JSON files, we can take advantage of Serialization. Serialization is a mechanism allowing the persistence of C# objects into a file in order to re-use them later or to send them to a server/other users. In the second recipe in this chapter, we will see a simple note-saving mechanism and the .NET library to use Serialization.
  • Finally, our last option: a database. In the last recipe of this chapter, we will learn how to use the SQLite engine as our database and how to retrieve data using the Microsoft ActiveX Data Objects (ADO).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.239.46