如何通过cordova exec在java中开发gps插件
我正在使用phonegap进行应用程序开发。现在我想使用通过cordova.exec在java中生成的gps。如何开发插件。如果我给cordova.exec,它就是给错误。 我的代码在javascript中如下所示:如何通过cordova exec在java中开发gps插件,java,javascript,android,cordova,Java,Javascript,Android,Cordova,我正在使用phonegap进行应用程序开发。现在我想使用通过cordova.exec在java中生成的gps。如何开发插件。如果我给cordova.exec,它就是给错误。 我的代码在javascript中如下所示: var gpsPlugin = { createEvent: function() { console.log("inside create ev"); cordova.exec( function succ
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不是函数
有什么帮助吗
我在哪里出了问题??我正在使用cordova 2.5.0,有时也会出现此错误。尝试如下方式调用此函数:
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova;
cordovaRef.exec(
callback,
function(err) {
callback('ERROR');
},
'gpsPlugin',
'showCurrentLocation'
[]);
它现在正在工作。我提到这个链接:
http://www.adobe.com/devnet/html5/articles/extending-phonegap-with-native-plugins-for-android.html
多亏了所有的u cal gpsPlugin.createEvent();内部设备准备好了吗?是的,它是内部设备准备好了。我尝试过这个解决方案。但我还是得到了同样的错误:(