3

我正在使用 QtCreator 部署一个 Android 应用程序。

我想播放声音并让手机振动以通知用户发生了不好的事情。我使用 QAndroidJniObject 来调用 SDK 函数。

我不能让任何人工作。

对于“振动”:

// java code:
// 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::getStaticObjectField<jstring>("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", "(I)V", 1000);
        }
    }
}

vibrator.callMethod已到达(感谢Michael 的帮助),但手机没有振动(请注意,我的应用程序有android.permission.VIBRATE

对于“播放声音”,我尝试了两种方法:

// java code:
// Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Ringtone ring = RingtoneManager.getRingtone(getApplicationContext(), notification);
// ring.play();
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if ( activity.isValid() )
{
    jint notifcationType = QAndroidJniObject::getStaticField<jint>("android/media/RingtoneManager", "TYPE_NOTIFICATION");
    {
        QAndroidJniObject notification = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager", "getDefaultUri", "(I)Landroid/net/Uri;", notifcationType);
        if ( notification.isValid() )
        {
            QAndroidJniObject ring = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager", "getRingtone", "(Landroid.content.Context;Landroid/net/Uri;)Landroid.media.Ringtone;",activity.object<jobject>(),notification.object<jobject>());
            if ( ring.isValid() )
            {
                ring.callMethod<void>("play", "()V");
            }
        }
    }
}

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if ( activity.isValid() )
{
    jint notifcationType = QAndroidJniObject::getStaticField<jint>("android/media/RingtoneManager", "TYPE_NOTIFICATION");

    QAndroidJniObject notification = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager", "getDefaultUri", "(I)Landroid/net/Uri;", notifcationType);
    if ( notification.isValid() )
    {
        QAndroidJniObject player = QAndroidJniObject::callStaticObjectMethod("android/media/MediaPlayer", "create", "(Landroid.content.Context;Landroid/net/Uri;)Landroid.media.MediaPlayer;",activity.object<jobject>(),notification.object<jobject>());

        if ( player.isValid() )
        {
            player.callMethod<void>("start", "()V");
        }
    }
}

在这两种情况下,都没有到达最后一个函数,因为对象无效(玩家和戒指都无效)。

4

1 回答 1

3
  1. vibrate() 需要 long 作为参数,而不是 int。
  2. 在您的 jni 调用中,始终使用斜杠来分隔包名和类名。

尝试:

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
//    QAndroidJniObject activity = QtAndroid::androidActivity(); // Req. Qt 5.3
if ( activity.isValid() )
{
    // This gets the string value of the VIBRATOR_SERVICE field.
    QAndroidJniObject serviceField = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Context", "VIBRATOR_SERVICE");
    if ( serviceField.isValid() )
    {
        QAndroidJniObject vibrator = activity.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;",serviceField.object<jobject>());
        if ( vibrator.isValid() )
        {
            // Note that vibrate takes a long and not an int as argument.
            vibrator.callMethod<void>("vibrate", "(J)V", 500);
        }
    }
}

QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if ( activity.isValid() )
{
    jint notifcationType = QAndroidJniObject::getStaticField<jint>("android/media/RingtoneManager", "TYPE_NOTIFICATION");
    QAndroidJniObject notification = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager", "getDefaultUri", "(I)Landroid/net/Uri;", notifcationType);
    if ( notification.isValid() )
    {
        // Note that package and class names needs to be separated with '/' and not '.'
        QAndroidJniObject ring = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager",
                                                                       "getRingtone",
                                                                       "(Landroid/content/Context;Landroid/net/Uri;)Landroid/media/Ringtone;",
                                                                       activity.object<jobject>(),
                                                                       notification.object<jobject>());
        if ( ring.isValid() )
        {
            ring.callMethod<void>("play", "()V");
        }
    }
}
于 2015-04-25T19:44:23.773 回答