关于Inactivity()函数配合活动对象,获取手机不活动时间,实现特定功能

 Inactivity()函数官方SDK的解释为

 

Requests an event if no activity occurs within the specified interval.

 

所以可以使用Inactivity()配合AO获取手机不活动的时间,实现某些特定功能

 

先看Start()函数的实现

void CActiveBall::Start(TTimeIntervalSeconds aTimeout)
{
 Cancel();
 iTimeout = aTimeout;
 iInactivity = ETrue;
 Inactivity(iTimeout);
}

void CActiveBall::Stop()
{
 Cancel();
}

 

当然数据的初始化是必不可少的,下面一并贴出
// constructor
CActiveBall::CActiveBall(MObserver& aObserver)
:CTimer(EPriorityStandard), iObserver(aObserver)
{
 // No implementation required
}

// EPOC default constructor can leave.
void CActiveBall::ConstructL()
{
 //this is explicitly decareded in the CTimer document
 CTimer::ConstructL();

 // The following line is also essential!
 CActiveScheduler::Add(this);
}


void CActiveBall::RunL()
{
 if(iStatus!=KErrNone)
 {
  return;
 }

 if (iInactivity)
 {
  TTimeIntervalSeconds inactivity = User::InactivityTime().Int();
  if (inactivity >= iTimeout)
  {
   iObserver.StartDisPlay();    //设置的获取到的手机不活动时间已到,调用某设置

   if(!IsActive())
   {
    Inactivity(0);
    iInactivity = EFalse;
   }
  }
  else
  {
   Inactivity(iTimeout);
  }
 }
 else
 {
  iObserver.CloseDisPlay();     //手机处于活动状态,调用某设置 
  if (!IsActive())
  {
   Inactivity(iTimeout);
   iInactivity = ETrue;
  }
 }

 if (!IsActive())
 {
  SetActive();
 }
}

 

好了呵呵。

以上代码在3rd MR ,3rd fp1上测试可用

获取系统的睡眠状态,可以使用 Windows API 的 PowerGetActiveScheme 函数和 ReadPwrScheme 函数。以下是一个简单的示例代码: ```c++ #include <windows.h> #include <powrprof.h> #include <iostream> #pragma comment(lib, "PowrProf.lib") int main() { // 获取当前电源计划的标识符 GUID activeScheme; if (PowerGetActiveScheme(NULL, &activeScheme) != ERROR_SUCCESS) { std::cerr << "Failed to get active power scheme" << std::endl; return 1; } // 读取当前电源计划的睡眠设置 DWORD len = 0; if (ReadPwrScheme(&activeScheme, NULL, NULL, &len) != ERROR_SUCCESS) { std::cerr << "Failed to get power scheme settings length" << std::endl; return 1; } BYTE* buffer = new BYTE[len]; if (ReadPwrScheme(&activeScheme, NULL, buffer, &len) != ERROR_SUCCESS) { std::cerr << "Failed to read power scheme settings" << std::endl; delete[] buffer; return 1; } // 解析睡眠设置 DWORD sleepTimeout = 0; DWORD sleepType = 0; DWORD* ptr = (DWORD*)buffer; for (int i = 0; i < len / sizeof(DWORD); i += 2) { if (*ptr == 4 && *(ptr + 1) == 3) // 确保是睡眠设置 { sleepTimeout = *(ptr + 2); sleepType = *(ptr + 3); break; } ptr += 2; } delete[] buffer; // 输出睡眠设置 switch (sleepType) { case 0: std::cout << "Sleep is not available" << std::endl; break; case 1: std::cout << "Sleep after " << sleepTimeout << " seconds of inactivity" << std::endl; break; case 2: std::cout << "Sleep after " << sleepTimeout << " minutes of inactivity" << std::endl; break; case 3: std::cout << "Sleep after " << sleepTimeout << " hours of inactivity" << std::endl; break; default: std::cout << "Unknown sleep type" << std::endl; break; } return 0; } ``` 该程序获取当前电源计划的标识符,然后读取该电源计划的睡眠设置,并解析出睡眠延时和睡眠类型。最后输出睡眠设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值