在做网络连接选择中,我们往往是首先遍历用户手机内已存接入点,而后又用户选择适当接入点连接网络。由于每部手机内部每个已存接入点个数是不同的,那么就要求动态读取和现实已有接入点。所以可以通过继承 CAknEnumeratedTextPopupSettingItem,重载 CompleteConstructionL()方法,并在CAknSettingItemList中实现,
lib commdb.lib
AccessPointSettingItem.h
#include <aknsettingitemlist.h>
class CAccessPointSettingItem : public CAknEnumeratedTextPopupSettingItem
{
public:
CAccessPointSettingItem(TInt aIdentifier, TInt& aValue);
//load the Iap data
TInt LoadIapListL();
void LoadL();
private:
void CreateAndExecuteSettingPageL();
void CompleteConstructionL();
private:
TInt& iValue;
};
CAccessPointSettingItem.cpp
#include <commdb.h>
#include "Aaccesspointsettingitem.h"
CAccessPointSettingItem::CMobblerAccessPointSettingItem(TInt aIdentifier, TInt& aValue)
:CAknEnumeratedTextPopupSettingItem(aIdentifier, aValue), iValue(aValue)
{
}
//We must check whether the target value, iValue, is in the loaded array or not.
//If not, it definitely causes a panic -- "Setting Item Lis 6".
//In that case, we will choose the first item as the default one.
void CAccessPointSettingItem::LoadL()
{
CArrayPtr<CAknEnumeratedText>* texts(EnumeratedTextArray());
TInt selectedIndex(IndexFromValue(iValue));
if (selectedIndex < 0) // no match found.
{
if (texts->Count() > 0)
{
// choose the first item as default one.
CAknEnumeratedText* item(texts->At(0));
// reset external value to the default one.
SetExternalValue(item->EnumerationValue());
}
}
CAknEnumeratedTextPopupSettingItem::LoadL();
}
TInt CAccessPointSettingItem::LoadIapListL()
{
TInt firstIapId(KErrNotFound);
CArrayPtr<CAknEnumeratedText>* texts = this->EnumeratedTextArray();
// The destinations API does not exist
// so list all the connection points
// Add all the access points to the list
CCommsDatabase* commDb(CCommsDatabase::NewL(EDatabaseTypeIAP));
CleanupStack::PushL(commDb);
// Open IAP table
CCommsDbTableView* commView(commDb->OpenIAPTableViewMatchingBearerSetLC(
ECommDbBearerGPRS | ECommDbBearerWLAN,
ECommDbConnectionDirectionOutgoing));
// Search all IAPs
for (TInt error(commView->GotoFirstRecord()); error == KErrNone; error
= commView->GotoNextRecord())
{
TBuf<KCommsDbSvrMaxColumnNameLength> iapName;
TUint32 iapId;
commView->ReadTextL(TPtrC(COMMDB_NAME), iapName);
commView->ReadUintL(TPtrC(COMMDB_ID), iapId);
HBufC* text(iapName.AllocLC());
CAknEnumeratedText* enumText(new (ELeave) CAknEnumeratedText(iapId,
text));
CleanupStack::Pop(text);
CleanupStack::PushL(enumText);
EnumeratedTextArray()->AppendL(enumText);
CleanupStack::Pop(enumText);
if (firstIapId == KErrNotFound)
{
firstIapId = iapId;
}
}
CleanupStack::PopAndDestroy(commView);
CleanupStack::PopAndDestroy(commDb);
return firstIapId;
}
void CAccessPointSettingItem::CreateAndExecuteSettingPageL()
{
CAknSettingPage* dlg(CreateSettingPageL());
SetSettingPage(dlg);
SettingPage()->SetSettingPageObserver(this);
SettingPage()->SetSettingTextL(SettingName());
SettingPage()->ExecuteLD(CAknSettingPage::EUpdateWhenChanged);
SetSettingPage(0);
}
void CAccessPointSettingItem::CompleteConstructionL()
{
CAknEnumeratedTextPopupSettingItem::CompleteConstructionL();
}
rss中构建资源
RESOURCE AVKON_POPUP_SETTING_TEXTS r_popup_setting_texts_enum
{
setting_texts_resource = r_texts_enum;
}
RESOURCE ARRAY r_texts_enum
{
items =
{
AVKON_ENUMERATED_TEXT
{
value = 0;
text = "";
}
};
}
RESOURCE AVKON_SETTING_PAGE r_setting_page_enum
{
type = EAknCtPopupSettingList;
editor_resource_id = r_popup_setting_list_enum;
}
RESOURCE POPUP_SETTING_LIST r_popup_setting_list_enum
{
flags = 0;
}
在CCoeControl中调用
CAknSettingItemList* iItemList;
TInt tempIapId(0);
const TDesC& text(_L("AccessPoint"));
CAccessPointSettingItem* item(new (ELeave) CAccessPointSettingItem(0, tempIapId));
CleanupStack::PushL(item);
item->ConstructL(isNumberedStyle, iCount, text, icons, R_SETTING_PAGE_ENUM, -1, 0, R_POPUP_SETTING_TEXTS_ENUM);
CArrayPtr<CAknEnumeratedText>* textss(item->EnumeratedTextArray());
textss->ResetAndDestroy();
TInt firstIapId(item->LoadIapListL());
// Load list of IAPs
item->Load();
//>Append list
iItemList->SettingItemArray()->AppendL(item);
CleanupStack::Pop(item);