17.3.保存本地共享对象

问题
我想保存LSO到客户机上
解决办法
使用SharedObject.flush( )方法
讨论
以下几种情况会导致Flash自动试图保存LSO数据到硬盘上:当Flash播放器关闭时,当共享对象被垃圾回收时,当调用SharedObject.clear( )方法时。但是自动保存功能并不很实用,因为还有很多原因需要及时保存共享对象数据,因此我们可以显式调用SharedObject.flush( )方法:
+展开
-ActionScript
var flushResult:String = example.flush( );

flush( )方法有个可选的参数用于指定最小的硬盘空间,单位为字节,默认为0,指用最小的空间正好存储本地共享对象。

当flush( )方法触发时,它试图把数据写到客户端上,调用结果有三种:
如果用户拒绝存储或Flash播放器因某种原因导致存储失败,该方法会抛出一个Error。
如果本地存储空间不够导致数据不能保存,该方法返回SharedObjectFlushStatus.FLUSHED。
如果用户没有分配足够的空间,该方法返回SharedObjectFlushStatus.PENDING。

三种情况中,当flush( )方法返回SharedObjectFlushStatus.PENDING常量时,用户可以选择授权或拒绝保存数据,当用户做出选择后,netStatus事件被激活,需要定义一个事件处理函数,当事件处理函数被触发时,传递进一个类型为flash.events.NetStatusEvent的事件,检查info.code 属性值判断用户是同意(SharedObject.Flush.Success)还是拒绝(SharedObject.Flush.Failed)

这里有个例子调用flush( )保存数据,处理可能返回的结果:
+展开
-ActionScript
var example:SharedObject = SharedObject.getLocal( "example" );
example.data.someData = "a value";
try {
var flushResult:String = example.flush( );
// If the flush operation is pending, add an event handler for
// netStatus to determine if the user grants or denies access.
// Otherwise, just check the result.
if ( flushResult == SharedObjectFlushStatus.PENDING ) {
// Add an event handler for netStatus so we can check if the user
// granted enough disk space to save the shared object. Invoke
// the onStatus method when the netStatus event is raised.
example.addEventListener( NetStatusEvent.NET_STATUS, onStatus );
else if ( flushResult == SharedObjectFlushStatus.FLUSHED ) {
// Saved successfully. Place any code here that you want to
// execute after the data was successfully saved.
}
catch ( e:Error ) {
// This means the user has the local storage settings to 'Never.'
// If it is important to save your data, you may want to alert the
// user here. Also, if you want to make it easy for the user to change
// his settings, you can open the local storage tab of the Player
// Settings dialog box with the following code:
// Security.showSettings( SecurityPanel.LOCAL_STORAGE );.
}
// Define the onStatus() function to handle the shared object's
// status event that is raised after the user makes a selection from
// the prompt that occurs when flush( ) returns "pending."
function onStatus( event:NetStatusEvent ):void {
if ( event.info.code == "SharedObject.Flush.Success" ) {
// If the event.info.code property is "SharedObject.Flush.Success",
// it means the user granted access. Place any code here that
// you want to execute when the user grants access.
else if ( event.info.code == "SharedObject.Flush.Failed" ) {
// If the event.info.code property is "SharedObject.Flush.Failed", it
// means the user denied access. Place any code here that you
// want to execute when the user denies access.
}
// Remove the event listener now since we only needed to listen once
example.removeEventListener( NetStatusEvent.NET_STATUS, onStatus );
};

如果确切知道存储数据的大小,可直接给flush( )传参数:
+展开
-ActionScript
// Request 500 KB of space for the shared object.
var flashResult:String = example.flush( 500 * 1024 );

加支付宝好友偷能量挖...


评论(0)网络
阅读(113)喜欢(0)flash/flex/fcs/AIR