如何停止onLocationChanged?

时间:2017-11-16 07:24:17

标签: android service

当我停止此serviceOnlocationchanged()侦听器运行时。如何停止Service onlocationchanged Listener

我在OnlocationChanged()内使用service

Java Code Here,

 @Override
   public int onStartCommand(Intent aIntent, int aFlags, int aStartId) {
       super.onStartCommand(aIntent, aFlags, aStartId);


       System.out.println("---------Taxi Meter in start command--------------");

       System.out.println("**************GeoService==>start command-------------");

       return START_STICKY;
   }

   @Override
   public void onConnected(@Nullable Bundle bundle) {
       startLocationUpdates();

   }

   @Override
   public void onConnectionSuspended(int i) {

   }

   @Override
   public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

   }

@Override
public void onLocationChanged(Location location) {

if (myLocation != null) {
distanceto = myLocation.distanceTo(location);
}
this.myLocation = location;

currentLat = location.getLatitude();
currentLong = location.getLongitude();


if (myLocation != null) {

try {

currentLat = myLocation.getLatitude();
currentLong = myLocation.getLongitude();

LatLng latLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());

if (oldLatLng == null) {
System.out.println("----------inside oldLatLngnull--------");
oldLatLng = latLng;
}
newLatLng = latLng;
if (mLatLngInterpolator == null) {
mLatLngInterpolator = new LatLngInterpolator.Linear();
}
oldLocation = new Location("");
oldLocation.setLatitude(oldLatLng.latitude);
oldLocation.setLongitude(oldLatLng.longitude);

// float bearingValue = oldLocation.bearingTo(location);

myMovingDistance = oldLocation.distanceTo(location);
double toKm=myMovingDistance/1000.0;
dis += toKm;
half_km += oldLocation.distanceTo(location);

session.setRideKm(String.format("%.1f", dis));

Intent meter_distance = new Intent();
meter_distance.setAction("com.app.ride.distance");
meter_distance.putExtra("current_distance", dis);
meter_distance.putExtra("half_km",half_km);
myContext.sendBroadcast(meter_distance);

// Toast.makeText(TaxiHail.this, String.valueOf(myMovingDistance), Toast.LENGTH_SHORT).show();

System.out.println("------kannan-----movingdistacn------------" + myMovingDistance);
System.out.println("------kannan-----movingdistacn toKm------------" + toKm);
System.out.println("------kannan-----Current dis km------------" + dis);
System.out.println("------kannan-----half_km in meter------------" + half_km);
System.out.println("------kannan-----session------------" + String.format("%.1f", dis))

oldLatLng = newLatLng;

} catch (Exception e) {
e.printStackTrace();


}
}


}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

private void setLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(periodicTime);
mLocationRequest.setFastestInterval(periodicTime);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

protected void startLocationUpdates() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}

protected void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(TaxiMeterService.this)
.addOnConnectionFailedListener(TaxiMeterService.this)
.build();
mGoogleApiClient.connect();
}

Runnable mHandlerTask = new Runnable() {
@Override
public void run() {

String id = myDBHelper.retriveid();
double lat = currentLat;
double longitude = currentLong;
Calendar aCalendar = Calendar.getInstance();
SimpleDateFormat aDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String aCurrentDate = aDateFormat.format(aCalendar.getTime());
float[] result = new float[10];
System.out.println("----status 1---jai--------------------");
System.out.println("**************GeoService==>status 1---jai------");
long currentTime = aCalendar.getTime().getTime();
///// Toast.makeText(getBaseContext(), "currentTime - previous_time " + (currentTime - previous_time) + "==" + aCurrentDate, Toast.LENGTH_SHORT).show();
if ((currentTime - previous_time) > 5000) {
insertDB = insertDB + 1;
if (insertDB == 2) {
System.out.println("-----insert----meter-------------------lat-" + lat + "longitute" + longitude + "ride_id1" + ride_id + "ride_id3" + id);
myDBHelper.insertLatLong(id, lat, longitude, aCurrentDate);
// Toast.makeText(getBaseContext(), travel_history+" "+insertDB, Toast.LENGTH_SHORT).show();
insertDB = 0;
} else if (insertDB > 2) {
insertDB = 0;
System.out.println("-----not insert----jai--------------------");
}
}
previous_time = currentTime;

handler.postDelayed(mHandlerTask, 5 * 1000);


}
};

}



if (isMyServiceRunning(TaxiMeterService.class)) {
                           stopService(new Intent(getApplicationContext(), TaxiMeterService.class));
                           System.out.println("---------------------Meter service Stopped--------------");
                       }

0 个答案:

没有答案