我正在使用 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");
}
}
}
在这两种情况下,都没有到达最后一个函数,因为对象无效(玩家和戒指都无效)。