如何使手机振动从Qt Android [英] How to make phone vibrate from Qt Android
问题描述
我正在使用Qt部署Android应用程式。
我想让手机振动。因此,我尝试使用 QAndroidJniObject
执行此代码。
Java代码:
import android.os.Vibrator;
Vibrator v =(Vibrator)This.context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
C ++ Qt代码:
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.isValid
返回false,我不知道为什么....这不是我第一次尝试这样的东西,但在这里,我不能让它工作。
注意:我的应用程式有 android.permission.VIBRATE
设定
从我可以从收集的文档> a>, 所以你现在做的就好像你在Java中做了下面这样: 当你真正想要的是: 所以不要使用 可能需要删除 I'm deploying Android app using Qt. I'd like to make the phone vibrate. So I try to execute this code using Java code: C++ Qt code: Note: My app has From what I could gather from the documentation, So what you're doing right now is as if you had done the following in Java: when what you really want is: So instead of using It's possible that you need to delete the local reference to 这篇关于如何使手机振动从Qt Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! QAndroidJniObject :: fromString
返回一个包含Java字符串的对象,其中包含您给 fromString
/ p>
Object vibrator = getSystemService(Context.VIBRATOR_SERVICE);
Object vibrator = getSystemService(Context.VIBRATOR_SERVICE);
QAndroidJniObject :: fromString
你可能应该这样做:
QAndroidJniObject serviceName =
QAndroidJniObject :: getStaticObjectField< jstring> b $ bandroid / content / Context,
VIBRATOR_SERVICE);
serviceName
。QAndroidJniObject
.import android.os.Vibrator;
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
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()
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
setQAndroidJniObject::fromString
returns an object wrapping a Java string with the contents that you gave to fromString
.Object vibrator = getSystemService("Context.VIBRATOR_SERVICE");
Object vibrator = getSystemService(Context.VIBRATOR_SERVICE);
QAndroidJniObject::fromString
you probably ought to be doing something like this:QAndroidJniObject serviceName =
QAndroidJniObject::getStaticObjectField<jstring>(
"android/content/Context",
"VIBRATOR_SERVICE");
serviceName
afterwards.