Austris
Austris

Reputation: 61

Getting gps location updates from native/jni in Android?

I know that GPS location using Java can be obtained on change in the following way. I know that this excludes checking and asking for permissions, but for this example this is irrelevant. Full implementation in MainActivity.java you can find here link


LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        try {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
        } catch (SecurityException e) {
            e.printStackTrace();
        }

Listener itself

    private final LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            Toast.makeText(MainActivity.this, "Latitude: " + latitude + ", Longitude: " + longitude, Toast.LENGTH_SHORT).show();
        }
    };

The thing I want to achieve is to get the same location data from JNI native code. I know that from C++ I could find all java functions/classes, initialize, and use them. The only thing I don't know how, and there are limited examples on the internet.

What I know is that for example, I initialize a new object in the following way.

jclass cls = (*env)->FindClass(env, "java/lang/Integer");
jmethodID methodID = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
jobject a=(*env)->NewObject(env,cls, methodID, 5);

I would like to have some pointers on how to proceed with this and implement this GPS location tracking in JNI.

Upvotes: 1

Views: 225

Answers (0)

Related Questions