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. Qt 5.2 beta 1 - getting ANDROID_ID from the settings

Qt 5.2 beta 1 - getting ANDROID_ID from the settings

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 4 Posters 4.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.
  • T Offline
    T Offline
    thierry
    wrote on last edited by A Former User
    #1

    Hi all,

    I'm trying to get the ANDROID_ID from the settings through the JNI. You're supposed to be able to get it via the following code:

    @QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid.app.Activity;");
    QAndroidJniObject result = QAndroidJniObject::callStaticObjectMethod("android/provider/Settings/Secure",
                                                     "getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;",
                                                     activity.object<jobject>(), "android_id");
    

    @
    The problem is that it seems it can't find android/provider/Settings/Secure. It fails with a class not found exception.

    Has someone done this before?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thierry
      wrote on last edited by
      #2

      The reason is pretty simple: Secure is an inner class. so it should be used as "android/provider/Settings$Secure"

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maour
        wrote on last edited by
        #3

        This code works for me, thanks to <eskil_> <cstr> #necessitas

        @
        QAndroidJniObject myID = QAndroidJniObject::fromString("android_id");
        QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
        QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
        QAndroidJniObject contentR = appctx.callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
        QAndroidJniObject result = QAndroidJniObject::callStaticObjectMethod("android/provider/Settings$Secure","getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;",contentR.object<jobject>(), myID.object<jstring>());
        @

        L 1 Reply Last reply
        0
        • benlauB Offline
          benlauB Offline
          benlau
          Qt Champions 2016
          wrote on last edited by
          #4

          Hi Maour,

          I have tried your code in main() but it throw error in execution :

          @
          F/libc (28452): Fatal signal 11 (SIGSEGV) at 0x0000002c (code=1), thread 28494 (xample.delegate)
          @

          Caused by :

          @
          QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
          @

          p.s activity.isValid() return false.

          Do I need to setup anything before execute your code?

          Thanks.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maour
            wrote on last edited by
            #5

            Hi Benlau,

            There isn't anything special about that code, just create a new project and put that code on main(), i don't know about your error but activity.isValid() should return true.

            Can you execute androidextras example from Qt example directory?

            1 Reply Last reply
            0
            • benlauB Offline
              benlauB Offline
              benlau
              Qt Champions 2016
              wrote on last edited by
              #6

              HI maour,

              Thanks for your reply. Just tried the androidextras , it is working. Then I switch back to original project , I found it is also working too. It is rather strange..

              1 Reply Last reply
              0
              • M maour

                This code works for me, thanks to <eskil_> <cstr> #necessitas

                @
                QAndroidJniObject myID = QAndroidJniObject::fromString("android_id");
                QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
                QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
                QAndroidJniObject contentR = appctx.callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
                QAndroidJniObject result = QAndroidJniObject::callStaticObjectMethod("android/provider/Settings$Secure","getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;",contentR.object<jobject>(), myID.object<jstring>());
                @

                L Offline
                L Offline
                luca
                wrote on last edited by
                #7

                @maour said in [RESOLVED] Qt 5.2 beta 1 - getting ANDROID_ID from the settings:

                This code works for me, thanks to <eskil_> <cstr> #necessitas

                @
                QAndroidJniObject myID = QAndroidJniObject::fromString("android_id");
                QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
                QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
                QAndroidJniObject contentR = appctx.callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
                QAndroidJniObject result = QAndroidJniObject::callStaticObjectMethod("android/provider/Settings$Secure","getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;",contentR.object<jobject>(), myID.object<jstring>());
                @

                Just to thanks for that code !

                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