DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Android to Windows Phone 8: Working with Key-Value Pairs

Android to Windows Phone 8: Working with Key-Value Pairs

Adam Grocholski user avatar by
Adam Grocholski
·
May. 24, 13 · Mobile Zone · Interview
Like (0)
Save
Tweet
3.38K Views

Join the DZone community and get the full member experience.

Join For Free

over the next several posts i’ll show you how to work with local data on the windows phone 8 platform and compare it to working with data on the android platform.

in android development, if you have a small collection of key-value pairs that you want to persist from session to session you use the sharedpreferences apis. in windows phone development these key-value pairs are often referred to as application settings .

to save or retrieve data as a key-value pair in windows phone 8 you use the isolatedstoragesettings class. the isolatedstoragesettings class is nothing more than a dictionary that enables you to store any serializable object, such as a string, and is ideally suited for saving small pieces of data. data stored with the isolatedstoragesettings class is persisted in the app’s local folder when the app is closed or deactivated and automatically populated with previously persisted data when the app is launched.

android tip
the sharedpreferences class in android is similar to the isolatedstoragesettings class in windows phone apps.
windows phone tip
since application settings are tied to the lifetime of the app on a device, you should not use application settings to store information your users might thing as valuable or irreplaceable. you should consider using the user’s libraries or skydrive for permanent data.

it is important to note that once an application is removed, all of its data stores, including those associated with settings are deleted from the device.

let’s now look at how you write and read application settings.

creating an application setting

to create a shared preference in android a key of name and a value of me you would write code similar to the following:

sharedpreferences sharedpref = getactivity().getpreferences(context.mode_private);
sharedpreferences.editor editor = sharedpref.edit();
editor.putstring("name", "me");
editor.commit();
android tip
in android you can create preference files using mode_world_readable and mode_world_writeable which allows other apps that know your file identifier to access you data. in windows phone applications are sandboxed and their settings cannot be accessed by other apps.

to create a similar key-value pair in a windows phone app you would write the following:

var localsettings = isolatedstoragesettings.applicationsettings;
localsettings.add("name", "me");

reading an application setting

to retrieve a shared preference in android with a key of name and a default value of me (i.e. the value returned if the key is not found) you would write the following:

sharedpreferences sharedpref = getactivity().getpreferences(context.mode_private);
string namevalue = sharedpref.getstring("name", "me");

to perform a similar operation on the settings store in a windows phone app you would write:

var localsettings = isolatedstoragesettings.applicationsettings;
string namevalue = (string) localsettings["name"];

deleting an application setting

to remove a shared preference in android you use the following code:

sharedpreferences sharedpref = getactivity().getpreferences(context.mode_private);
sharedpreferences.editor editor = sharedpref.edit();
editor.remove("name");
editor.commit();

to delete a setting from a windows phone 8 app you would use:

var localsettings = isolatedstoragesettings.applicationsettings;
localsettings.remove("name");
windows phone hint
do not be tempted to use the localsettings and/or roamingsettings properties of the applicationdata class in place of the applicationsettings property of the isolatedstoragesettings class. while the api’s are available, they have not been implemented in windows phone 8. an exception will be thrown if they are called.



Windows Phone Android (robot) app application Data (computing)

Published at DZone with permission of Adam Grocholski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Screen Sharing in Java
  • Refactor Switch to a One-Liner
  • What Are Microservices?
  • No Code Expectations vs Reality

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo