我正在使用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);
}
}
}
到达{p> 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");
}
}
}
在这两种情况下,都没有到达最后一个函数,因为对象无效(播放器和环也无效)。
答案 0 :(得分:2)
尝试:
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");
}
}
}