stopping location service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michaeldebruin
    New Member
    • Feb 2011
    • 134

    stopping location service

    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.

    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();
    	}
    }
    Any help will be high;y appreciated.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Does it crash with an error message or stack trace?

    Comment

    • michaeldebruin
      New Member
      • Feb 2011
      • 134

      #3
      The screen just freezes, then turns black. And then it says that the application has stopped working.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Try running it in debug mode and checking the logs or debugging in the simulator.

        Comment

        • adri00713
          New Member
          • Apr 2013
          • 5

          #5
          i have a question

          Given the list of a maximum of 10 numbers, where all numbers
          are different in the list create a method that will
          return all possible combinations in each combination the
          of the numbers should be equal to 10.

          example: input number list {0,2,3,5,10}

          output is

          {2,3,5} {0,10}

          Comment

          • michaeldebruin
            New Member
            • Feb 2011
            • 134

            #6
            @r035198x, I did that and it seems that this line of code:
            Code:
            locMan.removeUpdates(myLocListener);
            caused an argumentNullexc eption. So I've made an if statement which checks whether the listener is null or not. But it still causes the error. Do you have any idea what is going wrong?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              You have two different myLocListener; variables declared.
              One at near the top
              Code:
              LocationListener myLocListener;
              and the other inside the thread
              Code:
              LocationListener myLocListener = new LocationListener() {
              The one outside the thread is never initialized.

              Comment

              • michaeldebruin
                New Member
                • Feb 2011
                • 134

                #8
                I've solved my problem. Surrounded the whole retrieve location part with a while loop. And then just interrupted the loop.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Ok, you should still not be hiding that myLocListener variable though so you should fix that if it's still there.

                  Comment

                  • michaeldebruin
                    New Member
                    • Feb 2011
                    • 134

                    #10
                    no it ain't there anymore. So fixed that one too.

                    Comment

                    Working...