如何使手机振动从Qt Android [英] How to make phone vibrate from Qt Android

查看:901
本文介绍了如何使手机振动从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>, QAndroidJniObject :: fromString 返回一个包含Java字符串的对象,其中包含您给 fromString / p>

所以你现在做的就好像你在Java中做了下面这样:

  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


I'm deploying Android app using Qt.

I'd like to make the phone vibrate. So I try to execute this code using QAndroidJniObject.

Java code:

import android.os.Vibrator;
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);

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() 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.

Note: My app has android.permission.VIBRATE set

解决方案

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.

So what you're doing right now is as if you had done the following in Java:

Object vibrator = getSystemService("Context.VIBRATOR_SERVICE");

when what you really want is:

Object vibrator = getSystemService(Context.VIBRATOR_SERVICE);

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");

It's possible that you need to delete the local reference to serviceName afterwards.

这篇关于如何使手机振动从Qt Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆