anonymous
anonymous

Reputation: 35

How to invoke Native Interface from StateMachineBase.java and send back some values from the Native Implementation using Codename One?

I am a newbie to Codename One and I am trying to invoke Native Interface from StateMachine.java and get some values back from the Native Interface Implementation. How do I do it?

Here are the codes of StateMachine.java and the Native Interface Implementation, NativeAndroidImpl.java

I want to call NativeAndroidImpl.java from StateMachine.java and get the longitude and latitude values from NativeAndroidImpl.java

----------------------------StateMachine.java----------------------------



@Override
protected void onMain_MainHelpButtonAction(Component c, ActionEvent event) {

    Vector<String> vec = (Vector<String>)Storage.getInstance().readObject("SavedData");

//Invoke NativeAndroidImpl.java

double lat=//get latitude from NativeAndroidImpl.java
double lng=//get longitude from NativeAndroidImpl.java
try {   
    Display.getInstance().sendSMS(vec.elementAt(2), "I am in trouble. Send HELP! My location: http://maps.google.com/?q=" + lat + "," + lng, true);
        } catch (IOException ex) {
            Dialog.show("Error!", "Failed to send SMS.", "OK", null);
            ex.printStackTrace();
    }
  }
}


------------------------NativeAndroidImpl.java--------------------------


package com.anonymous.emergencyhelp;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class NativeAndroidImpl {
  LocationManager locationManager;
  double longitudeNetwork, latitudeNetwork;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
}

private boolean checkLocation() {
    if(!isLocationEnabled())
        showAlert();
    return isLocationEnabled();
}

private void showAlert() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("Enable Location")
            .setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to use this app")
            .setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(myIntent);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                }
            });
    dialog.show();
}

private boolean isLocationEnabled() {
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

public void toggleNetworkUpdates(View view) {
    if(!checkLocation())
        return;
    Button button = (Button) view;
    if(button.getText().equals("Help"))
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60 * 1000, 10, locationListenerNetwork);
}


private final LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        longitudeNetwork = location.getLongitude();
        latitudeNetwork = location.getLatitude();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                //Send longitude and latitude values to StateMachine.java

                Toast.makeText(MainActivity.this, "Network Provider update", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
};


  public boolean isSupported() {
    return true;
  }

}

Upvotes: 1

Views: 43

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

Check out this tutorial for using native interfaces.

Notice that you don't need native interfaces for location since a cross platform location API exists in Codename One. I suggest checking out the developer guide and tutorials for Codename One.

Upvotes: 1

Related Questions