LocationListener with Thread.Sleep - java

My application uses a progress dialog to let my user know that the location listener is currently searching for their current coordinates as shown:
ProgressDialog searching;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
searching = ProgressDialog.show(this, null, "Gaining GPS posistion...", false,true);
This is great but I want to set a time limit that this can run for. Can i use a simple Thread.Sleep static instance set at - for example - 20 seconds? This will then check if my location object is still null and will then cancel the search?
Can i run both a Location Listener and a thread.sleep concurrently? I have looked at the Oracle notes for Thread.Sleep but I'm still not 100% sure.

Can i use a simple Thread.Sleep static instance set at - for example - 20 seconds?
That would be disastrous. Do not use sleep() on the main application thread in Android, as it freezes your UI for however long your sleep() call is set for.
There are many better ways to get control in your activity in 20 seconds. The lightest-weight solution is to call postDelayed() on one of your widgets, with a Runnable that will execute what you want in 20 seconds.
I have looked at the Oracle notes for Thread.Sleep but I'm still not 100% sure.
Since Oracle has little to do with Android, you are better served reading Android documentation.

Related

Checking status of GPS Provider in Android App

I have an operational Android app which reports users location within a background service. I want to integrate a feature which will notify the user when GPS signal has been lost.
Our current implementation to commence location updates is:
mLocationListener = LocationListener(LocationManager.GPS_PROVIDER, gpsDeviceCallback)
if (handlerThread?.isAlive == true) {
handlerThread?.quit()
}
handlerThread = HandlerThread("GpsLocationHandler")
handlerThread!!.start()
mLocationManager?.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE.toFloat(),
mLocationListener, handlerThread!!.looper)
with LOCATION_INTERVAL set to 1000 ms and LOCATION_DISTANCE set to 10m.
We get the expected onLocationChanged callbacks under normal operation. However, I would appreciate advice on how to detect a situation where there is a loss of GPS or handset cannot obtain an adequate GPS signal.
We have implemented a solution where we run a time task and determine if onLocationChanged is called during the timer period. The problem with this solution is that if the user handset is stationary during this time then no onLocationChanged callbacks will happen. So this approach will not work as a means of detecting no\inadequate GPS signal.
The user of onStatusChanged as a method of LocationListener is now deprecated so this is not an option either.
Is there some standard solution to this problem ? Perhaps some method which could be called in the case where no onLocationChanged callbacks happen when our check timer expires to test current GPS status?
Thanks!
you can check the current connected satellites and approximate the accuracy see :https://stackoverflow.com/a/10589949

Run a TimerTask or a Handler ALWAYS correctly

I have a problem (that's why I'm here :P):
I am running a TimerTask and/or a Handler. They should do something every second, no matter if the screen is on or not (standby). The problem is, after some time (2 to 10 hours) this process becomes weird timed. Sometimes it takes 10 seconds, sometimes 4 hours etc.
Now, I've read that you can use a Partial Wake Lock to solve this issue. Tried it, but it has not solved my issue (Maybe you should know that another library is also using a WakeLock which gets released after some time, but mine never gets released by me).
Maybe you should also know that the task/runnable runs on an asynctask (so on it's own thread). The wakelock is created from outside.
Edit:
Maybe it's good to know that it's a device owner app. Also, I know of the battery drain problem, but I still need it. The app really has to process this every second. I just need a solution for it, any, no matter which.
Edit 2:
Here's my current WakeLock code, which is started when my custom application reaches onCreate. As I said, it is never released.:
PowerManager mgr = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"CustomWakeLock");
wakeLock.acquire();

LocationManager requestLocationUpdates minTime OR minDistance

I'm using Android's LocationManager and its method requestLocationUpdates like this:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, this);
As I found out both the conditions of minTime and minDistance have to be met for location update, but I need to get update every 3 seconds or 10 meters.
I tried to put 2 requests like this:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, this);
but it doesn't work, I only get update every 10 meters with this code, not every 3 seconds.
I'm working with API19 if that matters.
The Documentation on requestLocationUpdate() says :
requestLocationUpdates(String provider, long minTime, float
minDistance, LocationListener listener)
Register for location updates using the named provider, and a pending intent.
So you should be calling it like locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, this);
But If you set minTime to 0, it will be called once when it first receives a location update, then it won't be called until you change your position in minDistance meters.
Documentation Link for Reference
EDIT
As per the Discussion with #Matej I need to get update every 10 meters even if it happened in less than 3 seconds, and update every 3 seconds even if the location didn't change by more than 10 meters
If you want to regularly requestLocationUpdates, you should use Timer and TimerTask and have requestLocationUpdates run once every 3 seconds
schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution,
beginning after the specified delay.
I read this discussion for a similar problem.
I had to update location every second, but setting:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
I didn't achieve my goal.
In the official documentation I read that also if you fix a timerate for updates, it could be not keeped so rigid as setted. (I can't find anymore the developper page which talk about).
To have a fixed and sure timerate for updates I found this post.
My problem was that Location updates went to update (by observers) the activity and I got error using Timer (as suggested).
the error was:
Only the original thread that created a view hierarchy can touch its views.
For anyone has similar problem I suggest to use Handler:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
notifyLocationObservers();
}
}, 1000);

Android Periodic Sensor Data

For an Android App, that will show the value of an internal sensor (e.g. Acceleration) in a graph, i need to find a way to access this signal periodically.
At the moment i am using a SensorEventListener, but unfortunately this only gives me the possibility to get a value whenever it changes.
Since I want to display the graph (point to point) in dependency of the time, this means it would directly draw a line from the old to the new value (and if the old value has been a long time, it looks like a linear changing of the value).
So my question: How can I get access to a sensor's data periodically?
The documentation for SensorManager says that registerListener(android.hardware.SensorEventListener, android.hardware.Sensor int)
"Registers a SensorEventListener for the given sensor at the given sampling frequency."
To get these events, though, your application would need to be active (hold a partial wake lock). It would be better to do this in a background service so that the application doesn't need to remain active. See for example, SensorEventListener in a service
So in the end i used a timer, which checked the values from my sensor, which I put into an array, periodically.
Unfortunately registerListener didnt work, since the value is just a suggestion for the system.
Thanks though for the help.

Getting location by few ways in Timer

Hello I'm creating tracking app and I want to quickly get the location. But this app should be friendly for people. It should works like that:
I turn on GPS and i want to get location by it
If it takes more that 5 seconds I try get location by network provider
If it takes more that 5 seconds i try to get location by wifi
if it takes more I do something else.
I already have this functions! How to do that they work one after another (If one wokrs more than 5 sec we move to another one).
So I'm thinking that I must use some kind of timer, but here I got a problem if I put code like this
Timer timer2 = new Timer();
TimerTask testing = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
}
});
}
}; timer2.schedule(testing, 1000); ***
*** here syntax errors
not inside onCreate I have syntax errors (and I want to do that after pushing the button). However how should looks this timer for 3 methods following next by next ?
depending on the OS that you are devlopping for, I would recommend you to make use of the OS specific methods available for retrieving location.
In case of Android for example there is a service available that makes is quite easy for you to retrieve the location in an user friendly manner:
Please check this: https://developer.android.com/training/location/index.html
This allows you to create location requests, depending on your situation you can define the request with a certain priority. For example: in case city precision is enough in your situation, you can define it and the framework might decide to only use the currently available WIFI to get the location (and not startup the GPS).

Categories