Hello all,
I am trying to write a location service which can run on an interval. But somehow when I press the stop button and call on the onDestroy method, it crashes. And I just can't figure out why.
Any help will be high;y appreciated.
I am trying to write a location service which can run on an interval. But somehow when I press the stop button and call on the onDestroy method, it crashes. And I just can't figure out why.
Code:
package com.example.locationservice;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;
public class Location extends Service {
Handler handler = new Handler();
Timer myTimer = null;
LocationManager locMan;
public static double latitude = 0;
public static double longitude = 0;
int sleepTime = 1000 * 5;
LocationListener myLocListener;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(getApplicationContext(),
"Please turn your GPS on and start the service again",
Toast.LENGTH_LONG).show();
stopSelf();
}
if (myTimer != null) {
myTimer.cancel();
} else {
myTimer = new Timer();
}
Toast.makeText(getApplicationContext(), "Service Started",
Toast.LENGTH_LONG).show();
myTimer.scheduleAtFixedRate(new LocationDisplayTask(), 0, sleepTime);
}
class LocationDisplayTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
boolean go = false;
while (go) {
try {
int minTime = 0;
float minDistance = 0;
LocationListener myLocListener = 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(
android.location.Location location) {
// TODO Auto-generated method stub
String loc = "Latitude: "
+ location.getLatitude()
+ "Longitude: "
+ location.getLongitude();
Toast.makeText(getApplicationContext(),
loc, Toast.LENGTH_LONG).show();
}
};
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);
String BestProvider = locMan.getBestProvider(
criteria, false);
locMan.requestLocationUpdates(BestProvider,
minTime, minDistance, myLocListener);
} catch (Exception e) {
e.printStackTrace();
go = false;
}
}
}
});
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myTimer.cancel();
locMan.removeUpdates(myLocListener);
Toast.makeText(getApplicationContext(), "Service Stopped",
Toast.LENGTH_SHORT).show();
}
}
Comment