请找到下面的代码片段,为 android 创建原生 Cordova 插件以及调用所需的配置并从 Web 端获取响应。
示例:使用 Cordova 原生插件的 android 设备的当前位置纬度和经度。
安卓代码:
package com.sample.activity;
import android.app.Activity;
import android.content.Context
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class LocationTrackPlugin extends CordovaPlugin implements LocationListener {
public static final String ACTION_START = "GetLocation";
public static CallbackContext callbackContext;
public static Activity activity;
@Override
public boolean execute(String action, final JSONArray jArray,
final CallbackContext callbackContext) throws JSONException {
activity = this.cordova.getActivity();
boolean result = false;
if (ACTION_START.equalsIgnoreCase(action)) {
LocationTrackPlugin.callbackContext = callbackContext;
LocationManager locationManager = (LocationManager) activity
.getSystemService(activity.LOCATION_SERVICE);
if (!locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
PluginResult pluginResult = new PluginResult(
PluginResult.Status.OK, "false");
pluginResult.setKeepCallback(true);
try {
callbackContext.sendPluginResult(pluginResult);
} catch (Exception e) {
e.printStackTrace();
}
} else {
result = true;
Location location = getCurrentDeviceLocation(activity);
String my_location = location.getLatitude() + ":" + location.getLongitude();
PluginResult pluginResult = new PluginResult(
PluginResult.Status.OK, my_location);
pluginResult.setKeepCallback(true);
try {
callbackContext.sendPluginResult(pluginResult);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return result;
}
public Location getCurrentDeviceLocation(Context contxt) {
LocationManager locationManager;
String provider;
Location location = null;
locationManager = (LocationManager) contxt
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
location = getCurrentLocation(provider, locationManager);
if (location != null) {
locationManager.removeUpdates(this);
return location;
} else {
location = getCurrentLocation(LocationManager.NETWORK_PROVIDER,
locationManager);
if (location != null) {
locationManager.removeUpdates(this);
return location;
} else {
locationManager.removeUpdates(this);
}
}
} else
Log.d("Location", "No Provider Found");
return location;
}
public Location getCurrentLocation(String provider,
LocationManager locationManager) {
Location newlocation = null;
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, 1000, 1, this);
if (locationManager != null) {
newlocation = locationManager.getLastKnownLocation(provider);
return newlocation;
}
}
return newlocation;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
清单文件.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Config.xml(文件位于 appname/cordova/Config.xml)
<feature name="LocationTrackPlugin">
<param name="android-package" value=com.sample.activity.LocationTrackPlugin" />
</feature>
ShowLocation.js
callGetlocationPlugin: function() {
if (Ext.os.is('Android')) {
cordova.exec(
function(result) {
console.log("Native call success", result);
},
function() {
console.log('Native call failed');
},
'LocationTrackPlugin', 'GetLocation', null);
}
}