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

Android runtime permissons



  • Hello,

    I'm trying to make my app with runtime permissions, but with the snippets so far tried, I've only managed to crash the app.

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
        keep_screen_on(true);
    
    //---------------SNIPPET_1-------------------
        auto  result = QtAndroid::checkPermission(QString("android.permission.CAMERA"));
        if(result == QtAndroid::PermissionResult::Denied){
            QtAndroid::PermissionResultMap resultHash = QtAndroid::requestPermissionsSync(QStringList({"android.permission.CAMERA"}));
            if(resultHash["android.permission.CAMERA"] == QtAndroid::PermissionResult::Denied)
                return 0;
        }
    //-----------------OR-------------------------
    //For this one, I do have declared "const  QVector<QString> permissions({"android.permission.ACCESS_COARSE_LOCATION", "android.permission.BLUETOOTH"});
    
        //Request requiered permissions at runtime
        for(const QString &permission : permissions){
            auto result = QtAndroid::checkPermission(permission);
            if(result == QtAndroid::PermissionResult::Denied){
                auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission}));
                if(resultHash[permission] == QtAndroid::PermissionResult::Denied)
                    return 0;
            }
        }
    
    //-------------------END---------------------
    
        QQmlApplicationEngine engine;
        anyclass Anyclass;
    
        QStringList dataList;
        QStringList windowNames;
        windowNames.append("P1");
        windowNames.append("P2");
        windowNames.append("P3");
        windowNames.append("P4");
    
        Anyclass.on_reload_list();
        dataList = Anyclass.m_Lmodel;
    
        qmlRegisterType<StatusBar>("StatusBar", 0, 1, "StatusBar");
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        engine.rootContext()->setContextProperty("Anyclass", &Anyclass);
        engine.rootContext()->setContextProperty("myModel", QVariant::fromValue(dataList));
        engine.rootContext()->setContextProperty("windowModel", QVariant::fromValue(windowNames));
        QObject *rootObject = engine.rootObjects().first();
        Anyclass.get_qml_obj(rootObject);
    
        if (engine.rootObjects().isEmpty())
            return -1;
    
        QtAndroid::hideSplashScreen();
    
    
        return app.exec();
    }
    

    Some other thing I've found is this, but I don't know how can I use it:

    Using QAndroidJniObject::callStaticMethod<void>()
    And the java code:
    
    public static void requestPermission(QtActivity activity){
                activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
    
            }
    

    Can you provide me with a working code or correct my usage of the current one ?

    Thanks!


  • Moderators

    @krokstr said in Android runtime permissons:

    Can you provide me with a working code or correct my usage of the current one ?

    Thanks!

    sure, from my current project

    #if defined (Q_OS_ANDROID)
    #include <QtAndroid>
    
    bool requestAndroidPermissions(){
        //Request requiered permissions at runtime
    
        const QVector<QString> permissions({"android.permission.ACCESS_COARSE_LOCATION",
                                            "android.permission.BLUETOOTH",
                                            "android.permission.BLUETOOTH_ADMIN",
                                            "android.permission.INTERNET",
                                            "android.permission.WRITE_EXTERNAL_STORAGE",
                                            "android.permission.READ_EXTERNAL_STORAGE"});
    
        for(const QString &permission : permissions){
            auto result = QtAndroid::checkPermission(permission);
            if(result == QtAndroid::PermissionResult::Denied){
                auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission}));
                if(resultHash[permission] == QtAndroid::PermissionResult::Denied)
                    return false;
            }
        }
    
        return true;
    }
    #endif
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
    #if defined (Q_OS_ANDROID)
        if(!requestAndroidPermissions())
            return -1;
    #endif
    .....
    }
    


  • @J-Hilk , Thanks for the fast answer and provided code.

    Just made few tries of calling the function at different places of the code, It keeps exiting with 0.
    If I understand right, this code is supposed to show user dialogues asking for permissions the app needs?

    I am testing it on Galaxy A8(2018) Android 8.0.0.


  • Moderators

    @krokstr
    did you also add that permissions to your android manifest ? AFAIK they also need to be in there, for you to be able to request them on runtime.



  • @J-Hilk ,
    Ok, they are now added. Both permissions for bluetooth and coarse location. Now the functions does not exit with 1. But It's not asking for the permissions (which I need to do (do I really need it?)) and it's not exiting with 0, when I manually turn off the only visible permission which is Location. The purpose of that function is to show dialogue, isn't it?


  • Moderators

    @krokstr what is your target SDK(API) version?
    the sdk needs to be set to one that supports runtime permission checks.
    24 I think is where that started.



  • @J-Hilk
    Thanks for the help, when I changed the Target SDK to API 26, everything came in place.
    Now the forum is having complete guide on how to start android runtime permissions step by step :D


Log in to reply