Skip to content

Instantly share code, notes, and snippets.

@vgonisanz
Last active February 25, 2016 09:05
Show Gist options
  • Save vgonisanz/52b0ff432855833551a7 to your computer and use it in GitHub Desktop.
Save vgonisanz/52b0ff432855833551a7 to your computer and use it in GitHub Desktop.
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