Solved Make toast in Qt android from Java code
-
I try to use Toast function from java in android using qt 5.6.1
I add the java file in$PWD/android/src/org/qtproject/example/amr
then i add
QT += androidextras
in the .pro file
the java codepackage org.qtproject.example.amr; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.widget.Toast; import org.qtproject.qt5.android.bindings.QtApplication; import org.qtproject.qt5.android.bindings.QtActivity; public class Main extends org.qtproject.qt5.android.bindings.QtActivity { public static int getSize() { return 111; } public void MakeToast(){ Toast.makeText(Main.this,"Hello world",Toast.LENGTH_LONG).show(); } }
the c++ code
#include "widget.h" #include "ui_widget.h" #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_clicked() { //QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/Main", "MakeToast"); // QAndroidJniObject obj; // obj.callMethod<void>("org/qtproject/example/notification/Main","MakeToast"); QtAndroid::androidActivity().callMethod<void>("org/qtproject/example/notification/Main","MakeToast"); }
but when i press the button to see the toast i don't get any response for that how to fix that
Thanks -
Hi.
First, you need to check your java class name.
Probably it will be "org/qtproject/example/amr/Main" instead of "org/qtproject/example/notification/Main".See check using
QAndroidJniObject::isClassAvailable()
-
ok i fix it and i check QAndroidJniObject::isClassAvailable() and return true
but when i press button i don't see the toast ! -
what i have reached
void Widget::on_pushButton_clicked() { QtAndroid::androidActivity().callMethod<void>("org/qtproject/example/amr/Main","MakeToast"); qDebug() << "class avalible = " << QAndroidJniObject::isClassAvailable("org/qtproject/example/amr/Main"); // return true }
-
@AmrCoder said in Make toast in Qt android from Java code:
Toast.makeText(Main.this,"Hello world",Toast.LENGTH_LONG).show();
JNI calls made from C++ are running in the Qt thread. But Toast.makeText() must be called from the Android UI thread.
This should be visible by a thrown exception in the console output.So either you change your Java code to run on the UI thread or you use Qt 5.7 to make the call explicitly on the UI thread:
void showToast(const QString &message, Duration duration = LONG) { // all the magic must happen on Android UI thread QtAndroid::runOnAndroidThread([message, duration] { QAndroidJniObject javaString = QAndroidJniObject::fromString(message); QAndroidJniObject toast = QAndroidJniObject::callStaticObjectMethod("android/widget/Toast", "makeText", "(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;", QtAndroid::androidActivity().object(), javaString.object(), jint(duration)); toast.callMethod<void>("show"); }); }
-
Thank you very much i install qt 5.7 and use this function it works
-
This introduces a different way using Java... like your code.
// in your java code
[...] import android.os.Handler; [...] private Handler handler = new Handler(); public void MakeToast() { handler.post(new Runnable() { @Override public void run () { Toast.makeText(getApplicationContext(), "Hello world", Toast.LENGTH_LONG).show(); } }); } [...]
// in your main.cpp
void Widget::on_pushButton_clicked() { QtAndroid::androidActivity().callMethod<void>("MakeToast"); }
-
i didn't work for me this my full java class
package org.qtproject.example.amr; import android.content.Context; import android.os.Environment; import android.os.Handler; import android.widget.*; public class Main extends org.qtproject.qt5.android.bindings.QtActivity { private Handler handler = new Handler(); public void MakeToast() { handler.post(new Runnable() { @Override public void run () { Toast.makeText(getApplicationContext(), "Hello world", Toast.LENGTH_LONG).show(); } }); } }
what i do wrong here ?
-