×
Namespaces

Variants
Actions

使用CRepository监听情景模式变化

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

测试基于
设备:: N73, N95 8G, N78, N97mini

文章
Cxt_programmer 在 30 Aug 2010 创建
最后由 hamishwillee 在 15 Dec 2011 编辑


最近遇到一个关于监听情景模式改变的case,我的第一反应是使用plugin api来实现:

如何获取情景模式改变通知

虽然就我目前的测试结果,这个plugin api在3rd mr至5th版本的手机中都可正常使用,但Nokia官方并没有保证plugin api在各个版本之间的二进制兼容
通过翻阅SDK头文件,我发现了一个更通用的方法来监听情景模式的改变。从3rd mr开始,SDK中就提供了一个名为profileenginesdkcrkeys.h的头文件,里面提供了一些情景模式相关的UID,于是我们可以使用CRepository来监听情景模式改变,自己实现plug in api的功能。

实现方法如下:
ProfileChangeNotifier.h

/*
============================================================================
Name  : ProfileChangeNotifier.h
Author  : Cxt_programmer
Version  : 1.0
Copyright  : Your copyright notice
Description  : CProfileChangeNotifier declaration
============================================================================
*/

 
#ifndef PROFILECHANGENOTIFIER_H
#define PROFILECHANGENOTIFIER_H
 
#include <e32base.h> // For CActive, link against: euser.lib
#include <e32std.h> // For RTimer, link against: euser.lib
 
class CRepository;
 
class CProfileChangeNotifier : public CActive
{
public:
// Cancel and destroy
~CProfileChangeNotifier();
 
// Two-phased constructor.
static CProfileChangeNotifier* NewL();
 
// Two-phased constructor.
static CProfileChangeNotifier* NewLC();
 
public:
// New functions
// Function for making the initial request
void StartL();
 
private:
// C++ constructor
CProfileChangeNotifier();
 
// Second-phase constructor
void ConstructL();
 
private:
// From CActive
// Handle completion
void RunL();
 
// How to cancel me
void DoCancel();
 
// Override to handle leaves from RunL(). Default implementation causes
// the active scheduler to panic.
TInt RunError(TInt aError);
 
private:
CRepository* iRepository;
};
 
#endif // PROFILECHANGENOTIFIER_H

ProfileChangeNotifier.cpp实现如下:

#include "ProfileChangeNotifier.h"
 
// Added by Cxt
#include <centralrepository.h>
#include <ProfileEngineSDKCRKeys.h>
#include <EIKENV.H>
// end of Cxt
 
CProfileChangeNotifier::CProfileChangeNotifier() :
CActive(EPriorityStandard) // Standard priority
{
}
 
CProfileChangeNotifier* CProfileChangeNotifier::NewLC()
{
CProfileChangeNotifier* self = new (ELeave) CProfileChangeNotifier();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
CProfileChangeNotifier* CProfileChangeNotifier::NewL()
{
CProfileChangeNotifier* self = CProfileChangeNotifier::NewLC();
CleanupStack::Pop(); // self;
return self;
}
 
void CProfileChangeNotifier::ConstructL()
{
// Added by Cxt
iRepository = CRepository::NewL( KCRUidProfileEngine );
// end of Cxt
CActiveScheduler::Add(this); // Add to scheduler
}
 
CProfileChangeNotifier::~CProfileChangeNotifier()
{
Cancel(); // Cancel any request, if outstanding
 
// Delete instance variables if any
// Added by Cxt
delete iRepository;
// end of Cxt
}
 
void CProfileChangeNotifier::DoCancel()
{
// Added by Cxt
iRepository->NotifyCancel( KProEngActiveProfile );
// end of Cxt
}
 
void CProfileChangeNotifier::StartL()
{
Cancel(); // Cancel any request, just to be sure
 
// Added by Cxt
iRepository->NotifyRequest( KProEngActiveProfile, iStatus );
SetActive();
// end of Cxt
}
 
void CProfileChangeNotifier::RunL()
{
// Added by Cxt
TInt currentProfile;
 
User::LeaveIfError( iStatus.Int() );
 
// 情景模式改变后,RunL回调,此时获取一下当前情景模式(并进行相应的处理)
User::LeaveIfError( iRepository->Get( KProEngActiveProfile, currentProfile ) );
 
TBuf<128> msg( _L("Current Profile is: ") );
msg.AppendNum( currentProfile );
 
CEikonEnv::InfoWinL( _L(" Profile was changed."), msg );
 
StartL();
// end of Cxt
}
 
TInt CProfileChangeNotifier::RunError(TInt aError)
{
return aError;
}


测试机型:N73、N95 8G、N78、N97mini。

This page was last modified on 15 December 2011, at 08:17.
51 page views in the last 30 days.