Passing function pointer from native to java

Adrian Brown

Im using JNI to call some functions from a java library (JAR). One of these functions requires a listener which has some callbacks (success and error). The functions needed to handle these callbacks are in native code. Is is possible to create a listener with these native functions from native code and pass it via JNI?

The interface itself is not public but it works the same as the InApp Purchase stuff where you use the google helper to launch the purchase flow with:

mHelper.launchPurchaseFlow(activity, mItems[idx], idx, mPurchaseFinishedListener, "");

The mPurcahseFinishedListener would then be something like

`

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener()
{
    public void onIabPurchaseFinished(IabResult result, Purchase purchase)
    {
        Log.v("YER", "Its here");
    }
};

`

The idea is the onIabPurchaseFinished function is actually a native code function.

Stephen C

To start with, you cannot pass a native-code function pointer to Java in such a way that the Java side can call the function, directly.

But what you could do is map the function pointers to integer (or enum) tokens and return those to the Java world. Then you define and implement a native method for performing callbacks that takes a function pointer token as an argument. The method would reverse map the token to a function pointer, and then call the corresponding function to perform the callback action.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Passing pointer from java to native

From Dev

call a native function pointer from java?

From Dev

Passing a list of string as an argument to javascript function from native java code

From Dev

Passing a pointer from a function to another function

From Dev

Properly passing pointer from function C++

From Dev

Passing pointer/array to main() from a function

From Java

Passing a pointer from JNI to Java using a long

From Dev

Passing function from object to React Native ListView

From Dev

Passing a function pointer from an array of function pointers as a template argument

From Dev

Passing a pointer to a list, to a function

From Dev

Passing pointer to a function callback

From Dev

Passing pointer or array to function

From Dev

Passing a pointer as a function argument

From Dev

Passing a pointer to a function

From Dev

Passing pointer to function

From Dev

Passing function pointer in Dlang

From Dev

Passing state to function pointer

From Dev

Passing global pointer to function

From Dev

C - Passing pointer to function

From Dev

Getting pointer from native syscall in Java

From Dev

Passing arguments from a function to an internal function in Java?

From Dev

ANSI C - passing data from function to a CHAR pointer

From Dev

Trouble passing a function pointer to a method from main in C++

From Java

JNA passing the equivalent of swift pointer from Java Android to C

From Dev

Javascript addEventListener passing function pointer

From Dev

passing pointer to function and using realloc

From Dev

Passing char pointer as argument to function

From Dev

Passing a pointer to an array in a function in C

From

Passing capturing lambda as function pointer

Related Related

HotTag

Archive