如何从StateMachineBase.java调用本机接口,并使用Codename One从本机实现发回一些值?

如何从StateMachineBase.java调用本机接口,并使用Codename One从本机实现发回一些值?,java,android,codenameone,Java,Android,Codenameone,我是一个代号为One的新手,我正在尝试从StateMachine.java调用本机接口,并从本机接口实现中获取一些值。我该怎么做 下面是StateMachine.java和本机接口实现NativeAndroidImpl.java的代码 ----------------------------StateMachine.java---------------------------- @Override protected void onMain_MainHelpButtonAction(Co

我是一个代号为One的新手,我正在尝试从StateMachine.java调用本机接口,并从本机接口实现中获取一些值。我该怎么做

下面是StateMachine.java和本机接口实现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;
  }

}
我想从StateMachine.java调用NativeAndroidImpl.java,并从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;
  }

}
------------------------------------StateMachine.java----------------------------
@凌驾
Main_MainHelpButtonAction上的受保护无效(组件c,ActionEvent事件){
Vector vec=(Vector)Storage.getInstance().readObject(“SavedData”);
//调用NativeAndroidImpl.java
double lat=//从NativeAndroidImpl.java获取纬度
double lng=//从NativeAndroidImpl.java获取经度
试试{
Display.getInstance().sendSMS(vec.elementAt(2),“我遇到麻烦了。发送帮助!我的位置:http://maps.google.com/?q=“+lat+”,“+lng,正确);
}捕获(IOEX异常){
显示(“错误!”,“发送短信失败”,“确定”,空);
例如printStackTrace();
}
}
}
------------------------NativeAndroidImpl.java--------------------------
包com.anonymous.emergencyhelp;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.provider.Settings;
导入android.support.v7.app.AlertDialog;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.Button;
导入android.widget.Toast;
公共类本地androidimpl{
地点经理地点经理;
双纵向网络、横向网络;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
}
私有布尔checkLocation(){
如果(!isLocationEnabled())
showarert();
返回isLocationEnabled();
}
私有void showAlert(){
最终AlertDialog.Builder对话框=新建AlertDialog.Builder(此);
对话框.setTitle(“启用位置”)
.setMessage(“您的位置设置已设置为“关闭”。\n请启用位置以使用此应用”)
.setPositiveButton(“位置设置”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface paraloginterface,int paramInt){
Intent myIntent=新意图(设置、操作、位置、源、设置);
星触觉(myIntent);
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface paraloginterface,int paramInt){
}
});
dialog.show();
}
私有布尔值isLocationEnabled(){
返回locationManager.isProviderEnabled(locationManager.GPS\U提供程序)||
locationManager.isProviderEnabled(locationManager.NETWORK\u提供程序);
}
公共void toggleNetworkUpdates(视图){
如果(!checkLocation())
返回;
按钮=(按钮)视图;
if(button.getText().equals(“Help”))
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,60*1000,10,locationListenerNetwork);
}
专用最终位置Listener locationListenerNetwork=新位置Listener(){
已更改位置上的公共无效(位置){
longitudeNetwork=location.getLongitude();
latitudeNetwork=location.getLatitude();
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//将经度和纬度值发送到StateMachine.java
Toast.makeText(MainActivity.this,“网络提供商更新”,Toast.LENGTH_SHORT.show();
}
});
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
};
公共布尔值isSupported(){
返回true;
}
}
看看这个


请注意,由于代码名One中存在跨平台的location API,所以不需要本地接口进行location。我建议查看开发者指南和。

我已经看过了关于本机接口的视频教程,但我不确定如何在我的例子中做到这一点。在我的StateMachine.java中,我想运行那些Android代码,由于Codename One目前不支持打开GPSWe,因此如果GPS显式关闭,则不会打开GPS,本机代码也不支持。您的代码低于我们使用play服务的位置代码