RProperty 1

Pulish及Subscribe:使用RProperty进行subscribe操作

详细描述

下列代码片段演示了如何使用RProperty 来订阅用户使用Symbian IPC发布及订阅机制定义的属性。下列步骤用来订阅并接收属性更新状态。

  • 使用RProperty::Attach()标注需要的属性,然后调用RProperty::Subscribe()订阅
  • 通过TRequestStatus信号通知RProperty::Subscribe()完成。
  • 调用RProperty::Get()获得更新后的属性值
  • 通过RProperty::Subscribe重新提交请求来监听属性变化
  • 最终调用RProperty::Cancel来取消订阅,以及通过RProperty::Close

Publish及Subscribe:使用Rproperty进行publish操作 中的代码片段演示了用户定义属性如何被发布,这些属性定义将通过ExampleProperties.h在发布者和订阅者之间共享。

#ifndef EXAMPLEPROPERTIES_H_
#define EXAMPLEPROPERTIES_H_
 
const Tint KMaxStrLen = 10;
const Tuid KExampleProperty = {0xED1917A8};
enum TExamplePropertyKeys { EIntProperty, EStrProperty };
 
#endif /*EXAMPLEPROPERTIES_H_*/

This snippet can be self-signed.

MMP文件

需要下列链接库

LIBRARY euser.lib

前言

发布者或订阅者线程都可以调用Rproperty::Define() 来定义属性。用户定义的属性将一直保存到操作系统重启或被删除。

Tint ret=Rproperty::Define(KExampleProperty,EIntProperty,Rproperty::Eint);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
 
ret= Rproperty::Define(KExampleProperty,EStrProperty,Rproperty::EByteArray,KMaxStrLen);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}

头文件

#ifndef EXAMPLESUBSCRIBER_H_
#define EXAMPLESUBSCRIBER_H_
 
// INCLUDES
#include <e32base.h>
#include <e32property.h>
 
#include "exampleproperties.h"
 
 
class MExampleSubscriberObserver
{
public:
virtual void IntPropertyUpdatedL(Tint aValue) = 0;
virtual void StrPropertyUpdatedL(Tdes& aValue) = 0;
};
 
 
class CExampleSubscriber : public Cactive
{
enum {Epriority=0};
public:
static CExampleSubscriber* NewL( const Tuid aUid,
const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
virtual ~CExampleSubscriber();
 
private:
CExampleSubscriber( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
void ConstructL();
void RunL();
void DoCancel();
private:
Rproperty iProperty;
const Tuid iUid;
const TUint32 iKey;
MExampleSubscriberObserver& iObserver;
};
 
#endif /*EXAMPLESUBSCRIBER_H_*/

源文件

#include "ExampleSubscriber.h"
 
CExampleSubscriber::CExampleSubscriber(
const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
: Cactive(Epriority), iUid( aUid ),
iKey( aKey ), iObserver( aObserver)
{
}
 
CExampleSubscriber* CExampleSubscriber::NewL( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
{
CExampleSubscriber* self=
new(Eleave) CExampleSubscriber( aUid, aKey, aObserver );
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
void CExampleSubscriber::ConstructL()
{
User::LeaveIfError( iProperty.Attach( iUid, iKey ) );
CActiveScheduler::Add(this);
// initial subscription and process current property value
RunL();
}
 
CExampleSubscriber::~CExampleSubscriber()
{
Cancel();
iProperty.Close();
}
 
void CExampleSubscriber::DoCancel()
{
iProperty.Cancel();
}
 
void CExampleSubscriber::RunL()
{
// resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
 
Tint intValue;
Tbuf<KMaxStrLen> strValue;
 
switch( iKey )
{
case EIntProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EIntProperty,intValue);
if( iProperty.Get( intValue ) == KErrNotFound )
{
// Int property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.IntPropertyUpdatedL(intValue);
}
break;
case EStrProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EStrProperty,strValue);
if( iProperty.Get( strValue ) == KErrNotFound )
{
// Str property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.StrPropertyUpdatedL(strValue);
}
break;
 
default:
break;
}
}
 
// End of File

使用CExampleSubscriber类

在头文件里:

#include "examplesubscriber.h"
 
class CPropertyExampleAppUi : public CAknAppUi, MExampleSubscriberObserver
{
//…
public:
void IntPropertyUpdatedL(Tint aValue);
void StrPropertyUpdatedL(Tdes& aValue);
//…
CExampleSubscriber* iIntSubscriber;
CExampleSubscriber* iStrSubscriber;
};

在源文件中:

void CPropertyExampleAppUi::ConstructL()
{
//…
iIntSubscriber = CExampleSubscriber::NewL(KExampleProperty,EIntProperty,*this);
iStrSubscriber = CExampleSubscriber::NewL(KExampleProperty,EStrProperty,*this);
}
 
CPropertyExampleAppUi::~CPropertyExampleAppUi()
{
//…
delete iIntSubscriber;
delete iStrSubscriber;
}
 
void CPropertyExampleAppUi::IntPropertyUpdatedL(Tint aValue)
{
//do something with the updated property
}
 
void CPropertyExampleAppUi::StrPropertyUpdatedL(Tdes& aValue)
{
//do something with the updated property
}

后记

使用Rproperty来订阅两个示例属性

如果订阅线程定义了属性,那么也可以使用Rproperty::Delete() 来删除掉。

Tint err(KErrNone);
 
err = Rproperty::Delete(KExampleProperty,EIntProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}
err = Rproperty::Delete(KExampleProperty,EStrProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}

当属性都被删除掉后,任何未完成的订阅将被告知KErrNotFound完成 (来自:Forum Nokia Wiki)

 

详细描述

下列代码片段演示了如何使用RProperty 来订阅用户使用Symbian IPC发布及订阅机制定义的属性。下列步骤用来订阅并接收属性更新状态。

  • 使用RProperty::Attach()标注需要的属性,然后调用RProperty::Subscribe()订阅
  • 通过TRequestStatus信号通知RProperty::Subscribe()完成。
  • 调用RProperty::Get()获得更新后的属性值
  • 通过RProperty::Subscribe重新提交请求来监听属性变化
  • 最终调用RProperty::Cancel来取消订阅,以及通过RProperty::Close

Publish及Subscribe:使用Rproperty进行publish操作 中的代码片段演示了用户定义属性如何被发布,这些属性定义将通过ExampleProperties.h在发布者和订阅者之间共享。

#ifndef EXAMPLEPROPERTIES_H_
#define EXAMPLEPROPERTIES_H_
 
const Tint KMaxStrLen = 10;
const Tuid KExampleProperty = {0xED1917A8};
enum TExamplePropertyKeys { EIntProperty, EStrProperty };
 
#endif /*EXAMPLEPROPERTIES_H_*/

This snippet can be self-signed.

MMP文件

需要下列链接库

LIBRARY euser.lib

前言

发布者或订阅者线程都可以调用Rproperty::Define() 来定义属性。用户定义的属性将一直保存到操作系统重启或被删除。

Tint ret=Rproperty::Define(KExampleProperty,EIntProperty,Rproperty::Eint);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
 
ret= Rproperty::Define(KExampleProperty,EStrProperty,Rproperty::EByteArray,KMaxStrLen);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}

头文件

#ifndef EXAMPLESUBSCRIBER_H_
#define EXAMPLESUBSCRIBER_H_
 
// INCLUDES
#include <e32base.h>
#include <e32property.h>
 
#include "exampleproperties.h"
 
 
class MExampleSubscriberObserver
{
public:
virtual void IntPropertyUpdatedL(Tint aValue) = 0;
virtual void StrPropertyUpdatedL(Tdes& aValue) = 0;
};
 
 
class CExampleSubscriber : public Cactive
{
enum {Epriority=0};
public:
static CExampleSubscriber* NewL( const Tuid aUid,
const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
virtual ~CExampleSubscriber();
 
private:
CExampleSubscriber( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
void ConstructL();
void RunL();
void DoCancel();
private:
Rproperty iProperty;
const Tuid iUid;
const TUint32 iKey;
MExampleSubscriberObserver& iObserver;
};
 
#endif /*EXAMPLESUBSCRIBER_H_*/

源文件

#include "ExampleSubscriber.h"
 
CExampleSubscriber::CExampleSubscriber(
const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
: Cactive(Epriority), iUid( aUid ),
iKey( aKey ), iObserver( aObserver)
{
}
 
CExampleSubscriber* CExampleSubscriber::NewL( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
{
CExampleSubscriber* self=
new(Eleave) CExampleSubscriber( aUid, aKey, aObserver );
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
void CExampleSubscriber::ConstructL()
{
User::LeaveIfError( iProperty.Attach( iUid, iKey ) );
CActiveScheduler::Add(this);
// initial subscription and process current property value
RunL();
}
 
CExampleSubscriber::~CExampleSubscriber()
{
Cancel();
iProperty.Close();
}
 
void CExampleSubscriber::DoCancel()
{
iProperty.Cancel();
}
 
void CExampleSubscriber::RunL()
{
// resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
 
Tint intValue;
Tbuf<KMaxStrLen> strValue;
 
switch( iKey )
{
case EIntProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EIntProperty,intValue);
if( iProperty.Get( intValue ) == KErrNotFound )
{
// Int property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.IntPropertyUpdatedL(intValue);
}
break;
case EStrProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EStrProperty,strValue);
if( iProperty.Get( strValue ) == KErrNotFound )
{
// Str property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.StrPropertyUpdatedL(strValue);
}
break;
 
default:
break;
}
}
 
// End of File

使用CExampleSubscriber类

在头文件里:

#include "examplesubscriber.h"
 
class CPropertyExampleAppUi : public CAknAppUi, MExampleSubscriberObserver
{
//…
public:
void IntPropertyUpdatedL(Tint aValue);
void StrPropertyUpdatedL(Tdes& aValue);
//…
CExampleSubscriber* iIntSubscriber;
CExampleSubscriber* iStrSubscriber;
};

在源文件中:

void CPropertyExampleAppUi::ConstructL()
{
//…
iIntSubscriber = CExampleSubscriber::NewL(KExampleProperty,EIntProperty,*this);
iStrSubscriber = CExampleSubscriber::NewL(KExampleProperty,EStrProperty,*this);
}
 
CPropertyExampleAppUi::~CPropertyExampleAppUi()
{
//…
delete iIntSubscriber;
delete iStrSubscriber;
}
 
void CPropertyExampleAppUi::IntPropertyUpdatedL(Tint aValue)
{
//do something with the updated property
}
 
void CPropertyExampleAppUi::StrPropertyUpdatedL(Tdes& aValue)
{
//do something with the updated property
}

后记

使用Rproperty来订阅两个示例属性

如果订阅线程定义了属性,那么也可以使用Rproperty::Delete() 来删除掉。

Tint err(KErrNone);
 
err = Rproperty::Delete(KExampleProperty,EIntProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}
err = Rproperty::Delete(KExampleProperty,EStrProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}

当属性都被删除掉后,任何未完成的订阅将被告知KErrNotFound完成 (来自:Forum Nokia Wiki)

内容概要:本文档详细介绍了Android高级控件的使用方法及其应用场景。首先讲解了下拉列表Spinner,包括其两种表现形式(下拉列表形式和对话框形式),并介绍了适配器Adapter的基础概念及其三种主要类型:数组适配器ArrayAdapter、简单适配器SimpleAdapter和基本适配器BaseAdapter,重点阐述了它们各自的特点和使用步骤。接着,文档对列表视图ListView进行了深入探讨,涉及分隔线样式、按压背景等属性的设置方式。随后,描述了网格视图GridView,详细解释了其拉伸模式的效果及取值。对于翻页视图ViewPager,不仅介绍了基本概念,还展示了翻页标签栏PagerTabStrip的具体应用,特别是用于创建启动引导页。最后,文档介绍了碎片Fragment的概念,强调了其在大屏设备上的优势,以及与ViewPager结合使用的实战案例——记账本应用。 适合人群:有一定Android开发基础,希望深入了解并掌握高级控件使用的开发者。 使用场景及目标:①掌握下拉列表、列表视图、网格视图、翻页视图等高级控件的实现细节;②理解适配器的作用及其不同类型的使用场景;③学会使用Fragment优化应用界面布局,提高用户体验;④通过具体案例(如记账本),将所学控件应用于实际开发中。 阅读建议:本文档内容详实,涵盖多种高级控件的理论知识与实践技巧。建议读者在学习过程中结合官方文档或相关资料进行对比研究,同时动手实践,以便更好地理解和掌握这些控件的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值