Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Notification of application change

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: mayankkedia (13 May 2009)
Last edited: hamishwillee (30 May 2013)


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

Link

Getting notifications of focus change and launch of other apps using Symbian C++

How to determine if a certain application is in the foreground?

This page was last modified on 30 May 2013, at 04:35.
18 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×