Developing Android Applications with Adobe AIR [30]
A word of warning: during development, if you are using Flash Professional to install your application on the device, every uninstall/reinstall deletes previously saved data, including updates. In Flash Builder, you can prevent this behavior from occurring by unchecking the “Clear application data” box on each Launch option when you first create your project. If the user installs an update of your application, however, previously saved data is preserved.
WARNING
The user may delete data, cache, and SD card content. Make sure to check for the existence of a file before reading or writing to it to avoid runtime errors. The exception to this rule is the local SharedObject, which handles this automatically.
It is better to use filenames than paths to guarantee consistency across devices and platforms, and the resolvePath method to refine the path. We will cover this further in The Filesystem.
Here is the list of directories and their equivalent paths:
/data/data/app.appId/app/assets
app:/
File.applicationDirectory
/data/data/app.appID/appID/Local Store
app-storage:/
/data/data/app.appID/appID/Local Store
File.applicationStorageDirectory
/sdcard
File.documentsDirectory
File.userDirectory
File.desktopDirectory
/data/data/app.appId/cache
File.createTempDirectory()
File.createTempFile()
There are several ways to save persistent application data on your device. The amount of space your data requires, and the complexity of the data, will determine which approach to take. We will discuss these options next.
Local SharedObject
This is a simple and convenient approach to saving small amounts of data on your device (100 KB maximum). You don’t need to manage where the data is saved and if it already exists—this is handled automatically for you.
The SharedObject static class belongs to the flash.net package and inherits from EventDispatcher.
A Flash shared object is similar to a browser cookie, but it does not expire and it has a default size limit of 100 KB. The object is written in the proprietary ActionScript Message Format (AMF). You can store data types such as String, Array, XML, Number, Data, Object, and ByteArray. An application can only access its own SharedObject data. A single application can have multiple shared objects.
To use this approach, first get a reference to your application’s shared object. The getLocal() method creates it if it does not already exist:
import flash.net.SharedObject;
var so:SharedObject = SharedObject.getLocal("myApplication");
The preceding code creates a new directory in the application storage area and a shared object inside it called myApplication.sol.
To add data to a shared object, add attributes to its data (the data object itself is read-only). Here we are storing a string, an array, a boolean, and an object:
so.data.animal = "Hamster";
so.data.food = ["Grains", "Avocado", "Carrot"];
so.data.isVegetarian = true;
so.data.stuff = {toy:"Wheel", house:"Cage"};
To save these new attributes to the persistent local file, call the flush method. For devices, it is recommended that you save data right away to avoid losing the data if the application is terminated. Saving data can be performance-intensive, so carefully plan when your application initiates a save:
so.flush();
If you wish to monitor the save process, create a String variable to store what the flush method returns. If the result is pending, set a listener to trace the reason the data was not stored:
import flash.net.SharedObjectFlushStatus;
import flash.events.NetStatusEvent;
var flushStatus:String = so.flush();
if (flushStatus != null) {
switch(flushStatus) {
case SharedObjectFlushStatus.PENDING:
so.addEventListener(NetStatusEvent.NET_STATUS,
onFlushStatus);
break;
case SharedObjectFlushStatus.FLUSHED:
trace("success");
break;
}
}
function onFlushStatus(event:NetStatusEvent):void {
trace(event.info.code);
}
To check