How to detect internet disconnection from application using red key
Article Metadata
Contents |
Purpose
Whenever an application has an active internet connection and if the user terminates the connection by long pressing the red/end call key, there is no way to capture the choice made by user in the "end data package query" and do the required cleanup. A workaround to this problem is the use of RConnectionMonitor & MConnectionMonitorObserver classes.
Headers to be included:
#include <rconnmon.h>Link against this libraries:
LIBRARY ConnMon.lib
The following steps must be implemented
1. Derive your class from MConnectionMonitorObserver and give implementation for virtual function EventL(const CConnMonEventBase &aConnMonEvent)
2.Have a RConnectionMonitor class object in your class and whenever you initiate an internet connection connect your class to Connection Monitor Server using the function:ConnectL()
3.After connecting ,register your class to catch connection monitor events using the function: NotifyEventL(MConnectionMonitorObserver &aObserver)
4.Whenever there is a change in the status of the connection ,the TConnMonEvent events generated by the connection monitor are handled by the implemented EventL() function of the class.
Example Code
Header File
class CExampleClass : public CCoeControl,
public MConnectionMonitorObserver
{
public:
/*
Some Functions
*/
//From MConnectionMonitorObserver
void EventL (const CConnMonEventBase &aConnMonEvent);
private: // Data
RConnectionMonitor iConnectionMonitor;
};
Source File
void CExampleClass::ConstructL( const TRect& aRect )
{
/*
Some Code
*/
User::LeaveIfError( iConnectionMonitor.ConnectL() ); //Connecting with server
User::LeaveIfError( iConnectionMonitor.NotifyEventL( *this )); //Registering
//with Server.
// Set the main window size
SetRect( aRect );
// Activate the window, which makes it ready to be drawn
ActivateL();
}
void CExampleClass::EventL (const CConnMonEventBase &aConnMonEvent)
{
switch ( aConnMonEvent.EventType() )
{
case EConnMonDeleteConnection:
{
// Some Implementation
break;
}
case EConnMonNetworkStatusChange:
{
// Some Implementation
break;
}
case EConnMonConnectionStatusChange:
{
// Some Implementation
break;
}
case EConnMonBearerChange:
{
// Some Implementation
break;
}
default:
break;
}
}


06 Sep
2009
This article provides a good way to alert the user when a connection is active and the user tries to close the application by using red key. The code snippet provides method to detect the red key. After implementing this the developer can do any action say ask the user if he really wants to close the application.