×
Namespaces

Variants
Actions

检测系统环境的变化

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

兼容于
文章
翻译:
hoolee
最后由 hamishwillee 在 08 Aug 2012 编辑
  • 详细描述

这里提供检测系统环境变化的方法,如系统时间改变,线程死亡或区域改变。

我们使用CEnvironmentChangeNotifier来完成上述需求,这是一个活动对象,主要用回调函数TCallBack来完成事件的通知。

  • 解决方案

需要的头文件和链接库

#include <bacntf.h> // Link against bafl.lib
<code>
 
<code cpp>
// NotifyChange is a static member function declared as follows:
// static TInt NotifyChange( TAny* aObject );
TCallBack envChangeCallback( NotifyChange, this );
iEnvNotifier = CEnvironmentChangeNotifier::NewL( CActive::EPriorityHigh, envChangeCallback );
// Issues a request for change events. When a change events occurs, the
// callback function is called. Note that after the first call to this
// function, the callback function is called immediately. iEnvNotifier->Start();

通过TChanges来获取事件通知

TInt CMyEnvironMentChangeNotifier::NotifyChange( TAny* aObject )   
{
CMyEnvironMentChangeNotifier* thisPtr = static_cast<CMyEnvironMentChangeNotifier*>( aObject );
// Retrieve the changes in a bit field
const TInt changes( thisPtr->iEnvNotifier->Change() );
if( changes & EChangesLocale )
{
// System locale changed
}
if( changes & EChangesMidnightCrossover )
{
// Midnight crossover
}
if( changes & EChangesThreadDeath )
{
// Thread death
}
if( changes & EChangesPowerStatus )
{
// Power status changed
}
if( changes & EChangesSystemTime )
{
// System time changed
}
if( changes & EChangesFreeMemory )
{
// Free memory changed
}
if( changes & EChangesOutOfMemory )
{
// Out of memory
}
}
This page was last modified on 8 August 2012, at 05:10.
57 page views in the last 30 days.
×