Next: Qt on Android : How to Vibrate the device

Qt on Android : How to Vibrate the device

Publish the new topic about: "How to Vibrate the device" at following link

List below steps for all developers.


0) change the .pro file in QT in this way:
  1. QT       += core gui androidextras
  1. ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
1) Add the permission in the AndroidMainifest.xml
  1. <uses-permission android:name="android.permission.VIBRATE"/>
2) replace the attribute in the activity:

from
  1. android:name="org.qtproject.qt5.android.bindings.QtActivity"
to
  1. android:name="org.qtproject.example.Chronometer.Vibrate"
Where Chronometer and Vibrate string can be changes.

3) add the include following in the mainwindow.h
  1. #include <QtAndroidExtras/QAndroidJniObject>
4) add the following Vibrate.java class in the following directory
myprogget>android/src/org/qtproject/example/Chronometer/

where Chronometer can be change:
  1. //
  2. // Vibrate.java  
  3. //
  4. package org.qtproject.example.Chronometer;
  5. import android.content.Context;
  6. import android.os.Vibrator;
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. public class Vibrate extends org.qtproject.qt5.android.bindings.QtActivity
  10. {
  11.    public static Vibrator m_vibrator;
  12.    public static Vibrate m_istance;
  13.    public Vibrate()
  14.    {
  15.        m_istance = this;
  16.    }
  17.    public static void start(int x)
  18.    {
  19.        if (m_vibrator == null)
  20.        {
  21.            if (m_istance != null)
  22.            {
  23.              m_vibrator = (Vibrator) m_istance.getSystemService(Context.VIBRATOR_SERVICE);
  24.              m_vibrator.vibrate(x);
  25.            }
  26.        }
  27.        else m_vibrator.vibrate(x);
  28.    }
  29. }
5) in your program call class in this way:
  1. QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/Chronometer/Vibrate", "start", "(I)V", 300);