Sending Accelerometer to C/C++
I got the following code inside my templateApp.java:
package com.android.templateApp;
import android.app.Activity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class templateApp extends Activity implements SensorEventListener
{
GL2View mView;
SensorManager mSensorManager;
@Override protected void onCreate(Bundle icicle)
{
super.onCreate( icicle );
mSensorManager = ( SensorManager ) getSystemService( SENSOR_SERVICE );
mView = new GL2View( getApplication() );
mSensorManager.registerListener( this,
mSensorManager.getDefaultSensor( SensorManager.SENSOR_ACCELEROMETER ),
SensorManager.SENSOR_DELAY_GAME );
setContentView( mView );
}
@Override protected void onResume()
{
super.onResume();
mView.onResume();
}
public static native void Accelerometer( float x, float y, float z );
public void onSensorChanged( SensorEvent event )
{
float x = event.values[ SensorManager.DATA_X ],
y = event.values[ SensorManager.DATA_Y ],
z = event.values[ SensorManager.DATA_Z ];
// ALWAYS CRASH HERE!!!!
Accelerometer( x, y, z );
}
public void onAccuracyChanged( Sensor sensor, int arg1 ) {}
}
And inside my templateApp.cpp I got the following to bridge the Accelerometer function:
extern "C"
{
JNIEXPORT void JNICALL Java_com_android_templateApp_GL2View_Init( JNIEnv *env, jobject obj, jint width, jint height );
JNIEXPORT void JNICALL Java_com_android_templateApp_templateApp_Accelerometer( JNIEnv *env, jfloat x, jfloat y, jfloat z );
};
JNIEXPORT void JNICALL Java_com_android_templateApp_GL2View_Init( JNIEnv *env, jobject obj, jint width, jint height )
{ Init( width, height); }
JNIEXPORT void JNICALL Java_com_android_templateApp_templateApp_Accelerometer( JNIEnv *env, jfloat x, jfloat y, jfloat z )
{ Accelerometer( x, y,z ); }
When I开发者_StackOverflow中文版 pass my mouse over in eclipse it show that the call belong to com.android.templateApp.templateApp.Accelerometer, which is exactly what I declare, however it always crash reporting an UnsatisfiedLinkError.
I tried every combination (assuming that my native definition is wrong) but it keep crashing at the Accelerometer call inside the java code... Weird thing is that my init function that is inside my GL2View.java work flawless. Is there anything that prevent me to call a native API from the main file that extends Activity? Cuz the sensor work, I print the value its the native call that crash and I don't know why.
Tks in advance!
Have you initiated your native code? This is mine, it located in the class with native methods exposed.
static {
Log.v("NativeCodeWrapper.java", "Loading native libraries");
System.loadLibrary("NameOfNativeModuleAsDefinedInAndroidDotMk");
}
You forgot to add implicit jclass/jobject argument to your JNI function:
JNIEXPORT void JNICALL Java_com_android_templateApp_templateApp_Accelerometer( JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z );
'clazz' argument will be the Java class of the calling class (templateApp in your case). For non-static functions implicit argument will be jobject of the calling object.
精彩评论