我正在使用 phonegap 进行应用程序开发。现在我想通过 cordova.exec 使用在 java 中生成的 gps。如何开发插件。如果我给 cordova.exec 它给出错误。我的代码在javascript中如下:
var gpsPlugin = {
createEvent: function() {
console.log("inside create ev");
cordova.exec(
function success() {
alert("succeed");
}, // success callback function
function error() {
alert("error");
}
, // error callback function
'gpsPlugin', // mapped to our native Java class called "gpsPlugin"
'showCurrentLocation' //, with this action name
//[]
);
}
}
gpsPlugin.createEvent();
在 JAVA 中:
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class gpsPlugin extends CordovaPlugin {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
return true;
}
protected void showCurrentLocation() {
alert("inside showcurrent location");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
//Toast.makeText(gpsPlugin.this, message,
//Toast.LENGTH_LONG).show();
}
}
private void alert(String string) {
// TODO Auto-generated method stub
}
private LocationManager getSystemService(String locationService) {
// TODO Auto-generated method stub
return null;
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
//Toast.makeText(gpsPlugin.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
//Toast.makeText(gpsPlugin.this, "Provider status changed",
//Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
//Toast.makeText(gpsPlugin.this,
//"Provider disabled by the user. GPS turned off",
//Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
//Toast.makeText(gpsPlugin.this,
//"Provider enabled by the user. GPS turned on",
//Toast.LENGTH_LONG).show();
}
}
}
在 XML 中,我添加了以下行:
<feature name="gpsPlugin">
<param name="android-package" value="com.exampl.exampleApp.gpsPlugin" />
</feature>
但它说: typeerror : cordova.exec is not a function 任何帮助?
我在哪里出错了吗?我正在使用科尔多瓦 2.5.0