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. [SOLVED] [Android] [Qt 5.2] How to get activity object?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] [Android] [Qt 5.2] How to get activity object?

Scheduled Pinned Locked Moved Mobile and Embedded
5 Posts 5 Posters 7.7k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rom8726
    wrote on 19 Dec 2013, 04:50 last edited by
    #1

    I need to get main activity, and I tried:
    [code]
    QAndroidJniEnvironment jniEnv;
    jclass clsAct = jniEnv->FindClass("android/app/Activity");
    qDebug() << "Activity: " << clsAct;

    if( QAndroidJniObject::isClassAvailable("org/qtproject/qt5/android/bindings/QtActivity") )
    {
    jclass clsApp = jniEnv->FindClass("org/qtproject/qt5/android/bindings/QtActivity");
    qDebug() << "QtActivity: " << clsApp;

    if( clsApp && clsAct )
    {
        jobject objAct = NULL;
        jmethodID mid = jniEnv->GetStaticMethodID( clsApp, "activity", "()Landroid/app/Activity;" );
        qDebug() << "activityMID: " << mid;
    
        if( mid )
        {
            objAct = jniEnv->CallStaticObjectMethod( clsApp, mid );
            qDebug() << "activityObj: " << objAct;
        }
    }
    

    }
    [/code]
    But on jniEnv->FindClass("org/qtproject/qt5/android/bindings/QtActivity") shoots error:
    [code]
    D/Qt ( 4074): mainwindow.cpp:32 (MainWindow::MainWindow(QWidget*)): >>>>>> QtActivity: 0x0
    W/dalvikvm( 4074): JNI WARNING: JNI function CallStaticVoidMethodV called with exception pending
    W/dalvikvm( 4074): in Ldalvik/system/NativeStart;.run:()V (CallStaticVoidMethodV)
    W/dalvikvm( 4074): Pending exception is:
    I/dalvikvm( 4074): java.lang.NoClassDefFoundError: org/qtproject/qt5/android/bindings/QtActivity
    I/dalvikvm( 4074): at dalvik.system.NativeStart.run(Native Method)
    I/dalvikvm( 4074): Caused by:
    I/dalvikvm( 4074): java.lang.ClassNotFoundException: Didn't find class "org.qtproject.qt5.android.bindings.QtActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib]]
    [/code]

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ahmad88me
      wrote on 5 Jan 2014, 12:11 last edited by
      #2

      I couldn't use this approach, but what ever you want you can try to edit the generated java code

      1 Reply Last reply
      0
      • B Offline
        B Offline
        benlau
        Qt Champions 2016
        wrote on 6 Jan 2014, 10:25 last edited by
        #3

        Try this piece of code:

        @ QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
        @

        1 Reply Last reply
        0
        • T Offline
          T Offline
          T.Iotti
          wrote on 22 Apr 2014, 13:21 last edited by
          #4

          I've looked to a lot of posts explaining how to sort this problem, but the only one thing that worked was this:

          I was in QT 5.2:

          first:

          in your .pro:
          @QT += gui-private@

          now in your cpp:
          @

          #include <QAndroidJniObject>
          #include <qpa/qplatformnativeinterface.h>

          QAndroidJniObject getMainActivity()
          {
          QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
          QAndroidJniObject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");

          if(!activity.isValid())
          qDebug()<<"CLASS NOT VALID!!!!!!!!";
          else
          qDebug()<<"HORRAY!";

          return activity;
          }
          @

          I also like to thanks the MontezooM post which helped me to figure out

          _ 1 Reply Last reply 1 Mar 2023, 10:54
          0
          • T T.Iotti
            22 Apr 2014, 13:21

            I've looked to a lot of posts explaining how to sort this problem, but the only one thing that worked was this:

            I was in QT 5.2:

            first:

            in your .pro:
            @QT += gui-private@

            now in your cpp:
            @

            #include <QAndroidJniObject>
            #include <qpa/qplatformnativeinterface.h>

            QAndroidJniObject getMainActivity()
            {
            QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
            QAndroidJniObject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");

            if(!activity.isValid())
            qDebug()<<"CLASS NOT VALID!!!!!!!!";
            else
            qDebug()<<"HORRAY!";

            return activity;
            }
            @

            I also like to thanks the MontezooM post which helped me to figure out

            _ Offline
            _ Offline
            _______________-________________
            wrote on 1 Mar 2023, 10:54 last edited by
            #5

            @T-Iotti

            hi I know its quite old but maybe this will help someone like me stumbing across this : just call this funcion through jni

            static Activity getActivity() {
                       try {
                           Class activityThreadClass = Class.forName("android.app.ActivityThread");
                           Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
                           Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
                           activitiesField.setAccessible(true);
            
                           Map<Object, Object> activities = (Map<Object, Object>) activitiesField.get(activityThread);
                           if (activities == null)
                               return null;
            
                           for (Object activityRecord : activities.values()) {
                               Class activityRecordClass = activityRecord.getClass();
                               Field pausedField = activityRecordClass.getDeclaredField("paused");
                               pausedField.setAccessible(true);
                               if (!pausedField.getBoolean(activityRecord)) {
                                   Field activityField = activityRecordClass.getDeclaredField("activity");
                                   activityField.setAccessible(true);
                                   return (Activity) activityField.get(activityRecord);
                               }
                           }
            
                           return null;
                       }
                       catch (Exception e)
                       {
                           return null;
                       }
                   }
            
            
            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