Qt 5.2 beta 1 - getting ANDROID_ID from the settings



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



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



  • 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>());
    @


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

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



  • @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 !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.