headless apps on blackberry 10.2.1

Post on 19-May-2015

429 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

The code can be found here: https://github.com/zezke/WaterThePlants

TRANSCRIPT

Headless AppsI am the invisible man.

Who am I?

Founder of Endare

Certified BlackBerry Developer

zezke on the BB Developer fora

@brvandewalle

What is “a headless app”?

A headless application is an application that runs in the background without a visible UI.

Are all headless apps equal?Short-running headless apps

maximum run time of 20 seconds

Long-running headless apps

no time limit

All are limited to:

7 to 25 percent of CPU at any given time

limited to 3MB of memory

Long-running headless apps

Requires extended permissions

Yes, a web form

The structure of a HA

Two separate projects in a single BAR file

UI part (required!)

headless service

How does the headless part start?

Invoked by the system on certain events (triggers)

sytem.STARTED

PORT_DIRECTED_SMS

PUSH

GEOREGIONEXIT

GEOREGIONENTER

By any UI application via an invoke target

Time for some code

What’s the goal of the demo app?

Water the plants!

Based on location

And ideally on time (like every two days)

1. Create the projectNew template in 10.2.1 SDK!

Saves you about 15 minutes of converting another template

2. Create the UI part

Standard Cascades stuff

a MapView showing your current location

some data

and a button to set the geofence

3. Set the geofence (I)

Request permissions

<permission>access_location_services</permission>

Link the necessary libraries (in the .pro file)

-lgeomonitor

( -lQtLocationSubset )

3. Set the geofence (II)

//Clear the previous geofence//If you fail/forget to do this you will get an error 257geomonitor_remove("Garden");

3. Set the geofence (III)//Set the geofencegeomonitor_region_t region = NULL;geomonitor_create_region(&region, "Garden");geomonitor_region_set_circle_shape(region, latitude, longitude,

100.0);geomonitor_region_set_monitoring_mode(region,

GEOMONITOR_MONITORING_MODE_PERSISTENT);geomonitor_region_set_notification_invoke_target(region,

"com.endare.WaterThePlantsService", GEOMONITOR_NOTIFICATION_DIRECT);

//Adding the regionint errorCode = geomonitor_add(region);if (errorCode!= 0){

qWarning()<<"Error occurred with code " << QString::number(errorCode) << endl;

}else {

qDebug() << "Geofence set!";}geomonitor_destroy_region(&region);

3. Set the geofence (IV)

//Adding the regionint errorCode = geomonitor_add(region);if (errorCode!= 0){

qWarning()<<"Error occurred with code " << QString::number(errorCode) << endl;

}else {

qDebug() << "Geofence set!";}geomonitor_destroy_region(&region);

4. Handle the invoke (I)

Now we have configured a geofence

Entering this geofence will trigger the headless part to be invoked

4. Handle the invoke (II)

First connect the slot to the signal in the constructor of the headless service

m_invokeManager->connect(m_invokeManager, SIGNAL(invoked(const bb::system::InvokeRequest&)), this, SLOT(handleInvoke(const bb::system::InvokeRequest&)));

4. Handle the invoke (III)

void Service::handleInvoke(const bb::system::InvokeRequest & request) {if (request.action().compare("bb.action.GEOREGIONENTER") == 0) {

qDebug() << "Service:: entered the georegion";triggerNotification(request);

}else {

qDebug() << "Service:: unknown service request " << request.action();}

}

5. Trigger a notification (I)

// clear any existing notificationsNotification::clearEffectsForAll();Notification::deleteAllFromInbox();

// turn on previews in settingsbb::platform::NotificationDefaultApplicationSettings settings;

settings.setPreview(bb::platform::NotificationPriorityPolicy::Allow);settings.apply();

5. Trigger a notification (II)InvokeRequest invokeReqNotification;invokeReqNotification.setTarget("com.endare.WaterThePlants");

//Setting the content of the bodyQString body;body = “Don’t forget to water your plants!”;

//Setting the body and the InvokeRequest of the notification_notification.setBody(QString::fromUtf8(body.toAscii()));_notification.setInvokeRequest(invokeReqNotification);

//Displaying the Instant Preview_notification.notify();

Conclusion

It works

But slowly (takes up to three hours to fire at the moment)

might be a bug

A lot of future work

include time aspect

Questions?

top related