如何从Qt的Android手机震动 [英] How to make phone vibrate from Qt Android
问题描述
我部署使用Qt的Android应用程序。
I'm deploying Android app using Qt.
我想使手机震动。于是我试图执行这个code 使用 QAndroidJniObject
。
I'd like to make the phone vibrate. So I try to execute this code using QAndroidJniObject
.
Java的code:
Java code:
import android.os.Vibrator;
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
C ++的Qt code:
C++ Qt code:
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if ( activity.isValid() )
{
QAndroidJniObject serviceName = QAndroidJniObject::fromString("android.content.Context.VIBRATOR_SERVICE");
if ( serviceName.isValid() )
{
QAndroidJniObject vibrator = activity.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;",serviceName.object<jobject>());
if ( vibrator.isValid() )
{
vibrator.callMethod<void>("vibrate", "(J)V", 1000);
}
// vibrator is actually not valid!
}
}
vibrator.isValid()
返回false,我想不通为什么....这不是我第一次尝试做这样的东西,但在这里,我不能让它工作。
vibrator.isValid()
returns false and I cannot figure out why....It's not my first time trying to do this kind of stuff, but here, I can't make it work.
请注意:我的应用程序有 android.permission.VIBRATE
设置
Note: My app has android.permission.VIBRATE
set
推荐答案
从我能从的文档, QAndroidJniObject :: fromString
返回一个对象包装Java字符串与你给内容fromString
。
From what I could gather from the documentation, QAndroidJniObject::fromString
returns an object wrapping a Java string with the contents that you gave to fromString
.
所以,你在做什么,现在就好像是你做了Java中的以下内容:
So what you're doing right now is as if you had done the following in Java:
Object vibrator = getSystemService("Context.VIBRATOR_SERVICE");
当你真正想要的是:
Object vibrator = getSystemService(Context.VIBRATOR_SERVICE);
因此,而不是使用 QAndroidJniObject :: fromString
你也应该可以做这样的事情:
So instead of using QAndroidJniObject::fromString
you probably ought to be doing something like this:
QAndroidJniObject serviceName =
QAndroidJniObject::getStaticObjectField<jstring>(
"android/content/Context",
"VIBRATOR_SERVICE");
这有可能是你需要删除本地引用服务名
之后。
这篇关于如何从Qt的Android手机震动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!