Symbian IPC发布及订阅机制提供了一种方便的单向进程通讯方式 (两边都发布都订阅的话就可以双向通讯了),对于一些简单的通知,使用起来非常方便.
这里简单记要一下使用方式:
订阅使用
1. 所需头文件
#include <e32property.h>
2. 所需 lib
euser.lib
3. 所需能力
[发布者 ]
Capability: WriteDeviceData if aCategory==KUidSystemCategoryValue.
WriteDeviceData if aCategory not equal to the current process's Secure ID
and aCategory<KUidSecurityThresholdCategoryValue.
意思是说发布系统范围的订阅id需要 WriteDeviceData
current process's Secure ID 就是uid3
于是发布的 id 等于自己的 uid3 的值不需要能力 , 其它值未测试
n95,6110c,e63上测试成功
[ 订阅者 ]
不需要
4. 使用示例 :
#define KPropertyID 0x20025204 // 根据自己的 uid3 值改
#define KPropertyKey 2
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[ 发布者 ]
TInt ret = RProperty::Define(KPropertyID, KPropertyKey,RProperty::EInt); // 第三个参数表示订阅类型,这里是整型还可以字符串等
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
ret = RProperty::Set(KPropertyID, KPropertyKey, 1);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[ 订阅者 ]
使用 CSubscriber , this 要继承观察者 MSubscriberObserver
iSubscriber = CSubscriber::NewL(KPropertyID, KPropertyKey,*this);
iSubscriber->Start();
收到订阅通知时会有回调
IntPropertyUpdatedL 接口
CSubscriber 类的代码:
/*
* Subscriber.h
*
* Created on: 2010-4-24
* Author: Huangdingwu
*/
#ifndef SUBSCRIBER_H_
#define SUBSCRIBER_H_
// INCLUDES
#include <e32base.h>
#include <e32property.h>
class MSubscriberObserver
{
public:
virtual void IntPropertyUpdatedL(TInt aValue) = 0;
};
class CSubscriber : public CActive
{
public:
/*
* aUid 订阅id
* aKey 订阅id 下所属的一个键值
*
* */
static CSubscriber* NewL( const TUid aUid, const TUint32 aKey,MSubscriberObserver& aNotifier );
virtual ~CSubscriber();
void Start();
private:
CSubscriber( const TUid aUid, const TUint32 aKey,MSubscriberObserver& aNotifier );
void ConstructL();
void RunL();
void DoCancel();
private:
RProperty iProperty;
const TUid iUid;
const TUint32 iKey;
MSubscriberObserver& iObserver;
};
#endif /* SUBSCRIBER_H_ */
/*
* Subscriber.cpp
*
* Created on: 2010-4-24
* Author: Huangdingwu
*/
#include "Subscriber.h"
CSubscriber::CSubscriber( const TUid aUid, const TUint32 aKey,MSubscriberObserver& aObserver )
: CActive(EPriorityStandard), iUid( aUid ),iKey( aKey ), iObserver( aObserver)
{
}
CSubscriber* CSubscriber::NewL( const TUid aUid, const TUint32 aKey,MSubscriberObserver& aObserver )
{
CSubscriber* self = new(ELeave) CSubscriber( aUid, aKey, aObserver );
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
void CSubscriber::ConstructL()
{
User::LeaveIfError( iProperty.Attach( iUid, iKey ) );
CActiveScheduler::Add(this);
}
CSubscriber::~CSubscriber()
{
Cancel();
iProperty.Close();
}
void CSubscriber::DoCancel()
{
iProperty.Cancel();
}
void CSubscriber::RunL()
{
TInt intValue;
if( iProperty.Get( intValue ) != KErrNotFound )
{
iObserver.IntPropertyUpdatedL(intValue);
}
// resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
}
void CSubscriber::Start()
{
Cancel();
iProperty.Subscribe( iStatus );
SetActive();
}
// end file
备注:
发布者或订阅者线程都可以调用Rproperty::Define() 来定义属性。用户定义的属性将一直保存到操作系统重启或被删除
删除方法:
Rproperty::Delete()
另外,在程序退出时同步发布一个值,订阅者会收不到
上面的代码基本来自 Forum Nokia Wiki , 看得不明白的可以看原文:
http://wiki.forum.nokia.com/index.php/CS000909_-_Publish_and_Subscribe:_Using_RProperty_for_subscribing