RChangeNotifier和CEnvironmentChangeNotifier 的使用

 RChangeNotifier
CEnvironmentChangeNotifier
可以用來監視是否到達了午夜,位置改變,有線程死掉了,系統時間被修改,電量變化等事件,有因內存不足引起的分配失敗等事件
 RChangeNotifier 和 CEnvironmentChangeNotifier 的關係類似於RTimer和CTimer的關係。
RChangeNotifier 是阻塞的,使用的時候最好放在一個CActive裏面。用法靈活。
CEnvironmentChangeNotifier  本身就是個CActive可以直接使用。使用方便

 

使用方法如下:
RChangeNotifier:

 

  1. {
  2. ...
  3. RChangeNotifier the_notifier;
  4. TRequestStatus the_status;
  5. ...
  6. the_notifier.Create();
  7. the_notifier.Logon(the_status);
  8. User::WaitForRequest(the_status);
  9. ...
  10. ...// prepare for a long wait
  11. ...
  12. TInt changes = the_status.Int();
  13. if (changes & EChangesSystemTime)
  14.     {
  15.     // handle a change to system time
  16.     }
  17. if (changes & EChanges EChangesLocale)
  18.     {
  19.     // handle a change to locale
  20.     }
  21. ...
  22. the_notifier.Close();
  23. ...
  24. }

CEnvironmentChangeNotifier

  1. void CExampleEnvChangeNotifier::ConstructL()
  2.       {
  3.       iCallBack = new (ELeave)TCallBack(CallBackFunction, this);
  4.       iChangeNotifier =
  5.            CEnvironmentChangeNotifier::NewL(0, *iCallBack);
  6.       iChangeNotifier->Start();
  7.       }
  8. CExampleEnvChangeNotifier::~CMyEnvChangeNotifier()
  9.       {
  10.       iChangeNotifier->Cancel();
  11.      delete iChangeNotifier;
  12.      delete iCallBack;
  13.       }
  14. TInt CExampleEnvChangeNotifier::CallBackFunction(TAny* aFunction)
  15.       {
  16.       return ((CEventsEnvChangeNotifier*)aFunction)->ChangeL();
  17.       }
  18. TInt CExampleEnvChangeNotifier::ChangeL()
  19.      {
  20.       TInt change = iChangeNotifier->Change();
  21. //當iChangeNotifier 啓動的時候繪產生一次回掉此時change爲127
  22.     if (change==127)
  23.     {
  24.         return -1;
  25.     }
  26.       if (change & EChangesLocale)
  27.              {
  28.              // Locale change, do something 
  29.              }
  30.       if (change & EChangesMidnightCrossover)
  31.              {
  32.              // Midnight crossover, do something 
  33.              }
  34.       if (change & EChangesThreadDeath)
  35.              {
  36.               // Thread death, do something 
  37.              }
  38.       if (change & EChangesPowerStatus)
  39.              {
  40.              // Power status change, do something 
  41.               }
  42.       if (change & EChangesSystemTime)
  43.               {
  44.               // System status change, do something 
  45.               }
  46.       return 1;
  47.        }

EChangesLocale
The system locale has changed.


Typically this event occurs as a result of a call to TLocale::Set().

 

位置改變
 
EChangesMidnightCrossover
The system time has passed midnight.

 

到達午夜
 
EChangesThreadDeath
A thread has died.


This event is reported when any thread in the system dies.

 

有線程死掉了
 
EChangesPowerStatus
The status of the power supply has changed.

 

電量變化
 
EChangesSystemTime
The system time has changed.

 

系統時間被修改


EChangesFreeMemory
The free memory level has crossed a specified threshold value.

 


 
EChangesOutOfMemory
A memory allocation has failed due to insufficient free memory.

 

有因內存不足引起的分配失敗
 

我專門寫了個CEnvironmentChangeNotifier 例子,在e51上可以正常工作了。http://download.csdn.net/source/710390
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章