The following example shows how to get notified of an application change, and once the change has been observed, how to get the application name and other details of the application that has come on top.
This kind of code is needed when we are writing some logic in the code which would do different things based on what application is in the foreground. For instance if standby application is on top show a global note to the user or something like that.
Header file required:
#include <cntdb.h>
Add following line to your .mmp file.
LIBRARY ws32.lib apgrfx.lib
Add following lines in AppChangeNotifier.h file.
#include <W32STD.H>
class MAppChangeObserver
{
public:
/**
* Virtual function, implemented elsewhere
*/
virtual void NotifyAppChangeL(TBool aStandbyInFront) = 0;
};
class CAppChangeNotifier: public CActive
{
private:
CAppChangeNotifier(MAppChangeObserver& aAppChangeObserver);
void ConstructL();
public:
static CAppChangeNotifier* NewL(MAppChangeObserver& aAppChangeObserver);
~CAppChangeNotifier();
void Start();
private:
// From CActive
void DoCancel();
void RunL();
TInt RunError(TInt aError);
private:
RWsSession iWs;
RWindowGroup iWindowGroup;
MAppChangeObserver& iAppChangeObserver;
};
Add following lines in AppChangeNotifier.cpp file.
#include <APGWGNAM.H>
// User Include
#include "AppChangeNotifier.h"
CAppChangeNotifier::CAppChangeNotifier(MAppChangeObserver& aAppChangeObserver)
:CActive(EPriorityIdle), iAppChangeObserver(aAppChangeObserver), iTodayInForeground(EFalse)
{
}
CAppChangeNotifier* CAppChangeNotifier::NewL(MAppChangeObserver& aAppChangeObserver)
{
CAppChangeNotifier* self = new (ELeave)CAppChangeNotifier(aAppChangeObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
void CAppChangeNotifier::ConstructL()
{
CActiveScheduler::Add(this);
User::LeaveIfError(iWs.Connect());
// Windows Group
iWindowGroup = RWindowGroup(iWs);
User::LeaveIfError(iWindowGroup.Construct(reinterpret_cast<TUint32> (&iWindowGroup), EFalse));
iWindowGroup.SetOrdinalPosition(0, ECoeWinPriorityNeverAtFront);
iWindowGroup.EnableReceiptOfFocus(EFalse);
User::LeaveIfError(iWindowGroup.EnableFocusChangeEvents());
}
CAppChangeNotifier::~CAppChangeNotifier()
{
Cancel();
iWindowGroup.Close();
iWs.Close();
}
void CAppChangeNotifier::Start()
{
iWs.EventReady(&iStatus);
SetActive();
}
void CAppChangeNotifier::DoCancel()
{
iWs.EventReadyCancel();
}
void CAppChangeNotifier::RunL()
{
// Get the event from the window server session iWs
TWsEvent wsEvent;
iWs.GetEvent(wsEvent);
// Get the event type: types are defined in TEventCode
switch (wsEvent.Type())
{
// Window-group related event types
case EEventFocusGroupChanged:
{
TInt wgid = iWs.GetFocusWindowGroup();
CApaWindowGroupName* gn = CApaWindowGroupName::NewLC(iWs, wgid);
TBuf<80> appName = gn->Caption();
_LIT(KStandby, "Standby mode");
if(appName.Compare(KStandby) == 0)
{
iAppChangeObserver.NotifyAppChangeL(ETrue);
}
else
{
iAppChangeObserver.NotifyAppChangeL(EFalse);
}
CleanupStack::PopAndDestroy(gn);
}
break;
}
Start();
}
TInt CAppChangeNotifier::RunError(TInt aError)
{
return KErrNone;
}
Add following lines in YourClass.h file.
class CYourClass:public CBase, public MAppChangeObserver
{
public:
void NotifyAppChangeL(TBool aStandbyInFront);
public:
CAppChangeNotifier* iAppChangeNotifier ;
};
Add following lines in YourClass.cpp file.
// Register for Ws Changes to be notified
iAppChangeNotifier = CAppChangeNotifier::NewL(*this);
void CYourClass::NotifyAppChangeL(TBool aStandbyInFront)
{
// Do something
}
Added by - Mayank on 13/05/2009
TSS000754 - Getting notifications of focus change and launching of other applications
How to determine if a certain application is in the foreground?
No related wiki articles found