Windows Phone Guide for Android Application Developers Table of Content



Download 0.57 Mb.
Page13/15
Date04.05.2017
Size0.57 Mb.
#17233
1   ...   7   8   9   10   11   12   13   14   15

Summary


In this chapter we had a look at the different states of application life cycle in Android and Windows Phone. We also compared methods for saving application state data in Android and Windows Phone.

Related Resources


To learn more about the execution model for Windows Phone, visit:

Launching, resuming, and multitasking for Windows Phone

Implementing the Model-View-ViewModel pattern in a Windows Phone app

App structure and navigation models for Windows Phone

App manifest file for Windows Phone

Asynchronous Programming For Windows Phone 8

Quickstart: Working with settings in Windows Phone

Chapter 6: Storage on Windows Phone


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

Let us look at how Android and Windows Phone store data locally:



  1. Application settings

  2. Files and folders

  3. Database



Purpose

Android

Windows Phone

Application Settings

SharedPreferences

IsolatedStorageSettings

Files and Folders

File in Internal storage

IsolatedStorage, StorageFolder

Relational Database

SQLite

*2 local database of SQLite

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

Windows Phone Isolated Storage


Local folder is the root folder of your app’s data store. This store, also called IsolatedStorage, provides application specific storage. Local folder is isolated from other apps and as a result data belonging to an application can only be accessed by that application. Local folder or IsolatedStorage can be compared with files stored in Android’s internal storage which are also private to each application. Android allows an application to share its internal data with other applications using an abstraction called content provider. This feature is not available on Windows Phone.

The following diagram shows the folder structure for application storage in Windows Phone:



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

Storage Features

Windows Phone

Android

Storage

Store application data in local folder

Store the application data in the phone’s internal storage

Isolation

Local folder isolates files belonging to an application from other applications. Applications cannot access files belonging to other applications..

Files stored in internal storage area are private to each application. Other applications cannot access that data.

Sandbox

Windows Phone restricts all Input and Output (I/O) operations to local folder and installation folder3 and prevents the I/O operations from accessing operating system files. This Windows Phone feature prevents unauthorized access and data corruption.

Android prevents unauthorized data access by restricting I/O operations to the same application. However, you have an option to make app data public by writing to external storage.

To learn more about data storage on Windows Phone, visit:

Data for Windows Phone


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.


Files and Folders


Android applications can create and use files and folders within the internal storage that is private to an application. Similarly, Windows Phone 8 applications can create files and folders using StorageFolder. It uses use StorageFolder and StorageFile classes for folder and file operations. StreamReader and StreamWriter classes may be used for reading and writing to files created in local folder.

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






Android

Windows Phone

Access application specific storage




Windows.Storage.ApplicationData.Current.LocalFolder

Create File or open File

Context.openFileInput, Context.openFileOutput

StorageFolder.CreateFileAsync

StorageFolder.OpenStreamForReadAsync

StorageFolder.OpenStreamForWriteAsync


File IO

FileOutputStream.write

FileInputStream.read



StreamWriter.Write

StreamReader.Read



Get or Create Directory

Context.getDir

StorageFolder.CreateFolderAsync

StorageFolder.GetFolderAsync



Using files from local folder is similar to using regular files. The only difference is that you have to first access application specific local folder. Here is an example of how you can use local folder to write to files. In the first line, we retrieve local folder for the calling application. We create a file in that storage and use a StreamWriter to write to the file. This may be compared to opening a private file in Android using MODE_PRIVATE mode. Note that most file and folder operations are asynchronous. The methods ReadFile and WriteToFile are also marked using async keyword. These methods return a Task which represents an async operation.
private async Task WriteToFile()

{

// Get the local folder.

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

// Create a new folder name DataFolder.

var dataFolder = await local.CreateFolderAsync("DataFolder", CreationCollisionOption.OpenIfExists);

StorageFile newFile = await dataFolder.CreateFileAsync("logfile.txt", CreationCollisionOption.ReplaceExisting);

using (StreamWriter writer = new StreamWriter(await newFile.OpenStreamForWriteAsync()))

{

await writer.WriteLineAsync("new log");

writer.Close();

} // Write the data from the textbox.

}

The following code snippet that shows how to read data from files in local folder. The structure of this code is similar to the above code.



private async Task ReadFile()

{

// Get the local folder.

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

if (local != null)

{

// Get the DataFolder folder.

var dataFolder = await local.GetFolderAsync("DataFolder");

// Read the data.

using (StreamReader streamReader = new StreamReader(await dataFolder.OpenStreamForReadAsync("logfile.txt")))

{

string text = streamReader.ReadToEnd();

}

}

}

Application Settings


Most applications need to store and use application settings, options, and user data, such as user language preference, or last used 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. On Windows Phone 8, 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 by using the following code:




IsolatedStorageSettings.ApplicationSettings.Add(“some_property”, value);
IsolatedStorageSettings.ApplicationSettings.Save();

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

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



Download 0.57 Mb.

Share with your friends:
1   ...   7   8   9   10   11   12   13   14   15




The database is protected by copyright ©ininet.org 2024
send message

    Main page