phonegap百度定位插件2.8版本在 android 2.2.2下卡死


请大侠们帮忙看下那里需要改进。

   
  package plugin;
  

//import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONException;
import org.json.JSONArray;
import org.json.JSONObject;

public class BaiduGeolocation extends CordovaPlugin {
public LocationClient mLocationClient = null;
public MyLocationListenner myListener = new MyLocationListenner();
public JSONObject jsonObj = new JSONObject();
public PluginResult result = null;
private CallbackContext Context;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {


this.Context = callbackContext;
if (action.equals("get")) {

cordova.getActivity().runOnUiThread(new RunnableLoc());

} else if(action.equals("stop")) {
mLocationClient.stop();
result = new PluginResult(PluginResult.Status.OK);
} else {
result = new PluginResult(PluginResult.Status.INVALID_ACTION);
}

while (result == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignoring exception, since we have to wait
// ui thread to finish
}
}
/**
在这里卡死*/
result.setKeepCallback(false);
this.Context.sendPluginResult(result);
return true;

}

@Override
public void onDestroy(){
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
mLocationClient = null;
}
super.onDestroy();
}


class RunnableLoc implements Runnable {

public void run() {
mLocationClient = new LocationClient(cordova.getActivity());
LocationClientOption option = new LocationClientOption();

option.setOpenGps(false);
option.setCoorType("bd09ll");
option.setPriority(LocationClientOption.NetWorkFirst);
option.setProdName("BaiduLoc");
option.setScanSpan(5000);
mLocationClient.setLocOption(option);

mLocationClient.registerLocationListener( myListener );
mLocationClient.start();
mLocationClient.requestLocation();

}

}


public class MyLocationListenner implements BDLocationListener {

public void onReceiveLocation(BDLocation location) {
if (location == null)
return;

try {
jsonObj.put("Latitude",location.getLatitude());
jsonObj.put("Longitude", location.getLongitude());
jsonObj.put("LocType", location.getLocType());
jsonObj.put("Radius", location.getRadius());


if (location.getLocType() == BDLocation.TypeGpsLocation){
jsonObj.put("Speed", location.getSpeed());
jsonObj.put("SatelliteNumber", location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
jsonObj.put("Addr", location.getAddrStr());
}

result = new PluginResult(PluginResult.Status.OK, jsonObj);

} catch (JSONException e) {
// TODO Auto-generated catch block
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}

}


public void onReceivePoi(BDLocation poiLocation) {
// TODO Auto-generated method stub

}


}

}

Android phonegap

totoko 11 years, 6 months ago

Your Answer