×
Namespaces

Variants
Actions

存储应用程序的设置

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 01 Jun 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000967


  • 开发伙伴平台:

S60 3rd Edition, MR


  • 详细描述

下列程序显示了如何设置应用程序的设置数据,并将其存储到程序的私有目录中。


Contents

MMP文件

需要下列能力和链接库

CAPABILITY      NONE
LIBRARY efsrv.lib
LIBRARY estor.lib


头文件CSettingsData.h

利用流stream存储和读取设置数据的类

#include <e32base.h>
class CSettingsData : public CBase
{
public:
static CSettingsData* NewL();
static CSettingsData* NewLC();
virtual ~CSettingsData();
 
// Loads itself from stream.
void LoadL(RReadStream& aStream);
 
// Saves itself to stream.
void SaveL(RWriteStream& aStream) const;
 
// Using default values (e.g. in first time)
void SetDefaultValues();
 
inline TBuf<100>& Text() { return iText; }
inline TInt& Enumeration() { return iEnumeration; }
 
private:
CSettingsData();
void ConstructL();
 
public:
TBuf<100> iText; //Data for text field
TInt iEnumeration; // Data for enumerated text field


源文件CSettingsData.cpp

CSettingsData *CSettingsData::NewL()
{
CSettingsData *self = CSettingsData::NewLC();
CleanupStack::Pop(self);
return self;
}
 
CSettingsData *CSettingsData::NewLC()
{
CSettingsData *self = new (ELeave) CSettingsData();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
CSettingsData::~CSettingsData()
{
}
 
CSettingsData::CSettingsData()
{
}
 
void CSettingsData::SetDefaultValues()
{
// TODO: Set default values of your setting
iEnumeration = 1000;
}
 
void CSettingsData::ConstructL()
{
}
 
// Reading setting data from stream
void CSettingsData::LoadL(RReadStream& aStream)
{
aStream >> iText;
iEnumeration = aStream.ReadInt32L();
}
 
// Storing setting data into stream
void CSettingsData::SaveL(RWriteStream& aStream) const
{
aStream << iText;
aStream.WriteInt32L(iEnumeration);
}


头文件CMyAppUi.h

应用程序类,可以在构造时读取数据,在里离开时存储数据

#include <aknviewappui.h>
#include "CSettingsData.h"
 
class CMyAppUi : public CAknViewAppUi
{
public:
void ConstructL();
CSettingExampleAppUi();
~CSettingExampleAppUi();
 
public: // from CEikAppUi
void HandleCommandL(TInt aCommand);
 
private:
void InternalizeSettingsDataL();
void ExternalizeSettingsDataL() const;
 
private:
CSettingsData* iData; // Pointer to settings data
TFileName iSettingsFile;
};


源文件CMyAppUi.cpp

void CMyAppUi::ConstructL()
{
BaseConstructL(EAknEnableSkin);
 
// Create private path
RFs& fsSession = iEikonEnv->FsSession();
User::LeaveIfError(fsSession.CreatePrivatePath( EDriveC ) );
User::LeaveIfError(fsSession.PrivatePath(iSettingsFile));
iSettingsFile += KSettingsFile;
 
// Construct the data object the settings list will use
iData = CSettingsData::NewL();
// Read settings from stream
InternalizeSettingsDataL();
 
// TODO: Construct and activate the app view as well
}
 
// Handle menu commands
void CMyAppUi::HandleCommandL(TInt aCommand)
{
switch(aCommand)
{
case EEikCmdExit:
case EAknSoftkeyExit:
{
// Store setting data on application exit
ExternalizeSettingsDataL();
Exit();
break;
}
default:
break;
}
}
 
// Load settings
void CMyAppUi::InternalizeSettingsDataL()
{
RFs& fs = iEikonEnv->FsSession();
RFileReadStream readStream;
TInt error = readStream.Open(fs, iSettingsFile, EFileRead);
TInt internalizationError = KErrNone;
// If file existed, try to read settings.
if (error == KErrNone)
{
TRAP(internalizationError, iData->LoadL(readStream);)
}
else
{
// Use default values in first time when no
// setting file exists
iData->SetDefaultValues();
}
readStream.Release();
 
// Reading failed, settings file might be corrupted.
if (internalizationError != KErrNone)
{
User::LeaveIfError(fs.Delete(iSettingsFile));
}
}
 
// Save settings
void CMyAppUi::ExternalizeSettingsDataL() const
{
RFs& fs = iEikonEnv->FsSession();
RFileWriteStream writeStream;
TInt error = writeStream.Open(fs, iSettingsFile, EFileWrite);
// Setting file did not exist, create one.
if (error != KErrNone)
{
User::LeaveIfError(writeStream.Create(fs, iSettingsFile, EFileWrite));
}
writeStream.PushL();
iData->SaveL(writeStream);
writeStream.CommitL();
writeStream.Pop();
writeStream.Release();
}


流操作

一些数据通过流方式的读取

// String
TBuf<100> iText;
aStream >> iText;
 
// Integer
TInt iVolume;
iVolume = aStream.ReadInt32L();
 
// Boolean
TBool iBinary;
iBinary = aStream.ReadInt16L();
 
// Ip address
TInetAddr iIpAddress;
iIpAddress.SetAddress(aStream.ReadUint32L());
 
// Time
TTime iTime;
TInt64 time;
aStream >> time;
iTime = TTime(time);
This page was last modified on 15 December 2011, at 09:30.
29 page views in the last 30 days.