package com.jiuqi.njt.service; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.IBinder; import com.amap.api.location.AMapLocation; import com.jiuqi.mobile.nigo.comeclose.bean.app.gps.UserPositionBean; import com.jiuqi.mobile.nigo.comeclose.bean.cross.LBSMode; import com.jiuqi.mobile.nigo.comeclose.bean.cross.PositionClientBean; import com.jiuqi.mobile.nigo.comeclose.manager.app.IPositionManager; import com.jiuqi.mobile.nigo.comeclose.manager.cross.IPositionClientManager; import com.jiuqi.mobile.nigo.comeclose.ws.client.ClientContext; import com.jiuqi.njt.data.MyApp; import com.jiuqi.njt.data.OptsharepreInterface; import com.jiuqi.njt.util.Constants; import com.jiuqi.njt.util.GaoDeLocation; import com.jqyd.android.module.lbs.LocationClient; import com.jqyd.android.module.lbs.Interface.ILocationListener; import com.jqyd.android.module.lbs.bean.LocationInfo; /** * 指令定位(已停用) * 没有切换到lbstime服务上 * @author * */ public class UpPositionByTypeService extends Service { private final String TAG = this.getClass().getName(); private Context context = this; private GaoDeLocation gaode = null; private OptsharepreInterface sharePre = null; private MyApp application = null; private int type; private long random; @Override public void onCreate() { super.onCreate(); gaode = new GaoDeLocation(); sharePre = new OptsharepreInterface(UpPositionByTypeService.this); application = (MyApp) this.getApplication(); } @Override public void onStart(Intent intent, int startId) { type = Integer.parseInt(intent.getStringExtra("type")); random = Long.parseLong(intent.getStringExtra("random")); /** * 0、基站定位 1、百度定位 2、高德定位 3、GPS定位 */ LocationClient client = new LocationClient(context); switch (type) { case 0: client.request(1, new ILocationListener() { @Override public void onReceiveLocInfo(int locType, LocationInfo loc) { upLocation(loc,type); } }); break; case 1: client.request(new ILocationListener() { @Override public void onReceiveLocInfo(int locType, LocationInfo loc) { upLocation(loc,type); } }); break; case 2: // gaode.enableMyLocation( context, new com.jiuqi.njt.util.GaoDeLocation.OnAmapLocationReceiver() { @Override public void getLocation(AMapLocation location) { LocationInfo loc = new LocationInfo(); loc.setLat(location.getLatitude()); loc.setLon(location.getLongitude()); loc.setCity(location.getCity()); loc.setContent(location.getDistrict()); loc.setCountry(""); loc.setProvince(location.getProvince()); upLocation(loc ,type); } }); break; case 3: client.request(2, new ILocationListener() { @Override public void onReceiveLocInfo(int locType, LocationInfo loc) { upLocation(loc,type); } }); break; default: client.request(new ILocationListener() { @Override public void onReceiveLocInfo(int locType, LocationInfo loc) { upLocation(loc,type); } }); break; } super.onStart(intent, startId); } private void upLocation(LocationInfo loc, int type) { new LocationByInstructionTask(context, type, loc).execute(); } @Override public IBinder onBind(Intent intent) { return null; } class LocationByInstructionTask extends AsyncTask<Void, Void, Void> { private Context context; private String TAG = this.getClass().getName(); private int locationType; private LocationInfo lococation; private PositionClientBean positionClientBean; public LocationByInstructionTask(Context context, int locationType, LocationInfo lococation) { super(); this.context = context; this.locationType = locationType; this.lococation = lococation; } @Override protected Void doInBackground(Void... arg0) { try { // 处理session过期的问题 ClientContext context = application.getClientContext(); IPositionClientManager manager = null; IPositionManager poiManager = null; try { manager = context.getManager(IPositionClientManager.class); positionClientBean = manager.getByRandom( sharePre.getPres("mobileNumber"), random); locationToPosition(lococation, positionClientBean); if (type == LBSMode.GPS.getCode() && (0.0 != positionClientBean.getLat() && 0.0 != positionClientBean .getLon())) { try { poiManager = context .getManager(IPositionManager.class); UserPositionBean poiBean = poiManager .findGisByLonLat(String .valueOf(positionClientBean .getLon()), String .valueOf(positionClientBean .getLat())); positionClientBean.setProvince(poiBean .getProvince()); positionClientBean.setCity(poiBean.getCity()); positionClientBean.setCountry(poiBean.getCountry()); positionClientBean.setContent(poiBean.getContent()); } catch (Exception e) { e.printStackTrace(); } } manager.updateRecord(positionClientBean); // 判断是否保存有数据 } catch (Exception ct) { if (ct.getMessage().contains("重新登录")) { context = ClientContext.getClientContext( Constants.SERVER_URL, Constants.ANONYMOUS, Constants.ANONYMOUS); manager = context .getManager(IPositionClientManager.class); manager.create(positionClientBean); } ct.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } } private PositionClientBean locationToPosition(LocationInfo loc, PositionClientBean bean) { if (loc != null) { bean.setCity(loc.getCity()); bean.setContent(loc.getContent()); bean.setCountry(loc.getCountry()); bean.setIsSuccess(0); // switch (loc.getState()) { // case -1: // bean.setIsSuccess(1000); // bean.setContent("GPS未开启"); // break; // case 0: // bean.setIsSuccess(1000); // bean.setContent("GPS获取失败"); // break; // case 3: // bean.setIsSuccess(1000); // bean.setContent("百度获取位置失败"); // break; // case 5: // bean.setIsSuccess(1000); // bean.setContent("高德获取位置失败"); // break; // default: // break; // } bean.setLat(loc.getLat()); bean.setLon(loc.getLon()); bean.setProvince(loc.getProvince()); } return bean; } }