首页 > 代码库 > GPS收集服务demo

GPS收集服务demo









public class GpsInfoCollectionService extends Service {
	
	private Boolean D=true;
	private GPSinfoDao mGpSinfoDao;
	private LocationManager mLocationManager;
	private Location mLocation;
	public GpsInfoCollectionService() {
		
	}

	@Override
	public IBinder onBind(Intent intent) {
		if(D){
			Log.i("GPS服务数据收集","IBinder()");
		}
		return null;
	}
	@Override
	public void onCreate() {
		if(D){
			Log.i("GPS服务数据收集","onCreate()");
		}
		mGpSinfoDao=DatabaseInfoFactory.getGPSinfoDao(getApplicationContext());
		//mGpSinfoDao=new GPSinfoDao(getApplicationContext());
		 mLocationManager=(LocationManager) getSystemService(LOCATION_SERVICE);
		super.onCreate();
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(D){
			Log.i("GPS服务数据收集","onStartCommand()");
		}
		  Criteria criteria = new Criteria();
	        criteria.setAccuracy(Criteria.ACCURACY_FINE);//获取精确的位置.
	        criteria.setAltitudeRequired(true);
	        criteria.setBearingRequired(true);
	        criteria.setCostAllowed(true);
	        criteria.setPowerRequirement(Criteria.POWER_HIGH);
	        criteria.setSpeedRequired(true);
	        
	        String provider = mLocationManager.getBestProvider(criteria, true);
	     
		
	     		
	        mLocationManager.requestLocationUpdates(provider, 50, 0.001f, new LocationListener() {
				
				@Override
				public void onStatusChanged(String provider, int status, Bundle extras) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProviderEnabled(String provider) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProviderDisabled(String provider) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onLocationChanged(Location location) {
					//updateLocation(location);
					mLocation=location;
					new AsyncTask<Void, Void, Void>() {

						@Override
						protected Void doInBackground(Void... params) {
							
							//double height=mLocation.getAltitude();
							//经度
							double longitude=mLocation.getLongitude();
							//纬度
							double latitude=mLocation.getLatitude();
							//把gps信号转成百度地图坐标
							JWD jwd=GPSToBaiDu.getData(longitude+"", latitude+"");
							GpsInfo info=new GpsInfo();
								//info.setLongitude(longitude+"");
								info.setLongitude(jwd.getX());
								//info.setLatitude(latitude+"");
								info.setLatitude(jwd.getY());
								//info.setHeight(height+"");
								mGpSinfoDao.addGpsInfo(info);
								info=null;
							return null;
						}
						
						
					}.execute();
	        
	    
				}
				
	        });
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		if(D){
			Log.i("GPS服务数据收集","onDestroy()");
		}
		mGpSinfoDao=null;
		mLocationManager=null;
		mLocation=null;
		super.onDestroy();
	}

}

GPS收集服务demo