Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
New Developer
Posts: 7
Registered: ‎05-09-2013
My Device: Blackberry z10
My Carrier: mobilink

Is there something like real time clock listener in blackberry 10?

I was wondering if something like a realtime clock listener existed in blackberry 10? If not,is there any other way to achieve the same functionality? I know that the real time clock listener used to respond the clockupdated event of blackberry.Is there a similar event type in BB10 which runs after every minute?

Developer
Posts: 671
Registered: ‎03-21-2012
My Device: BlackBerry PlayBook 16GB
My Carrier: Vip

Re: Is there something like real time clock listener in blackberry 10?

New Developer
Posts: 7
Registered: ‎05-09-2013
My Device: Blackberry z10
My Carrier: mobilink

Re: Is there something like real time clock listener in blackberry 10?

seems like it can be used to get events when my timezone changes.Can I use it to get events when the minute changed?

New Developer
Posts: 7
Registered: ‎05-09-2013
My Device: Blackberry z10
My Carrier: mobilink

Re: Is there something like real time clock listener in blackberry 10?

I want my bb10 app to call a function after every minute.Is there an event in this class to which my app could register to?

Developer
Posts: 38
Registered: ‎04-11-2012
My Device: 8520
My Carrier: RIM

Re: Is there something like real time clock listener in blackberry 10?

Hi Sir did your Problem resolved.Please post your solution here.

Thanks

Developer
Posts: 1,065
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Is there something like real time clock listener in blackberry 10?

[ Edited ]

Is there any reason you couldn't just use a QTimer with an interval of 60,000? Once you start it it will fire its triggered() signal every 60 seconds. Just put the code you want to execute every minute into a slot that is connected to the triggered() signal.

 

Something like this:

 

#include <QTimer>

MyClass::MyClass() {
bool success;
Q_UNUSED( success );
QTimer* timer = new QTimer( this );
timer->setInterval( 60000 );
success = QObject::Connect( timer, SIGNAL( timeout() ), SLOT( mySlot() ) );
Q_ASSERT( success );

timer->start(); }

MyClass::mySlot() {
// ---This slot will be exectuted every 60 seconds

}

 



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Developer
Posts: 242
Registered: ‎01-08-2013
My Device: Z10 LE
My Carrier: SiMobil

Re: Is there something like real time clock listener in blackberry 10?

[ Edited ]

60s isn't ok because it won't fire immediately after the time changes (although it depends on what his needs are)

 

I suggest you set interval to 1s (so setInterval(1000)) and then check if minutes have changed (for which you need to store previous time)

Developer
Posts: 380
Registered: ‎04-08-2013
My Device: Z10
My Carrier: Virgin Canada

Re: Is there something like real time clock listener in blackberry 10?

There are none that I know of, I've created a Clock class for you.

 

This one will be fired every second, in case you ever need to get notified of second changed too. You can easily modify the code to set the interval to 60000 msecond instead of 1000 msecs.

 

 

// Clock.cpp


#include <Clock.h> #include <QDebug> #include <QTime> Clock::Clock(QObject *_parent) : QObject(), m_myClock(new QTimer(this)) { oldTime.insert("second", QTime::currentTime().second()); oldTime.insert("minute", QTime::currentTime().minute()); oldTime.insert("hour", QTime::currentTime().hour()); connect(m_myClock, SIGNAL(timeout()), this, SLOT(onTimeout())); m_myClock->setInterval(1000); QTimer::singleShot(1000 - QTime::currentTime().msec(), m_myClock, SLOT(start())); } void Clock::onTimeout() { QVariantMap newTime; newTime.insert("msec", QTime::currentTime().msec()); newTime.insert("second", QTime::currentTime().second()); newTime.insert("minute", QTime::currentTime().minute()); newTime.insert("hour", QTime::currentTime().hour()); qDebug() << "newTime:" << newTime.keys() << newTime.values(); if (newTime["second"] != oldTime["second"]) emit secondChanged(newTime["second"].toInt()); if (newTime["minute"] != oldTime["minute"]) emit minuteChanged(newTime["minute"].toInt()); if (newTime["hour"] != oldTime["hour"]) emit hourChanged(newTime["hour"].toInt()); oldTime = newTime; // Detect if the timer gets out of sync, max out of sync value is 10 msec if ((newTime["msec"].toInt() > 10) && (newTime["msec"].toInt() < 990)) QTimer::singleShot(1000 - QTime::currentTime().msec(), m_myClock, SLOT(start())); }

 

// Clock.h


#ifndef CLOCK_H_
#define CLOCK_H_

#include <QObject>
#include <QTimer>

class Clock : public QObject
{
    Q_OBJECT

public:
    Clock(QObject *_parent = 0);
    virtual ~Clock() {}

private slots:
    void onTimeout();

private:
    QTimer* m_myClock;
    QVariantMap oldTime;

signals:
    void secondChanged(const int&);
    void minuteChanged(const int&);
    void hourChanged(const int&);
};

#endif /* CLOCK_H_ */

  

Developer
Posts: 150
Registered: ‎10-19-2012
My Device: Playbook, BB10DevAlphaB/C, Z10
My Carrier: Sonera, Saunalahti

Re: Is there something like real time clock listener in blackberry 10?

That code is very inefficient, why do you use a hash table to store the time when a QTime can do it just fine.

And you also have a race condition when you query the time 3-4 times in a row, the time might change between the calls.
Developer
Posts: 380
Registered: ‎04-08-2013
My Device: Z10
My Carrier: Virgin Canada

Re: Is there something like real time clock listener in blackberry 10?

You're right, just did the test with Device Monitor, using QTime instead of QVariantMap is more efficient, here's my testing results after trying both for one hour :

QVariantMap :
Battery Usage - 0.05%
CPU - 0.10%
Memory Usage - 10Mb

QTime :
Battery Usage - 0.04%
CPU - 0.08%
Memory Usage - 10Mb

I realized there's a coding mistake too where if the timer is fired a few msecs before the second, it will not fire a second change signal. A quick fix is to round the time to the nearest second, so that 11:59:59 and 999 msecs would round to 12:00:00.

I don't understand your last point though, this code doesn't allow to 'query the time', it only allows to be notified of a time change. To query the time, use QTime::currentTime().