Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Android JNI, display AlertDialog for LOCATION permissions



  • I want to show an AlertDialog on my android App to request "Location" permission since it's compulsory from Android API 26. I am using JNI to call a Java function that will prompt the trigger the AlertDialog.

    below is my Java code

        public static void init(String node){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (m_instance.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(m_instance);
                    builder.setTitle("This app needs location access");
                    builder.setMessage("Please grant location access so this app can detect beacons.");
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            m_instance.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                        }
                    });
                    builder.show();
                }
            }
    }
    

    And this is how i am calling it from my C++ code.

        QAndroidJniObject javaNotification = QAndroidJniObject::fromString("beconNode");
        QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/beacorat/Scan",
                                                  "init",
                                                  "(Ljava/lang/String;)V",
                                                  javaNotification.object<jstring>());
    

    I am receiving the below error when trying to call the function.

    F art     : art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: JNI GetStaticMethodID called with pending exception java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    F art     : art/runtime/java_vm_ext.cc:410]   at void android.os.Handler.<init>(android.os.Handler$Callback, boolean) (Handler.java:200)
    F art     : art/runtime/java_vm_ext.cc:410]   at void android.os.Handler.<init>() (Handler.java:114)
    F art     : art/runtime/java_vm_ext.cc:410]   at void android.app.Dialog.<init>(android.content.Context, int, boolean) (Dialog.java:152)
    F art     : art/runtime/java_vm_ext.cc:410]   at void android.app.AlertDialog.<init>(android.content.Context, int, boolean) (AlertDialog.java:202)
    F art     : art/runtime/java_vm_ext.cc:410]   at android.app.AlertDialog android.app.AlertDialog$Builder.create() (AlertDialog.java:1105)
    F art     : art/runtime/java_vm_ext.cc:410]   at android.app.AlertDialog android.app.AlertDialog$Builder.show() (AlertDialog.java:1150)
    F art     : art/runtime/java_vm_ext.cc:410]   at void org.qtproject.example.beacorat.Scan.initBT() (Scan.java:54)
    F art     : art/runtime/java_vm_ext.cc:410]   at void org.qtproject.example.beacorat.Scan.startLeScan(java.lang.String) (Scan.java:74)
    
    

    This crashes the whole App.

    Please help.


  • Moderators

    @Bongani

    I'm not very good with java, a reason why I'm very grateful for the QtAndroid modul.

    you can use that one for runtime permission request/check

    http://doc.qt.io/qt-5/qtandroid.html#requestPermissions

    This should help you.


Log in to reply