IsolateStorageSettings lets you save any serializable object to the settings store. Managing the IsolatedStorage Space
While Windows Phone 8 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 available space carefully since it is a shared resource. Saving minimal amount of data can also speed up the launch of the application.
The techniques used in managing application state are the same in both platforms. An Android application and a Windows Phone 8 application should save minimal amount of data to application specific storage.
Sharing Data Between Applications Android data sharing
On Android, 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.
In Android, ContentProviders make data accessible to other 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 on internal storage, ContentProviders is another way to share data across applications.
Windows Phone data sharing
Unlike Android, Windows Phone 8 does not support access to external storage. By limiting the use of external storage, Windows Phone 8 provides the application with a safe-sandbox, and prevents unpredictable behavior caused by missing external storage or corruption of data by other applications.
Content producers
Windows Phone applications cannot directly access information from other information stores, such as the contacts list or data managed by other applications.
Windows Phone 8 uses a mechanism called Launchers and Choosers which allow one application to invoke other applications to access data managed by the phone or by other applications, such as contacts and photos. This avoids the risk of unsafe data access and data corruption.
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 invoke the PhoneCallTask with a phone number as a parameter. Windows Phone 8 OS invokes the Phone application with the supplied number and makes the requested call. When the invoked application completes, the invoking application is reactivated. The called application does not return any data resulting from the user’s actions.
Choosers
A Chooser is similar to a launcher except that a Chooser returns data back to the calling application.
For example, consider that in your app you want the user to select a photo from the photo gallery. Instead of allowing your app to access photo gallery, Windows Phone allows your app to invoke the built-in PhotoChooserTask and select a photo. Then the Chooser returns that photo to your app. .
Summary
In this chapter we compared data storage and access mechanisms for Windows Phone and Android. We also looked at the file and folder storage apis in Windows Phone and Android as well as for application settings. We looked at launchers and choosers from Windows Phone 8 which allow one app to invoke another without allowing direct data sharing among apps.
Related Resources
To learn more, visit:
Launchers and Choosers for Windows Phone
Chapter 7: XML Support in Windows Phone and Android
This chapter discusses the XML parsing methods in Windows Phone 8 and compares them with the XML parsing methods in Android.
With its use of Java, the Android SDK includes rich support for handling XML. In particular, Android supports Java's Simple API for XML (SAX) and the Document Object Model (DOM) parser. In addition, Android support a pull parser that works similar to Java StAX. Windows Phone 8 supports an efficient DOM
XML Support in Windows Phone
While the SAXParser and the DOMParser are not available in Windows Phone 8, Windows Phone 8 provides a comparable, if not richer, mechanism for handling XML using two separate mechanisms, namely, XMLReader and LINQ to XML. XMLReader is similar to Android pull parser i.e. it reads the XML document node one at a time and parses it. Both XMLReader and LINQ to XML provide read-only functionality; XML cannot be inserted or deleted. Unlike DOM parser, iXMLReader does not load the entire document at once. Unlike SAXParser, XMLReader is not event driven.
XML Parsing Using XmlReader
XmlReader provides a forward-only and read-only access to a stream of XML data.
The following C# snippet illustrates how to use XmlReader and query the XML data stream to determine the current node type. It also shows the use of XmlWriter to write an XML document.
StringBuilder output = new StringBuilder();
String xmlString =
@"
- test with a child element stuff
";
// Create an XmlReader
// Uses StringReader which reads from string
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
// XmlWriterSettings specifis options on XmlWriter, i.e. Indent = true
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
// XmlWriter is used to produce XML document
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
// Parse the file and display each node.
// In Android, following iteration is done in handler
//implementation while using SAXParser
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
// This is similar to XmlPullParser.START_TAG
writer.WriteStartElement(reader.Name);
break;
case XmlNodeType.Text:
// Similar to XmlPullParser.TEXT
writer.WriteString(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
// This is similar to XmlPullParser.END_TAG
writer.WriteFullEndElement();
break;
case XmlNodeType.Attribute:
// In Android, attribute would be read using getAttribute
break;
default:
break;
}
}
}
}
As you can see above, handling XML in Windows Phone has many similarities with XMLPullParser in Android.
Handling XML using LINQ to XML
Windows Phone provides another technology called LINQ to XML that makes it very easy to handle XML. It provides a versatile in-memory XML programming API. With LINQ to XML, you can read, write and manipulate XML. It helps you to:
Read and parse XML from memory, a local file or from a communication channel
Manipulate an XML document by inserting or deleting XML elements
Create an XML document or Save XML to a file or XmlWriter
Like the Document Class of the DOM parser, the XDocument Class in Windows Phone 8 represents an in-memory representation of the XML document.
Create an XML Document
Let us look at an example that shows how to create an XML document using LINQ to XML. The following code creates a document then adds a comment and element to the document. The sample code also creates another document using the results of a query. The XElement class, used in the sample code, is similar to the Element class in Android.
In the following example, we use LINQ to query an XML Document to extract all nodes whose value starts with “C” and construct another document.
String xmlString1 =
@"
- Beer
- Chips
- Salsa
- Cheesecake
";
XDocument srcTree = XDocument.Parse(xmlString1);
XDocument doc = new XDocument(
new XComment("This is a list of items"),
new XElement("ItemsWithC",
// The following query is similar to a SQL select statememt
// from all children of “Items”, select those that start with C
from el in srcTree.Element("Items").Elements()
where ((string)el).StartsWith("C")
select el
)
);
// In Android to parse the XML tags using DOM, the equivalent code is
// root.getElementsByTagName(ITEM);
output1.Append(doc + Environment.NewLine);
We use the following LINQ query to all children of “Items” node to extract those that start with “C”.
from el in srcTree.Element("Items").Elements()
where ((string)el).StartsWith("C")
select el
You can compare this functionality with the DOMParser in Android where you can build an XML document from bottom-up. You create the document and add XML elements to the document.
Functional Construction
LINQ to XML supports functional construction to construct XML using a single statement. Functional construction uses the XElement and XAttribute constructors to build an XML tree. For example, consider the following XML Data:
Patrick Hines
206-555-0144
425-555-0145
123 Main St
Mercer Island
WA
68042
10
Gretchen Rivas
…
The following example constructs an XML tree by using LINQ to XML functional construction:
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144",
new XAttribute("Type", "Home")),
new XElement("phone", "425-555-0145",
new XAttribute("Type", "Work")),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
Share with your friends: |