J.Hilk
J.Hilk

Reputation: 1

Calling java-functions in Qt for runtimePermissions

to make my App ready for Android 6.0 I need to use the new runtimePermission feature. Qt, currently, does not support this yet.

I think I'll have to use some native java-code, explicitly:

checkSelfPermission and requestPermissions

from the ContextCompat.java and ActivityCompat.java respectively

the way to do this seems to be via

#include <QtAndroidExtras> and QAndroidJniObject::callStaticMethod

here is what I have so far, from my simple test program:

void MainWindow::on_pushButton_clicked()
{
jint res = 9999;

QAndroidJniObject javaCall = QAndroidJniObject::fromString("android.permission.CAMERA");
res = QAndroidJniObject::callStaticMethod<jint>("JavaSrc/ContextCompat",
                                    "checkSelfPermission",
                                    "(Ljava/lang/String;)V",
                                    javaCall.object<jstring>());

ui->label->setText(QString::number(res));
qDebug() << Res;
}

I copied the ContextCompat from the SDK path into my ResourceFolder. I did not make an extra java file for my App, might this be a problem?

The return of 'res' is always 0 (0 = permissen granted), whether the permission is granted or not. So my approach seems to work somewhat, but not in the way it's supposed to.

I would guess, that the checkSelfPermission is not linked to my Qt-App, even so its called from there.

Might be the Java-file or might be my Qt-code, I'm not sure.

So what is the right way to do this?

Upvotes: 0

Views: 425

Answers (1)

Assane Gueye
Assane Gueye

Reputation: 11

Try the following:

QAndroidJniObject javaCall = QAndroidJniObject::fromString("android.permission.READ_PHONE_STATE");
res = QAndroidJniObject::callStaticMethod<jint>("android/support/v4/content/ContextCompat",
                                    "checkSelfPermission",
                                    "(Landroid/content/Context;Ljava/lang/String;)I",
                                    QtAndroid::androidActivity().object(),
                                    javaCall.object<jstring>());
if(res == 0)
   qDebug()<<"you have permission";
else
   qDebug()<<"you do not have permission";

Upvotes: 1

Related Questions