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. - 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.
9 2027
Does it crash with an error message or stack trace?
The screen just freezes, then turns black. And then it says that the application has stopped working.
Try running it in debug mode and checking the logs or debugging in the simulator.
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}
@r035198x, I did that and it seems that this line of code: - 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?
You have two different myLocListener; variables declared.
One at near the top - LocationListener myLocListener;
and the other inside the thread - LocationListener myLocListener = new LocationListener() {
The one outside the thread is never initialized.
I've solved my problem. Surrounded the whole retrieve location part with a while loop. And then just interrupted the loop.
Ok, you should still not be hiding that myLocListener variable though so you should fix that if it's still there.
no it ain't there anymore. So fixed that one too.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |