473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

stopping location service

134 100+
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
9 2027
r035198x
13,262 8TB
Does it crash with an error message or stack trace?
Apr 3 '13 #2
michaeldebruin
134 100+
The screen just freezes, then turns black. And then it says that the application has stopped working.
Apr 3 '13 #3
r035198x
13,262 8TB
Try running it in debug mode and checking the logs or debugging in the simulator.
Apr 3 '13 #4
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
michaeldebruin
134 100+
@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
r035198x
13,262 8TB
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
michaeldebruin
134 100+
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
r035198x
13,262 8TB
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
michaeldebruin
134 100+
no it ain't there anymore. So fixed that one too.
Apr 4 '13 #10

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

Similar topics

0
by: Jeffrey Bradshaw | last post by:
Is there any way for a service to stop itself? I have conditions (like network problems) where I want to stop the service not just let it quietly keep running not doing anything. So is there any...
0
by: RayG | last post by:
What if a service needs to signal the ServiceControlManager (SCM) that it is stopping for instance, a necessary parameter or object is missing or failed and is discovered in the OnStart method of...
9
by: SP | last post by:
Hi All, I wrote a windows service which is supposed to stop after specified amount of time. I am calling OnStop() after specified time. OnStop() methods executed but I dont see the service...
0
by: Glen Wolinsky | last post by:
I am creating a Windows service that will check a request queue (database) for pending requests. It will then process each individual request until completed, wait a set time interval and then...
6
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class...
5
by: marccruz | last post by:
Hi, I am writing a Windows Service in C#. I want to gracefully fail the "protected override void OnStop()" function. To do this, I first tried throwing an Exception in the function. However,...
2
by: tshad | last post by:
I have a Service that starts a thread that never ends. When I stop the service from the Service Applet, does it kill the thread or do I need to do it myself? If it isn't killed, what happens...
2
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello everyone, I have this application that stops and starts IIS admin. When I try to stop the service. I get an error "Cannot open IISADMIN service on computer '.'.". I tried changing the...
10
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on...
6
by: santhanalakshmi | last post by:
Hi, I want to start the mysql services by manually in the dos on windows...... Starting an Mysql Service : shell>net start mysql The service name is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.