I’ve been doing a bit of research for the last couple of week on how to create a native extension in Haxe 3/OpenFL with the geolocation/gps.
I thought first that maybe at this point in the framework, someone should have created native extensions for the hardware related and basic stuff such as camera, gallery, geolocation and accelerometer (I know that for accelerometer there is already code in NME and OpenFL). But since it seems that it does not exist, I decided to create it.
Hard to find proper documentation on how to create one, I stumbled upon the command openfl create extension TestExtension and it works, it creates a template for you.
The code works and the static method provided returns its int value to the OpenFL side of the app. So my next step was to search for the Android code that returns the latitude and longitude. I tested the code with a plain Android app and it works, copied it into the native extension, and after looking how to, I changed the reference related to the main activity with the one provided in the comment on top of the java class (Extension.mainActivity). And here is the problem, the app crashes, so bad that it exists the app inmediately. I thought it was maybe the way I was handling the interface methods with the macros and JNI, but later on I removed all the geolocation related code from the Java class, and the interface worked.
So I guess, it’s something incredibly simple I’m missing to see, some kind of event such as the onCreate to wait for the main activity to load, but I’ve tried that also and didn’t work.
For the record, I thougth it might be the Android permissions, but they are not, the Manifest has the permissions for ACCESS_FINE_LOCATION and/or ACCESS_COARSE_LOCATION and the app still crashes.
Java code:
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
_longitude = String.valueOf(location.getLongitude());
_latitude = String.valueOf(location.getLatitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
This next part I’ve called it on onCreate or for testing, in a button click. In the original Java code Extension.mainActivity is simply “this”
locationManager = (LocationManager) Extension.mainActivity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, locationListener);
My goal is to publish the extension to Github after I’ve finished,I just need someone to point me into the right direction.
Thanks