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. How to request the permissions in runtime

How to request the permissions in runtime

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
17 Posts 8 Posters 12.1k 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.
  • BlasterB Offline
    BlasterB Offline
    Blaster
    wrote on last edited by
    #1

    I am trying to develop an app, but I can not find the way to request the runtime permissions on android 6. Can someone help me?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

      And be careful with the callback - in my experience it does not work. So if you want the full solutions, with checking callback results, use Java.

      (Z(:^

      BlasterB J.HilkJ 2 Replies Last reply
      3
      • sierdzioS sierdzio

        Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

        And be careful with the callback - in my experience it does not work. So if you want the full solutions, with checking callback results, use Java.

        BlasterB Offline
        BlasterB Offline
        Blaster
        wrote on last edited by
        #3

        @sierdzio How do I do it with java? I tried some codes but the application hangs

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          In your main activity, in onCreate() function, you can plug the code from Android docs: https://developer.android.com/training/permissions/requesting#java

          Once you get that working (you can skip the step with shouldShowRequestPermissionRationale initially), you can then transfer that to some other methods which you'd call dynamically, from your app, via JNI.

          (Z(:^

          BlasterB 1 Reply Last reply
          3
          • sierdzioS sierdzio

            In your main activity, in onCreate() function, you can plug the code from Android docs: https://developer.android.com/training/permissions/requesting#java

            Once you get that working (you can skip the step with shouldShowRequestPermissionRationale initially), you can then transfer that to some other methods which you'd call dynamically, from your app, via JNI.

            BlasterB Offline
            BlasterB Offline
            Blaster
            wrote on last edited by
            #5

            @sierdzio Thanks, I will tray

            1 Reply Last reply
            0
            • sierdzioS sierdzio

              Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

              And be careful with the callback - in my experience it does not work. So if you want the full solutions, with checking callback results, use Java.

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

              @sierdzio said in How to request the permissions in runtime:

              Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

              Are you sure?
              I'm using the Sync version in my application, for camera access, and it has jet to fail. I request it before I create the ui, because my app can't work without it.

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
              
                  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;
                  }
              
                 MainWindow w;
                 return a.exec();
              }
              

              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.

              sierdzioS 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @sierdzio said in How to request the permissions in runtime:

                Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

                Are you sure?
                I'm using the Sync version in my application, for camera access, and it has jet to fail. I request it before I create the ui, because my app can't work without it.

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                
                    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;
                    }
                
                   MainWindow w;
                   return a.exec();
                }
                
                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                @J.Hilk said in How to request the permissions in runtime:

                @sierdzio said in How to request the permissions in runtime:

                Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

                Are you sure?
                I'm using the Sync version in my application, for camera access, and it has jet to fail. I request it before I create the ui, because my app can't work without it.

                Yes, tested with Qt 5.10, 5.11. It will hand your app if you request coarse location permission. Although very probably it is a bug in QtBluetooth, not in QAndroidExtras.

                (Z(:^

                J.HilkJ 1 Reply Last reply
                0
                • sierdzioS sierdzio

                  @J.Hilk said in How to request the permissions in runtime:

                  @sierdzio said in How to request the permissions in runtime:

                  Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

                  Are you sure?
                  I'm using the Sync version in my application, for camera access, and it has jet to fail. I request it before I create the ui, because my app can't work without it.

                  Yes, tested with Qt 5.10, 5.11. It will hand your app if you request coarse location permission. Although very probably it is a bug in QtBluetooth, not in QAndroidExtras.

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

                  @sierdzio said in How to request the permissions in runtime:

                  @J.Hilk said in How to request the permissions in runtime:

                  @sierdzio said in How to request the permissions in runtime:

                  Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

                  Are you sure?
                  I'm using the Sync version in my application, for camera access, and it has jet to fail. I request it before I create the ui, because my app can't work without it.

                  Yes, tested with Qt 5.10, 5.11. It will hand your app if you request coarse location permission. Although very probably it is a bug in QtBluetooth, not in QAndroidExtras.

                  Is that a repeatable error?

                  Because I'm using Bluetooth LE in my application and I'm also requesting ACCESS_COARSE_LOCATION permission during runtime.

                  And it's working every time. This is done with the latest Qt 5.11.2

                  Here is the corresponding source code snippet

                  #if defined (Q_OS_ANDROID)
                  #include <QtAndroid>
                  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"});
                  #endif
                  
                  int main(int argc, char *argv[])
                  {
                      qRegisterMetaType<QVector<quint16> >("QVector<quint16>");
                      qRegisterMetaType<Datagram>();
                      qRegisterMetaType<QLowEnergyDescriptor>();
                      qRegisterMetaType<QLowEnergyCharacteristic>();
                      qRegisterMetaType<TerminalIO::serviceErrors>("serviceErrors");
                      qRegisterMetaType<uint32_t>("uint32_t");
                      qRegisterMetaType<int32_t>("int32_t");
                      qRegisterMetaType<uint8_t>("uint8_t");
                  
                  #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
                      qRegisterMetaType<QModbusDevice::Error>("Error");
                  #endif
                  
                      QGuiApplication app(argc, argv);
                  
                  #if defined (Q_OS_ANDROID)
                      //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;
                          }
                      }
                  #endif
                  
                  .... 
                  }
                  

                  My test hardware is a Samsung Galaxi tablet with android 7 on it.


                  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.

                  P E 2 Replies Last reply
                  2
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    Hm, maybe it got fixed in 5.11.2, good to know.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @sierdzio said in How to request the permissions in runtime:

                      @J.Hilk said in How to request the permissions in runtime:

                      @sierdzio said in How to request the permissions in runtime:

                      Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

                      Are you sure?
                      I'm using the Sync version in my application, for camera access, and it has jet to fail. I request it before I create the ui, because my app can't work without it.

                      Yes, tested with Qt 5.10, 5.11. It will hand your app if you request coarse location permission. Although very probably it is a bug in QtBluetooth, not in QAndroidExtras.

                      Is that a repeatable error?

                      Because I'm using Bluetooth LE in my application and I'm also requesting ACCESS_COARSE_LOCATION permission during runtime.

                      And it's working every time. This is done with the latest Qt 5.11.2

                      Here is the corresponding source code snippet

                      #if defined (Q_OS_ANDROID)
                      #include <QtAndroid>
                      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"});
                      #endif
                      
                      int main(int argc, char *argv[])
                      {
                          qRegisterMetaType<QVector<quint16> >("QVector<quint16>");
                          qRegisterMetaType<Datagram>();
                          qRegisterMetaType<QLowEnergyDescriptor>();
                          qRegisterMetaType<QLowEnergyCharacteristic>();
                          qRegisterMetaType<TerminalIO::serviceErrors>("serviceErrors");
                          qRegisterMetaType<uint32_t>("uint32_t");
                          qRegisterMetaType<int32_t>("int32_t");
                          qRegisterMetaType<uint8_t>("uint8_t");
                      
                      #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
                          qRegisterMetaType<QModbusDevice::Error>("Error");
                      #endif
                      
                          QGuiApplication app(argc, argv);
                      
                      #if defined (Q_OS_ANDROID)
                          //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;
                              }
                          }
                      #endif
                      
                      .... 
                      }
                      

                      My test hardware is a Samsung Galaxi tablet with android 7 on it.

                      P Offline
                      P Offline
                      PSI_lbc
                      wrote on last edited by
                      #10

                      @J.Hilk

                      Thanks for the code snippet.

                      Haven't tried it yet but it appears to check the permissions established in the manifest(?)..if the permission has been granted, it continues with the next permission in the list..if the permission was not granted or has been changed to NOT granted, the user is asked to grant permission..program exits if needed permission is not granted.

                      Questions..

                      Is the const QVector<QString> permissions declaration the same list of permissions as has been declared in the AndroidManifest.xml file?

                      Is the app responsible for maintaining the Granted/Not Granted status or is that something done by the Android OS in the app Settings?

                      Does the QtAndroid::requestPermissionsSync function pop a dialog that queries the user for permission or does it just check the permissions as set in App Settings but the OS?

                      Sorry for the questions. The Android docs aren't that clear and this is the first thread I've found that gives an example..

                      J.HilkJ 1 Reply Last reply
                      0
                      • P PSI_lbc

                        @J.Hilk

                        Thanks for the code snippet.

                        Haven't tried it yet but it appears to check the permissions established in the manifest(?)..if the permission has been granted, it continues with the next permission in the list..if the permission was not granted or has been changed to NOT granted, the user is asked to grant permission..program exits if needed permission is not granted.

                        Questions..

                        Is the const QVector<QString> permissions declaration the same list of permissions as has been declared in the AndroidManifest.xml file?

                        Is the app responsible for maintaining the Granted/Not Granted status or is that something done by the Android OS in the app Settings?

                        Does the QtAndroid::requestPermissionsSync function pop a dialog that queries the user for permission or does it just check the permissions as set in App Settings but the OS?

                        Sorry for the questions. The Android docs aren't that clear and this is the first thread I've found that gives an example..

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

                        @PSI_lbc said in How to request the permissions in runtime:

                        @J.Hilk

                        Thanks for the code snippet.

                        I'm happy to help ;-)

                        Questions..

                        Is the const QVector<QString> permissions declaration the same list of permissions as has been declared in the AndroidManifest.xml file?

                        In this example, yes it is the same list as in the manifest file.

                        Is the app responsible for maintaining the Granted/Not Granted status or is that something done by the Android OS in the app Settings?

                        You have to check it each time you want to do an operation that requires on of the special permission. Since on of the later Android versions, not sure which one, the user can at any time revoke granted permissions. So you will have to check it regularly/before each use.

                        Does the QtAndroid::requestPermissionsSync function pop a dialog that queries the user for permission or does it just check the permissions as set in App Settings but the OS?

                        Both, it checks it first, and if it's not granted makes a standard popup message box, asking for the permission.

                        Sorry for the questions. The Android docs aren't that clear and this is the first thread I've found that gives an example..

                        No worries, but for future references, I would suggest creating a new topic rather than waking such an old one from its slumber.


                        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.

                        T 1 Reply Last reply
                        1
                        • J.HilkJ J.Hilk

                          @sierdzio said in How to request the permissions in runtime:

                          @J.Hilk said in How to request the permissions in runtime:

                          @sierdzio said in How to request the permissions in runtime:

                          Use QtAndroid::requestPermissions() https://doc.qt.io/qt-5/qtandroid.html#requestPermissions. If possible, don't use the "sync" version, it can hang your app.

                          Are you sure?
                          I'm using the Sync version in my application, for camera access, and it has jet to fail. I request it before I create the ui, because my app can't work without it.

                          Yes, tested with Qt 5.10, 5.11. It will hand your app if you request coarse location permission. Although very probably it is a bug in QtBluetooth, not in QAndroidExtras.

                          Is that a repeatable error?

                          Because I'm using Bluetooth LE in my application and I'm also requesting ACCESS_COARSE_LOCATION permission during runtime.

                          And it's working every time. This is done with the latest Qt 5.11.2

                          Here is the corresponding source code snippet

                          #if defined (Q_OS_ANDROID)
                          #include <QtAndroid>
                          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"});
                          #endif
                          
                          int main(int argc, char *argv[])
                          {
                              qRegisterMetaType<QVector<quint16> >("QVector<quint16>");
                              qRegisterMetaType<Datagram>();
                              qRegisterMetaType<QLowEnergyDescriptor>();
                              qRegisterMetaType<QLowEnergyCharacteristic>();
                              qRegisterMetaType<TerminalIO::serviceErrors>("serviceErrors");
                              qRegisterMetaType<uint32_t>("uint32_t");
                              qRegisterMetaType<int32_t>("int32_t");
                              qRegisterMetaType<uint8_t>("uint8_t");
                          
                          #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
                              qRegisterMetaType<QModbusDevice::Error>("Error");
                          #endif
                          
                              QGuiApplication app(argc, argv);
                          
                          #if defined (Q_OS_ANDROID)
                              //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;
                                  }
                              }
                          #endif
                          
                          .... 
                          }
                          

                          My test hardware is a Samsung Galaxi tablet with android 7 on it.

                          E Offline
                          E Offline
                          eLim2g
                          wrote on last edited by
                          #12

                          @j-hilk said in How to request the permissions in runtime:

                          Is that a repeatable error?

                          Because I'm using Bluetooth LE in my application and I'm also requesting ACCESS_COARSE_LOCATION permission during runtime.

                          And it's working every time. This is done with the latest Qt 5.11.2

                          Sorry to necro an old thread. I can reproduce this error on the full 5.12.x line of kits. However, if I request the permission before I load my first QML file (ie before I create an instance of QQmlApplicationEngine) then QtAndroid::requestPermissionsSync seems to work perfectly fine for me.

                          J.HilkJ 1 Reply Last reply
                          0
                          • E eLim2g

                            @j-hilk said in How to request the permissions in runtime:

                            Is that a repeatable error?

                            Because I'm using Bluetooth LE in my application and I'm also requesting ACCESS_COARSE_LOCATION permission during runtime.

                            And it's working every time. This is done with the latest Qt 5.11.2

                            Sorry to necro an old thread. I can reproduce this error on the full 5.12.x line of kits. However, if I request the permission before I load my first QML file (ie before I create an instance of QQmlApplicationEngine) then QtAndroid::requestPermissionsSync seems to work perfectly fine for me.

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

                            @elim2g mmh, interesting,
                            do you have a minimal compile-able example we could test?


                            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
                            0
                            • J.HilkJ J.Hilk

                              @PSI_lbc said in How to request the permissions in runtime:

                              @J.Hilk

                              Thanks for the code snippet.

                              I'm happy to help ;-)

                              Questions..

                              Is the const QVector<QString> permissions declaration the same list of permissions as has been declared in the AndroidManifest.xml file?

                              In this example, yes it is the same list as in the manifest file.

                              Is the app responsible for maintaining the Granted/Not Granted status or is that something done by the Android OS in the app Settings?

                              You have to check it each time you want to do an operation that requires on of the special permission. Since on of the later Android versions, not sure which one, the user can at any time revoke granted permissions. So you will have to check it regularly/before each use.

                              Does the QtAndroid::requestPermissionsSync function pop a dialog that queries the user for permission or does it just check the permissions as set in App Settings but the OS?

                              Both, it checks it first, and if it's not granted makes a standard popup message box, asking for the permission.

                              Sorry for the questions. The Android docs aren't that clear and this is the first thread I've found that gives an example..

                              No worries, but for future references, I would suggest creating a new topic rather than waking such an old one from its slumber.

                              T Offline
                              T Offline
                              Taher Lkdw
                              wrote on last edited by
                              #14

                              @J-Hilk Hello, Sorry for restarting old topic.

                              If permission was asked using requestPermissionsSync method and user denies it, second time onwards requestPermissionsSync doesn't show any popup at all.

                              What to do for asking permission again if user has denied it the first time.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Hi,

                                Ask him to go to the settings and enable them.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #16

                                  these permissions are only valid for android how can we do this job on ios

                                  sierdzioS 1 Reply Last reply
                                  0
                                  • ? A Former User

                                    these permissions are only valid for android how can we do this job on ios

                                    sierdzioS Offline
                                    sierdzioS Offline
                                    sierdzio
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    @NullByte said in How to request the permissions in runtime:

                                    these permissions are only valid for android how can we do this job on ios

                                    You need to use native code for this.

                                    (Z(:^

                                    1 Reply Last reply
                                    0

                                    • Login

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