1、 shareObj.mxml
2、 MySharedObj.as
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
minWidth="1024" minHeight="768"
creationComplete="init();">
<fx:Script>
<![CDATA[
private var sharedObj:MySharedObj;
private function init():void{
sharedObj = new MySharedObj();
}
]]>
</fx:Script>
<mx:Button id="readBut" x="100" y="100" label="read" click="trace(sharedObj.myReadValue());"/>
<mx:Button id="saveBut" x="200" y="100" label="save" click="sharedObj.mySaveValue('hello', 10000);"/>
<mx:Button id="clearBtn" x="300" y="100" label="clear" click="sharedObj.myClearValue();"/>
</s:Application>
2、 MySharedObj.as
package{
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
public class MySharedObj{
private var sharedObj:SharedObject;
public function MySharedObj(){
}
/*read if not then this value will return null*/
public function myReadValue():String{
sharedObj = SharedObject.getLocal("shareObj");
return sharedObj.data.savedValue;
}
/*save*/
public function mySaveValue(sharedValue:String, flushLength:Number):void{
sharedObj.data.savedValue = sharedValue;
var flushStatus:String = null;
try{
flushStatus = sharedObj.flush(flushLength);
}catch(error:Error){
trace("error message: " + error.message);
}
if(flushStatus != null){
switch(flushStatus){
case SharedObjectFlushStatus.PENDING:
sharedObj.addEventListener(NetStatusEvent.NET_STATUS, nowFlushStatus);
break;
case SharedObjectFlushStatus.FLUSHED:
trace("access saved");
break;
}
}
}
/*clear*/
public function myClearValue():void{
delete sharedObj.data.savedValue;
}
/*SharedObjectFlushStatus.PENDING*/
private function nowFlushStatus(event:NetStatusEvent):void{
switch(event.info.code){
case "SharedObject.Flush.Success": //flush auccess
trace("User granted permission, value saved");
break;
case "SharedObject.Flush.Failed": //flush failed
trace("User denied permission, value not saved");
break;
}
sharedObj.removeEventListener(NetStatusEvent.NET_STATUS, nowFlushStatus);
}
}
}