Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Android runtime permissons
Forum Updated to NodeBB v4.3 + New Features

Android runtime permissons

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krokstr
    wrote on last edited by
    #1

    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!

    J.HilkJ 1 Reply Last reply
    0
    • K krokstr

      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!

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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
      .....
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • K Offline
        K Offline
        krokstr
        wrote on last edited by
        #3

        @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.

        J.HilkJ 1 Reply Last reply
        0
        • K krokstr

          @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.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @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.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • K Offline
            K Offline
            krokstr
            wrote on last edited by
            #5

            @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?

            J.HilkJ 1 Reply Last reply
            0
            • K krokstr

              @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?

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • K Offline
                K Offline
                krokstr
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved