How to start an Android activity in Java file from Qt



  • Hi all,

    I need to start an activity which implements an interface from a third party library.

    So I am writing a sample application to start activity. But I have no clue how to do it. :( ..

    I have a button click slot:

    void MainWindow::on_startButton_clicked()
    {
        qDebug() << Q_FUNC_INFO << "Invoked";
        QAndroidJniObject intent;
        QtAndroid::startActivity(intent, 5, mpResultReceiver);
    }
    

    I don't know how to set intent so that it will refer to my activity.
    mpResultReceiver is a pointer to ActivityResultReceiver Object.

    //activityresultreceiver.h - I need to get some result from the activity...

    #ifndef ACTIVITYRESULTRECEIVER_H
    #define ACTIVITYRESULTRECEIVER_H
    
    #include <QAndroidActivityResultReceiver>
    
    class ActivityResultReceiver : public QAndroidActivityResultReceiver
    {
    public:
        ActivityResultReceiver();
    
        void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data);
    };
    
    #endif // ACTIVITYRESULTRECEIVER_H
    

    //activityresultreceiver.cpp

    #include "activityresultreceiver.h"
    #include <QDebug>
    
    ActivityResultReceiver::ActivityResultReceiver()
    {
    
    }
    
    void ActivityResultReceiver::handleActivityResult(int receiverRequestCode,
                                                      int resultCode,
                                                      const QAndroidJniObject &data)
    {
        qDebug() << Q_FUNC_INFO << "Invoked";
    }
    

    //ActivityClient.java -** in the path of androidmanifest.xml file path /src/org/abc/startActivity**

    package org.abc.startActivity;
    
    import android.util.Log;
    
    public class ActivityClient extends org.qtproject.qt5.android.bindings.QtActivity
    {
        private static ActivityClient m_instance;
    
        public ActivityClient()
        {
            m_instance = this;
        }
    
    //    @Override
    //    protected void onCreate(Bundle savedInstanceState) {
    //        super.onCreate(savedInstanceState);
    //        Log.d("ActivityClient", "on create");
    //    }
    
    }
    

    Can somebody help to figure this out, as this is not near to any good work


  • Qt Champions 2016

    I don't suggest to use JNI to initial an Activity. It is just too troublesome. Instead I will implement it in Java.

    e.g:

        public void takePhoto() {
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    
            mRequestedPhotoPath = "";
    
            String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss").format(new Date());
    
            File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    
            File image = new File(storageDir.getAbsolutePath() + "/" + timeStamp + ".jpg");
            i.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(image));
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            mRequestedPhotoPath = image.getAbsolutePath();
         
            startActivityForResult(i, TAKE_PHOTO);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (resultCode == RESULT_OK) {
                 if (requestCode == TAKE_PHOTO) {
                    if (data == null && mRequestedPhotoPath != "") {
                        importFromPath(mRequestedPhotoPath);
                        addImageFileToGallery(mRequestedPhotoPath);
                        mRequestedPhotoPath = "";
                    } else {
                        importMediaImage(data);
                    }
                }
            }
        }
    
    

    C++ side:

    void AndroidActivity::takePhoto()
    {
        QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
        activity.callMethod<void>("takePhoto","()V");
    }
    
    


  • @benlau Thanks for the post it helps .. :)

    But how to send if there is a data onActivityResult() have that is to be sent to C++ side...


  • Qt Champions 2016


Log in to reply
 

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