首选项:首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型以及这3种类型的数组类型。 通常用于首次启动后弹出隐私协议弹窗,然后记录后,下次启动不在弹窗。 
 
 
import  SpUtils from  './utils/SpUtils' @ Entry @ Component { @ State :  string  =  "" onPageShow ( ) :  void  { this . msg =  "当前数据"  +  SpUtils. isFirst ( ) } build ( )  { Column ( )  { Button ( "保存数据" ) . id ( 'PagePreferenceHelloWorld' ) . margin ( {  top:  20  } ) . fontSize ( 20 ) . fontWeight ( FontWeight. Bold) . onClick ( ( )  =>  { SpUtils. setFirst ( ) this . msg =  "保存数据" } ) Button ( "获取数据" ) . id ( 'PagePreferenceHelloWorld' ) . margin ( {  top:  20  } ) . fontSize ( 20 ) . fontWeight ( FontWeight. Bold) . onClick ( ( )  =>  { this . msg =  "获取数据"  +  SpUtils. isFirst ( ) } ) Button ( "清空数据" ) . id ( 'PagePreferenceHelloWorld' ) . margin ( {  top:  20  } ) . fontSize ( 20 ) . fontWeight ( FontWeight. Bold) . onClick ( ( )  =>  { this . msg =  "清空数据" SpUtils. clearData ( ) } ) Text ( this . msg) . fontSize ( 25 ) . margin ( {  top:  20  } ) } . height ( '100%' ) . width ( '100%' ) } 
} 
import  preferences from  '@ohos.data.preferences' export  default  class  SpUtils  { private  static  KEY_DATA  =  "key_data" private  static  SP_NAME  =  "data" private  static  sp:  preferences. Preferences =  preferences. getPreferencesSync ( getContext ( this ) ,  { name:  SpUtils. SP_NAME } ) static  setFirst ( )  { SpUtils. sp. putSync ( SpUtils. KEY_DATA ,  false ) SpUtils. sp. flush ( ) } static  isFirst ( ) :  boolean  { let  data =  SpUtils. sp. getSync ( SpUtils. KEY_DATA ,  true )  as  boolean return  data} static  clearData ( )  { SpUtils. sp. clearSync ( ) SpUtils. sp. flush ( ) } 
}