Android to WP7-Chapter 6: Storing Data and Preferences

Date: Friday, June 3, 2011, 10:46:44 AM

Tags:

Table of Contents

Windows Phone (WP7) features a very comprehensive system of managing data for applications on the phone. This section compares the data management features of WP7 and Android.

Local Data Storage

Both Android and Windows Phone provide three different ways for the application to store data locally:

  1. Application settings
  2. Files and folders
  3. Database

image

Windows phone uses IsolatedStorage mechanism to store data supported by the above abstractions. We will look at each of the Windows Phone features in detail below.

image

Windows Phone Isolated Storage

WP7 provides IsolatedStorage, a feature that can be used to store application specific data securely on the phone. IsolatedStorage provides security by isolating the data for different applications from each other. Data belonging to an application can only be accessed by that application alone.

IsolatedStorage can be compared with files stored in Android's internal storage. Files stored in the internal storage are private to each application.

Android allows an application to share its internal data with other applications using an abstraction called content provider. On the other hand, Windows Phone provides complete isolation between applications.

The following diagram shows the folder structure for a storage application:

clip_image001

The following table compares the Data Storage Methods in Windows Phone and Android:

image

To know more about the comparison between the data storage methods, visit:

In both operating systems, the application developer has to manage the data that gets stored during application installation or reinstallation. The developer has to modify and migrate data if the application is updated; the OS does not manage data for the application.

Both operating systems delete the application files and folders in the private store when the application is uninstalled.

Settings

Android provides SharedPreferences class to save and retrieve persistent key-value pairs of primitive data types. This data survives across application sessions. Similarly, Windows Phone provides the IsolatedStorageSettings class to persist key-value pairs. Windows IsolatedStorageSettings are stored in the application specific IsolatedStorage.

Files and Folders

Android applications can create and use files and folders within the internal storage that is private to an application. Similarly, WP7 applications can create files and folders in the IsolatedStorage. They use IsolatedStorageFile and IsolatedStorageFileStream classes for folder and file operations. StreamReader and StreamWriter classes may be used for reading and writing to files created in IsolatedStorage.

The following table shows how various file operations are accomplished on the two platforms.

image

Database

Android provides full support for the SQLite database. A database created in the application is available to that application alone. Starting with 7.1 release, Windows Phone supports SQL CE (Compact Edition), a relational database. he SQL CE database file resides in the application's IsolatedStorage. Similar to Android, database belonging to an application is available to that application alone. , no other application can access it.

Just as SQL commands can be executed in Android, WP7 databases can be accessed using Language Integrated Query (LINQ). Transact-SQL is not supported. Windows Phone applications use LINQ to SQL for all database operations; LINQ to SQL is used to define the database schema, select data, and save changes to the underlying database file residing in isolated storage. LINQ to SQL provides an object-oriented approach towards working with database and comprises of object model and runtime.

Use Cases

Storing the Configuration Settings and Data

Many applications need to store application settings and user data, such as user language preference, or last use time stamp.

In Android, you can store application settings and data by using the SharedPreferences class. The SharedPreferences class stores the key value pair of primitive data types. In WP7, you can save such data using the IsolatedStorageSettings class. This is the most convenient way to store the data. The application can store its settings store by using the following code:

IsolatedStorageSettings.ApplicationSettings.Add("some_property", value);
IsolatedStorageSettings.ApplicationSettings.Save();

The value object is serialized to a disk when you call the Save() method. You can read the data back by using the following code:

List<string> mydata = (List<string>)IsolatedStorageSettings.ApplicationSettings["some_property"];

Serializing Objects to XML

You may need to serialize an entire object to the file system to save the state of object or to save it to memory. In Java, you can serialize objects by using ObjectOutputStream. In WP7, you can serialize objects by using the StreamWriter class.

You can store objects as XML by using the following code. This code serializes a list of strings to the xml format and saves this format in IsolatedStorage:

using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()){
   using(IsolatedStorageFileStream fs = storage.CreateFile("myfile.wp")){
       using(StreamWriter writer = new StreamWriter(fs)){
          XmlSerializer serializer = new XmlSerializer(typeof(List<string>));
          serializer.Serialize(writer, myStringList);
       }
    }
}

After storing the data in the XML format, you can use LINQ to XML to utilize the data later. You can use this method to store any structured data on a disk.

Note: For details about LINQ to XML, see Chapter 7.

To read back the file, use the following code:

using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()){
   using(IsolatedStorageFileStream fs = storage.OpenFile("myfile.wp", FileMode.Open)){
      XDocument doc = XDocument.Load(fs);
      XmlSerializer serializer = new XmlSerializer(typeof(List<string>));
      List<string> myData = (List<string>)serializer.Deserialize(doc.CreateReader());
   }
}

Managing the IsolatedStorage Space

While WP7 OS does not impose a quota on the size of the application's IsolatedStorage, each application should make efficient use of available storage space. It is important to use the shared resource carefully. Saving minimal amount of data can also speed up the subsequent launch of the application.

The techniques used in managing application state are the same in both platforms. An Android application and a WP7 application should save minimal amount of data.

Windows Phone External Storage

You can save files to a shared external storage in an Android compatible device. The shared external storage can also include the internal removable storage. Android allows you to access and modify the files that are saved to the external storage by connecting to a computer

Unlike Android, WP7 does not support application's access to external storage. By limiting the use of external storage, WP7 provides the application with a safe-sandbox, and prevents unpredictable behavior caused by missing external storage or corruption of data by other applications.

Managing Content across Applications

In Android, ContentProviders store and retrieve data and make data accessible to all applications. There are a number of content providers for common data types such as audio, video, calendar and contacts. While Android does not provide shared data storage, ContentProviders are the way to share data across applications. Every content provider decides where its data is stored.

The WP7 execution model isolates every application in its own sandbox. Applications cannot access information from other information stores, such as the contacts list. Applications cannot programmatically access data managed by other applications either. To enable applications to perform these common tasks, WP7 offers a set of APIs called Launchers and Choosers. They allow applications to invoke other applications.

Launchers and Choosers

Launchers

A Launcher allows you to programmatically invoke another built-in application to accomplish some task. If your application needs to make a phone call, you can use launchers API to invokethe PhoneCallTask with a phone number as a parameter. WP7 OS invokes the Phone application with the supplied number which the user needs to complete. When the invoked application completes, the invoking application is activated again. The built-in application does not return any data resulting from the user's actions.

Choosers

A Chooser is an API that launches built-in applications in WP7 similar to Launchers. The difference between Launcher and Chooser is that Chooser returns data back to the calling application.

For example, consider that you want the user to select a photo to return to your application. Your application can launch the PhotoChooserTask using Choosers to show the built-in Photo Chooser application. The user can select a photo and return back to your application. WP7 returns the result of the Chooser, i.e. the photo in this case.

Summary

In this chapter we compared data storage guidelines for Windows Phone and Android. We also looked at the storage apis in Windows Phone and Android.

Related Resources

To learn more about the topics covered in this blog, visit:

 
blog comments powered by Disqus