Author Archives: admin

How to vibrate with Qt/QML on Android?

Short answer – pretty simple. Thanks to QtAndroidExtras

First we need to add to your .pro file:

android: QT += androidextras

 

then add simple class to your codebase. Header:

#ifndef VIBRATOR_H
#define VIBRATOR_H

#include <QObject>

#if defined(Q_OS_ANDROID)
#include <QAndroidJniEnvironment>
#include <QAndroidJniObject>
#endif
class Vibrator : public QObject
{
    Q_OBJECT
public:
    explicit Vibrator(QObject *parent = 0);
signals:
public slots:
    void vibrate(int milliseconds);
private:
#if defined(Q_OS_ANDROID)
    QAndroidJniObject vibratorService;
#endif
};

#endif // VIBRATOR_H

 

and the code:

#include "vibrator.h"
#include <QDebug>

Vibrator::Vibrator(QObject *parent) : QObject(parent)
{
#if defined(Q_OS_ANDROID)
    QAndroidJniObject vibroString = QAndroidJniObject::fromString("vibrator");
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
    QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
    vibratorService = appctx.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", vibroString.object<jstring>());
#endif
}

#if defined(Q_OS_ANDROID)

void Vibrator::vibrate(int milliseconds) {
    if (vibratorService.isValid()) {
        jlong ms = milliseconds;
        jboolean hasvibro = vibratorService.callMethod<jboolean>("hasVibrator", "()Z");
        vibratorService.callMethod<void>("vibrate", "(J)V", ms);
    } else {
        qDebug() << "No vibrator service available";
    }
}

#else
void Vibrator::vibrate(int milliseconds) {
    Q_UNUSED(milliseconds);
}

#endif

 

now you have to expose the class to QML:

#include "vibrator.h"
...
Vibrator vibrator;
engine.rootContext()->setContextProperty("Vibrator", &vibrator);

 

voila! its ready to use!

Vibrator.vibrate(500)

 

Enjoy

New tech can boost smartphone resolution to 4K without hindering battery life – CNET

By Adam Bolton Software technology being developed in Japan is able to improve smartphone screen resolution to 4K while not drastically effecting battery life.

…read more

Source:: http://cnet.com.feedsportal.com/c/34938/f/645093/s/4bbd8138/sc/15/l/0L0Scnet0N0Cnews0Csmartphones0Eare0Eabout0Eto0Elooks0Esmarter0Ethanks0Eto0E4k0Etech0C0Tftag0FCAD590Aa51e/story01.htm

      

Ahmed the Clock Kid wants $15 million from city and school – CNET

By Chris Matyszczyk Technically Incorrect: Ahmed Mohamed, the 14-year-old who was arrested for bringing a homemade clock to school and then became a hero at Google and Facebook, is now demanding compensation and an apology.

…read more

Source:: http://cnet.com.feedsportal.com/c/34938/f/645093/s/4bbd1e07/sc/7/l/0L0Scnet0N0Cnews0Cahmed0Ethe0Eclock0Eboy0Ewants0E150Emillion0Efrom0Ecity0Eand0Eschool0C0Tftag0FCAD590Aa51e/story01.htm

      

Microsoft's female workforce shrank 2 percent last year – CNET

By Steven Musil The company’s workforce diversity efforts suffer a setback due to job cuts at overseas factories — jobs held by a high percentage of women.

…read more

Source:: http://cnet.com.feedsportal.com/c/34938/f/645093/s/4bbcf95b/sc/21/l/0L0Scnet0N0Cnews0Cmicrosofts0Efemale0Eworkforce0Eshrank0E20Epercent0Elast0Eyear0C0Tftag0FCAD590Aa51e/story01.htm

      

Robot snake can shift its shape quickly, also haunts our dreams (Tomorrow Daily 276 show notes) – CNET

By Ashley Esqueda All we can say is, “Robot snakes. Why did it have to be robot snakes?”

…read more

Source:: http://cnet.com.feedsportal.com/c/34938/f/645093/s/4bbca07d/sc/23/l/0L0Scnet0N0Cnews0Cmit0Emade0Ea0Eshapeshifting0Esnake0Erobot0Eand0Eit0Ecreeps0Eus0Eout0Etomorrow0Edaily0E2760C0Tftag0FCAD590Aa51e/story01.htm