3

我正在使用 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

4

2 回答 2

0

我有时也会遇到这个错误。尝试像这样调用这个函数:

var cordovaRef = window.PhoneGap || window.Cordova || window.cordova;
cordovaRef.exec(
      callback,
      function(err) {
          callback('ERROR');
      },
      'gpsPlugin',
      'showCurrentLocation'
      []);
于 2013-09-07T20:28:14.490 回答
0

它现在正在工作。我提到了这个链接: http://www.adobe.com/devnet/html5/articles/extending-phonegap-with-native-plugins-for-android.html

谢谢大家

于 2013-09-19T09:25:27.917 回答