[SOLVED] Qt on Android : How to Vibrate the device ?
-
bq. In the doc [developer.android.com]) it says “This method requires the caller to hold the permission VIBRATE.”
Did you get this permission for your app?Yes. I have this permission in the AndroidManifest.xml file:
@<uses-permission android:name="android.permission.VIBRATE"/>@
I have change the following code
@public static int add(int x)
{
return (x++);
}@with the following:
@ public static int add(int x)
{
return (++x);}@
but the result don't change:
@D/Qt ( 5509): ..\chronometer\mainwindow.cpp:123 (void MainWindow::manageStartEnd()): IN = 10 , OUT = 0@
Also I have change the function in the following mode, but in anycase the function don't return any value:
@ public static int add(int x)
{
return 53;}@
infact:
@D/Qt ( 6222): ..\chronometer\mainwindow.cpp:123 (void MainWindow::manageStartEnd()): IN = 10 , OUT = 0@
I'd like to see the following:
@D/Qt ( 6222): ..\chronometer\mainwindow.cpp:123 (void MainWindow::manageStartEnd()): IN = 10 , OUT = 53@
bq. I'm going crazy .....
-
Don't know maybe it is not important but I don't see semicolon at the end of a signature in the "example":http://qt-project.org/doc/qt-5/qandroidjniobject.html#callStaticMethod-2
@
jint value_out = QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example/Chronometer/Vibrate", "add", "(I)I;", /* <<<< at the end of this string */ value_in);
@ -
I have try whitout semicolon in this way:
@ QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/Chronometer/Vibrate", "start", "(I)V", 300);
int value_in = 10; jint value_out = QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example/Chronometer/Vibrate", "add", "(I)I", value_in); qDebug() << "IN = " << value_in << ", OUT = " << value_out;@
but the result is the same:
@D/Qt (20929): ..\chronometer\mainwindow.cpp:123 (void MainWindow::manageStartEnd()): IN = 10 , OUT = 0@
I have a new error message when I quit for my application:
@W/dalvikvm(24652): threadid=11: thread exiting with uncaught exception (group=0x416c92a0)
E/AndroidRuntime(24652): FATAL EXCEPTION: QtThread-1516700272
E/AndroidRuntime(24652): java.lang.NullPointerException
E/AndroidRuntime(24652): at org.qtproject.example.Chronometer.Vibrate.start(Vibrate.java:21)
E/AndroidRuntime(24652): at dalvik.system.NativeStart.run(Native Method)
W/SurfaceView(24652): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false@ -
I have found the solution and I have change the java class in this way:
@
import android.content.Context;
import android.os.Vibrator;
import android.app.Activity;public class Vibrate extends org.qtproject.qt5.android.bindings.QtActivity
{public void start(int x)
{
Vibrator v;
v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(x);
}public static int add(int x)
{
return 53;}
}@
and now the result is the following (without error):
@
D/Qt (27146): ..\chronometer\mainwindow.cpp:123 (void MainWindow::manageStartEnd()): IN = 10 , OUT = 53@The problem is that I haven't the vibration in my device :-(
-
there is something I am not considering, and I begin to doubt the official example: "Here":http://qt-project.org/doc/qt-5/qtandroidextras-notification-example.html [qt-project.org].
I have developed the new class java in this way:
@package org.qtproject.example.Chronometer;
import android.content.Context;
import android.os.Vibrator;
import android.app.Activity;public class Vibrate extends org.qtproject.qt5.android.bindings.QtActivity
{public static Vibrator m_vibrator;
public static Vibrate m_istance;public Vibrate()
{
System.out.println("Vibrate costrutor called");
m_istance = this;
}public static int start(int x)
{
System.out.println("start function called");
if (m_vibrator == null)
{
if (m_istance != null)
{
m_vibrator = (Vibrator) m_istance.getSystemService(Context.VIBRATOR_SERVICE);
m_vibrator.vibrate(x);
return 1;
}
else
{
return 3;
}
}
else
{
m_vibrator.vibrate(x);
return 2;
}
}public static int add(int x)
{
System.out.println("Add function called");
return 53;
}}
@and I call the class from c++ in this way:
@jint value_out1 = QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example/Chronometer/Vibrate", "start", "(I)I", 300);
qDebug() << "OUT = " << value_out1;
int value_in = 10;
jint value_out = QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example/Chronometer/Vibrate", "add", "(I)I", value_in);qDebug() << "IN = " << value_in << ", OUT = " << value_out;@
the output is the following (and I still have the vibration of the device):
@I/System.out(12301): start function called
D/Qt (12301): ..\chronometer\mainwindow.cpp:120 (void MainWindow::manageStartEnd()): OUT = 3
I/System.out(12301): Add function called
D/Qt (12301): ..\chronometer\mainwindow.cpp:125 (void MainWindow::manageStartEnd()): IN = 10 , OUT = 53@As you can see the function "start" is called but the m_istance is null and the the function return the value 3:
@D/Qt (12301): ..\chronometer\mainwindow.cpp:120 (void MainWindow::manageStartEnd()): OUT = 3@
This means that the java class constructor is never called.
In fact, in the log I do not see the inscription: "Vibrate costrutor called"
-
Wow !!!!! Now work. Thanks you very much.
List below steps for all developers.
- change the .pro file in QT in this way:
@QT += core gui androidextras@
@ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android@
- Add the permission in the AndroidMainifest.xml
@<uses-permission android:name="android.permission.VIBRATE"/>@
- replace the attribute in the activity:
from
@android:name="org.qtproject.qt5.android.bindings.QtActivity"@
to
@android:name="org.qtproject.example.Chronometer.Vibrate"@
Where Chronometer and Vibrate string can be changes.
- add the include following in the mainwindow.h
@#include <QtAndroidExtras/QAndroidJniObject>@
- add the following Vibrate.java class in the following directory
myprogget>android/src/org/qtproject/example/Chronometer/
where Chronometer can be change:
@//
// Vibrate.java
//
package org.qtproject.example.Chronometer;import android.content.Context;
import android.os.Vibrator;
import android.app.Activity;
import android.os.Bundle;public class Vibrate extends org.qtproject.qt5.android.bindings.QtActivity
{public static Vibrator m_vibrator;
public static Vibrate m_istance;public Vibrate()
{
m_istance = this;
}public static void start(int x)
{
if (m_vibrator == null)
{
if (m_istance != null)
{
m_vibrator = (Vibrator) m_istance.getSystemService(Context.VIBRATOR_SERVICE);
m_vibrator.vibrate(x);
}} else m_vibrator.vibrate(x);
}
}@- in your program call class in this way:
@QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/Chronometer/Vibrate", "start", "(I)V", 300);@
-
Hi
First of all thank you for sharing your solution.
but when i replaced the attribute in the activity:
from
@android:name="org.qtproject.qt5.android.bindings.QtActivity"@
to
@android:name="org.qtproject.example.Chronometer.Vibrate"@i got this error msgs:
@Warning: Dependency not found: C:/Qt/Qt5.3.0/5.3/android_armv7/plugins/platformthemes
Warning: Dependency not found: C:/Qt/Qt5.3.0/5.3/android_armv7/plugins/platforminputcontexts
Error while building/deploying project vibration (kit: Android for armeabi-v7a (GCC 4.7, Qt 5.3.0))
When executing step 'Deploy to Android device'@What am i doing wrong?
-
It is not easy to understand, but it seems a problem of Dependency.
Please can you check if you are following all steps indicate above ?
You have put the file Vibrate.java under the following directory ?
myprogget>android/src/org/qtproject/example/Chronometer/
and add the file Vibrate.java into your qt projects (as exist file) ?
If you share your .pro file perhaps I will understand better the problem.