shareobject 实现本地共享

1,发送方
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="add()" layout="absolute">

<mx:Script>
<![CDATA[
private function add():void
{
//定义本地共享对象  
          var   myLSO : SharedObject ;  
//创建本地共享对象  
myLSO   =   SharedObject . getLocal ("hje","/" );
//给共享对象赋值
//可以保存的有数字、字符串、布尔型、XML、日期型、数组型和对象等数据类型
var currentUserName:String   =   "Liu_hje" ;
var itemsArray:Array   =   new   Array( 101 , 346 , 222 )
var currentUserIsAdmin:Boolean   =   true ;
myLSO . data . userName   =   currentUserName ;
  myLSO . data . itemNumbers   =   itemsArray ;
  myLSO . data . adminPrivileges   =   currentUserIsAdmin ;
  //共享对象的写入会在应用程序退出时候自动进行
//如果需要强制写入,可以使用flush方法
myLSO . flush ();

}
]]>
</mx:Script>
</mx:Application>
2,接收方
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="tt(event)">

<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
private function tt(event:FlexEvent):void
{
var so1: SharedObject = SharedObject.getLocal("hje");
        Alert.show( so1.data.username);
        Alert.show( so1.data.username);
        so1.clear();
       
}
]]>
</mx:Script>

<mx:Button click="" label="Button" x="189.5" y="125"/>
</mx:Application>



//封装成类
package
{

public class ShareObjectManager
{
public function ShareObjectManager()
{
}
public static function getSOData(SOName:String,parModel:String):String
{

try
{
var SO: SharedObject = SharedObject.getLocal(SOName,"/");
    var arr_getData:Array=SO.data.arrayData;
  var result:String=arr_getData[parModel];
    SO.close();
    //so1.clear();//清除数据
}
catch(error:Error)
{
//CTIlog.error(error.message.toString());
}
return result;
}

public static function setSOData(SOName:String,parModel:String,parValue:String):void
{
var SO:SharedObject ;  
SO=SharedObject.getLocal (SOName,"/" );
var arr_sOData:Array = new Array();
arr_sOData[parModel] = parValue;
SO.data.arrayData=arr_sOData ;
Alert.show(arr_sOData[parModel]);
SO.flush(10000);
SO.close();
}

public static function clearSOData(SOName:String):void
{
var SO:SharedObject = SharedObject.getLocal(SOName,"/");
SO.clear();
SO.close();
}

}
}

你可能感兴趣的:(xml)