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

Problems with QAndroidActivityResultReceiver



  • Hello, I am trying to capture the result of choosing a contact, but the handleActivityResult () method does not finish the job. It starts correctly, but when trying to access the method proccessData () stops completely at runOnAndroidThread().

    The redefinition of the handleActivityResult () method looks like this:

    void MyReciever::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
    {
        jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
        if(receiverRequestCode == 1 && resultCode == RESULT_OK)
        {
            QAndroidJniObject valor;
            qDebug()<<"HANDLEACTIVITYRESULT  RESULT_OK && RECUESTCODE";
            QtAndroid::runOnAndroidThread([&data, &valor] {
                qDebug()<<"HANDLEACTIVITYRESULT ENTER THREAD SYNC";
                QAndroidJniObject java("cu/cibertools/regcharge/fetch/FetchClass");
                valor = java.callObjectMethod("proccessData", "(Landroid/content/Intent;)Ljava/lang/String;",
                                                                  data.object<jobject>());
                qDebug()<<"HANDLEACTIVITYRESULT DATA PROCCESSED";
            });
            QString cad = valor.toString();
    
            if(!cad.isEmpty())
            {
                QStringList list = cad.split(";", QString::SkipEmptyParts);
                m_contactName = list.at(0);
                m_contactNumber = list.at(1);
                qDebug()<<"m_contactName:"<<m_contactName<<"m_contactNumber:"<<m_contactNumber;
                emit this->contactNumberChanged();
                emit this->contactNameChanged();
            }
            else
            {
                qDebug()<<"RETURN STRING IS EMPTY";
            }
    
        }
    }
    

    The proccessData() method is this:

    public String proccessData(Intent data)
        {
            Cursor cursor = null;
            String ret = "PROCCESSDATA RETORNO";
                    String phoneNo = null ;
                    String name = null;
                    // getData() method will have the Content Uri of the selected contact
                    Uri uri = data.getData();
                    //Query the content uri
                    cursor = getContentResolver().query(uri, null, null, null, null);
                    cursor.moveToFirst();
                    // column index of the phone number
                    int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    // column index of the contact name
                    int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    // Getting values from cursor
                    phoneNo = cursor.getString(phoneIndex);
                    name = cursor.getString(nameIndex);
                    // Set the value to the textviews
                    ret = name + ";" + phoneNo;
            return ret;
        }
    

    Can someone tell me what I should change? It's wrong?



  • I already resolved it.
    In my handleActivityResult():

    void MyReciever::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
    {
        jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
    //    qDebug()<<"HANDLEACTIVITYRESULT ISMAEL";
        if(receiverRequestCode == 1 && resultCode == RESULT_OK)
        {
                        QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver",
                                                                "()Landroid/content/ContentResolver;");
            QAndroidJniObject ret = QAndroidJniObject::callStaticObjectMethod("cu/cibertools/regcharge/fetch/FetchClass",
                                                                              "dataProccess",
                                                                              "(Landroid/content/Intent;Landroid/content/ContentResolver;)Ljava/lang/String;",
                                                                              data.object<jobject>(), contentResolver.object<jobject>());
            if(ret.isValid())
            {
                QString cad = ret.toString();
    //            qDebug()<<"RETORNO"<<cad<<"FIN RETORNO";
                   if(!cad.isEmpty())
                   {
                       QStringList list = cad.split(";", QString::SkipEmptyParts);
                       m_contactName = list.at(0);
                       m_contactNumber = list.at(1);
    //                   qDebug()<<"m_contactName:"<<m_contactName<<"m_contactNumber:"<<m_contactNumber;
                       emit this->contactNumberChanged();
                       emit this->contactNameChanged();
                   }
            }
        }
    }
    

    In my java class:

    public static String dataProccess(Intent data, ContentResolver resolver)
    {
        Cursor cursor = null;
        String retorno = null;
        String phoneNo = null;
        String name = null;
    
        Uri uri = data.getData();
    
        cursor = resolver.query(uri, null, null, null, null);
    
        cursor.moveToFirst();
    
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    
        phoneNo = cursor.getString(phoneIndex);
    
        name = cursor.getString(nameIndex);
    
        retorno = name + ";" + phoneNo;
    
        cursor.close();
    
        return retorno;
    }
    

Log in to reply