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

How can I call a non-static method of a java class from c ++?



  • Let's see, in an android application that I'm doing I'm trying to get the device IMEI. In my java class I have something like this:

    package cu.cibertools.regcharge.fetch;
    
    import android.provider.ContactsContract;
    import android.content.Context;
    import android.database.Cursor;
    import android.net.Uri;
    import android.content.Intent;
    import android.telephony.TelephonyManager;
    
    
    public class FetchClass extends org.qtproject.qt5.android.bindings.QtActivity
    {
    
        public FetchClass()
        {
        }
    
        public String getIMEI1()
        {
            String srvcName = Context.TELEPHONY_SERVICE;
            TelephonyManager phone = (TelephonyManager)getSystemService(srvcName);
            String deviceId = phone.getDeviceId();
            return deviceId;
        }
    
    }
    

    In my c ++ class I have:

    QString BackEnd::getIMEI()
    {
        bool avail = QAndroidJniObject::isClassAvailable("cu/cibertools/regcharge/fetch/FetchClass");
        QAndroidJniObject clase("cu/cibertools/regcharge/fetch/FetchClass");
        QString imei;
        QAndroidJniObject imeiCode;
    
        if (avail && clase.isValid())
        {
           imeiCode = clase.callObjectMethod("obtenerImei", "()Ljava/lang/String;");
           qDebug()<<"IMEI RETURN:"<<imeiCode.toString();
        }
        else
        {
           qDebug() << "IMEI NOT AVAILABLE";
        }
        return imeiCode.toString();
    
    }
    

    But in the debug messages I do not get anything ... the value of imeiCode.toString () is empty. In a moment I replaced all the contents of the function getIMEI () of the java class and placed only return "Some Text"; and nothing. Can you tell me what I'm doing wrong?



  • Researching I found this:

    QAndroidJniObject java("cu/cibertools/regcharge/fetch/FetchClass");
            QAndroidJniObject imeiNum = java.callMethod<jstring>("obtenerImei", "(V)Ljava/lang/String;");
            QString imei = imeiNum.toString();
    
            qDebug()<<"IMEI:"<<imei;
            return imei;
    

    but it gives me the following error.

    error: ld returned 1 exit status
    File not found: collect2.exe
    


  • I finally resolved. I replace:

    QAndroidJniObject java("cu/cibertools/regcharge/fetch/FetchClass");
            QAndroidJniObject imeiNum = java.callMethod<jstring>("obtenerImei", "(V)Ljava/lang/String;");
            QString imei = imeiNum.toString();
    
            qDebug()<<"IMEI:"<<imei;
            return imei;
    

    for

    QtAndroid::runOnAndroidThread([] {
            QAndroidJniObject java("cu/cibertools/regcharge/fetch/FetchClass");
            QAndroidJniObject imeiNum = java.callObjectMethod("obtenerImei", "(Landroid/content/Context;)Ljava/lang/String;",
                                                              QtAndroid::androidActivity().object<jobject>());
            QString imei = imeiNum.toString();
    
            qDebug()<<"IMEI:"<<imei;
            return imei;
        });
    

    And in java class:

    private String obtenerImei(Context c) {
    
            String imei = "352319065579474";
            TelephonyManager tm = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
            imei =tm.getDeviceId(); // Obtiene el imei  or  "352319065579474";
    
            if(imei == "")
                imei = "NO getDeviceId";
            return imei;
    
        }
    


  • Well ... I retract .. that only works sometimes ... can someone tell me what is going on?


  • Moderators

    @Blaster Is your QAndroidJniObject valid? What is the result of imeiNum.isValid()?



  • Yes, now is for real. I solved.

    QString imei;
    QtAndroid::runOnAndroidThreadSync([&imei] {
            QAndroidJniObject java("cu/cibertools/regcharge/fetch/FetchClass");
            QAndroidJniObject imeiNum = java.callObjectMethod("obtenerImei", "(Landroid/content/Context;)Ljava/lang/String;",
                                                              QtAndroid::androidActivity().object<jobject>());
            imei = imeiNum.toString();
        });
    return imei;
    

    I just change runOnAndroidThread for runOnAndroidThreadSync and is it;


Log in to reply