Last active
February 25, 2016 09:05
-
-
Save vgonisanz/52b0ff432855833551a7 to your computer and use it in GitHub Desktop.
Class to get calls from C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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