以下のコードスニペットを見つけて、Android用のネイティブCordovaプラグインを作成し、Web側から呼び出して応答を取得するために必要な構成を作成してください。
例: Cordovaネイティブプラグインを使用したAndroidデバイスからの現在地の緯度と経度。
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
}
}
Manifestfile.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);
}
}