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 helpのおかげです) が、電話が振動しません (私のアプリケーションには があることに注意してください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() は 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 に答える