Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing java file in qt

I'm trying to execute a java file inside qt, here is my java file code:

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;

public class Main extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                    int[] appWidgetIds) {
            // TODO Auto-generated method stub
            super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

The question is how this java file can be called, I'm confused & don't know how to call the function onUpdate, Any ideas ?

like image 815
CodeMaster Avatar asked Jan 03 '15 23:01

CodeMaster


1 Answers

To run Java code in your Qt Android application you should use the Qt Android Extras module which contains additional functionality for development on Android.

You can use JNI to call a Java function from C/C++ or callback a C/C++ function from Java.

Let's consider you have a static Java method like :

package com.MyApp;

public class JavaClass
{
    public static int SomeMethod(int n)
    {
        ...
    }
}

First you need to add this to your .pro file :

QT += androidextras

And Include the relevant header file :

#include <QAndroidJniObject>

You can then call a static java function from your C++ code like :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/JavaClass" // class name
                        , "SomeMethod" // method name
                        , "(I)I" // signature
                        , val);

For a more detailed explanation you can see this.

like image 125
Nejat Avatar answered Oct 20 '22 19:10

Nejat