如何通过cordova exec在java中开发gps插件

How to develop gps plugin in java through cordova exec

本文关键字:开发 gps 插件 java 何通过 cordova exec      更新时间:2023-09-26

我正在使用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不是函数有什么帮助吗??

我是不是哪里出错了??我使用的是cordova 2.5.0

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

var cordovaRef = window.PhoneGap || window.Cordova || window.cordova;
cordovaRef.exec(
      callback,
      function(err) {
          callback('ERROR');
      },
      'gpsPlugin',
      'showCurrentLocation'
      []);

它现在正在工作。我引用了这个链接:CCD_ 1。

感谢所有