Instantly share code, notes, and snippets.

Embed
What would you like to do?
Class to get calls from C++
// Usage
//
// 1º In MainActivity class, onCreate method, add this line:
// InterfaceFromCJNI.callbackMainActivity = this;
// 2º Add this function, defined as interface in InterfaceFromCJNI class:
// @Override
// public void msgFromC(String message)
// {
// // Your own
// Log.d(LOG_TAG, "msgFromC is: " + message);
// }
// Java package
package com.github.vgoni;
// Imports
import android.util.Log;
public final class InterfaceFromCJNI
{
// Callback for activities. Must be initialized onCreate Activity
static public MainActivity callbackMainActivity;
// Function in Activity to call
interface InterfaceMainActivity
{
void msgFromC(String message);
}
// This function is called from JNI
static public void receivedMsgFromC(String message)
{
Log.d(LOG_TAG, "receivedMsgFromC");
callbackMainActivity.runOnUiThread(new Runnable()
{
public void run()
{
// Notify with Toast
Toast.makeText(callbackMainActivity.getApplicationContext(), message, Toast.LENGTH_LONG).show();
callbackMainActivity.msgFromC(message);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment