By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,649 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,649 IT Pros & Developers. It's quick & easy.

stopping location service

100+
P: 134
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.

Expand|Select|Wrap|Line Numbers
  1. package com.example.locationservice;
  2.  
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5.  
  6. import android.app.Service;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.location.Criteria;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.os.IBinder;
  15. import android.widget.Toast;
  16.  
  17. public class Location extends Service {
  18.  
  19.     Handler handler = new Handler();
  20.     Timer myTimer = null;
  21.     LocationManager locMan;
  22.     public static double latitude = 0;
  23.     public static double longitude = 0;
  24.     int sleepTime = 1000 * 5;
  25.     LocationListener myLocListener;
  26.  
  27.     @Override
  28.     public IBinder onBind(Intent intent) {
  29.         // TODO Auto-generated method stub
  30.         return null;
  31.     }
  32.  
  33.     @Override
  34.     public void onCreate() {
  35.         // TODO Auto-generated method stub
  36.         super.onCreate();
  37.  
  38.         locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  39.  
  40.         if (!locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  41.             Toast.makeText(getApplicationContext(),
  42.                     "Please turn your GPS on and start the service again",
  43.                     Toast.LENGTH_LONG).show();
  44.             stopSelf();
  45.         }
  46.  
  47.         if (myTimer != null) {
  48.             myTimer.cancel();
  49.         } else {
  50.             myTimer = new Timer();
  51.         }
  52.  
  53.         Toast.makeText(getApplicationContext(), "Service Started",
  54.                 Toast.LENGTH_LONG).show();
  55.  
  56.         myTimer.scheduleAtFixedRate(new LocationDisplayTask(), 0, sleepTime);
  57.     }
  58.  
  59.     class LocationDisplayTask extends TimerTask {
  60.  
  61.         @Override
  62.         public void run() {
  63.             // TODO Auto-generated method stub
  64.             handler.post(new Runnable() {
  65.  
  66.                 @Override
  67.                 public void run() {
  68.                     // TODO Auto-generated method stub
  69.                     boolean go = false;
  70.  
  71.                     while (go) {
  72.  
  73.                         try {
  74.                             int minTime = 0;
  75.                             float minDistance = 0;
  76.  
  77.                             LocationListener myLocListener = new LocationListener() {
  78.  
  79.                                 @Override
  80.                                 public void onStatusChanged(String provider,
  81.                                         int status, Bundle extras) {
  82.                                     // TODO Auto-generated method stub
  83.  
  84.                                 }
  85.  
  86.                                 @Override
  87.                                 public void onProviderEnabled(String provider) {
  88.                                     // TODO Auto-generated method stub
  89.  
  90.                                 }
  91.  
  92.                                 @Override
  93.                                 public void onProviderDisabled(String provider) {
  94.                                     // TODO Auto-generated method stub
  95.  
  96.                                 }
  97.  
  98.                                 @Override
  99.                                 public void onLocationChanged(
  100.                                         android.location.Location location) {
  101.                                     // TODO Auto-generated method stub
  102.                                     String loc = "Latitude: "
  103.                                             + location.getLatitude()
  104.                                             + "Longitude: "
  105.                                             + location.getLongitude();
  106.  
  107.                                     Toast.makeText(getApplicationContext(),
  108.                                             loc, Toast.LENGTH_LONG).show();
  109.                                 }
  110.                             };
  111.                             locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  112.  
  113.                             Criteria criteria = new Criteria();
  114.                             criteria.setPowerRequirement(Criteria.POWER_LOW);
  115.                             criteria.setAccuracy(Criteria.ACCURACY_FINE);
  116.                             criteria.setAltitudeRequired(false);
  117.                             criteria.setBearingRequired(false);
  118.                             criteria.setCostAllowed(true);
  119.                             criteria.setSpeedRequired(false);
  120.  
  121.                             String BestProvider = locMan.getBestProvider(
  122.                                     criteria, false);
  123.  
  124.                             locMan.requestLocationUpdates(BestProvider,
  125.                                     minTime, minDistance, myLocListener);
  126.  
  127.                         } catch (Exception e) {
  128.                             e.printStackTrace();
  129.                             go = false;
  130.                         }
  131.                     }
  132.                 }
  133.             });
  134.         }
  135.     }
  136.  
  137.     @Override
  138.     public void onDestroy() {
  139.         // TODO Auto-generated method stub
  140.         super.onDestroy();
  141.         myTimer.cancel();
  142.         locMan.removeUpdates(myLocListener);
  143.         Toast.makeText(getApplicationContext(), "Service Stopped",
  144.                 Toast.LENGTH_SHORT).show();
  145.     }
  146. }
Any help will be high;y appreciated.
Apr 3 '13 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
Does it crash with an error message or stack trace?
Apr 3 '13 #2

100+
P: 134
The screen just freezes, then turns black. And then it says that the application has stopped working.
Apr 3 '13 #3

10K+
P: 13,264
Try running it in debug mode and checking the logs or debugging in the simulator.
Apr 3 '13 #4

P: 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}
Apr 3 '13 #5

100+
P: 134
@r035198x, I did that and it seems that this line of code:
Expand|Select|Wrap|Line Numbers
  1. locMan.removeUpdates(myLocListener);
caused an argumentNullexception. 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?
Apr 4 '13 #6

10K+
P: 13,264
You have two different myLocListener; variables declared.
One at near the top
Expand|Select|Wrap|Line Numbers
  1. LocationListener myLocListener;
and the other inside the thread
Expand|Select|Wrap|Line Numbers
  1. LocationListener myLocListener = new LocationListener() {
The one outside the thread is never initialized.
Apr 4 '13 #7

100+
P: 134
I've solved my problem. Surrounded the whole retrieve location part with a while loop. And then just interrupted the loop.
Apr 4 '13 #8

10K+
P: 13,264
Ok, you should still not be hiding that myLocListener variable though so you should fix that if it's still there.
Apr 4 '13 #9

100+
P: 134
no it ain't there anymore. So fixed that one too.
Apr 4 '13 #10

Post your reply

Sign in to post your reply or Sign up for a free account.