Reputation: 36018
I am woring on an Android application which is sensitive to user location.
Once user stay at a location for a long time(20min for example), then we should do something accordingly (set the status of the nearest parking spot nearest as "busy"), and once he left that position set the status of the nearest parking spot nearest as "free".
Now I can get the user location for a registered LocationListener
, however I have two problems:
1)How to track the user location to make it sure if he have style for the specified time at the same location?
I have thought to create a Handler
and run a task to check the location:
private Location mLastLocation;
private Location mCurrentLocation;
private Handler mHandler = new Handler();
private Runnable mLocationCalculateRunnable = new Runnable() {
@Override
public void run() {
if (mCurrentLocation != null) {
if (!userLocationChanged()) {
} else {
}
}
mHandler.postDelayed(mLocationCalculateRunnable,10000); //check every 10 seconds
}
private boolean userLocationChanged() {
return getDistance(mCurrentLocation, mLastLocation) > 5;
}
};
mHandler.postDelayed(mLocationCalculateRunnable,1000);
private void onLocationChanged(Location location) {
mLastLocation = mCurrentLocation;
mCurrentLocation = location;
}
But I can not continue to check the location and the time.
2)The app should run even if user click the back
key to go back to the home screen, and the screen may get off, even the cpu may be go to sleep.
If so, I may not get the new location, and the handler may be stopped also.
I wonder my location check idea is proper and how to keep the app running at any suitation?
BTW,I can not access the google related services.
Upvotes: 2
Views: 195
Reputation: 15512
1) Use defines to set proper updating for the LocationManager
. These defines are minimum time and distance that is required to update position of user.
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
2) Run Service
that will work at background.
Small code example:
public class GPSservice extends Service implements LocationListener {
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 2;
private static final long MIN_TIME_BW_UPDATES = 1000 * 1;
double latitude, longitude;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
protected LocationManager locationManager;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getLocation();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onLocationChanged(Location location) {
new LocationReceiver(location, getApplicationContext());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public Location getLocation() {
try {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.d("Network", "OFF");
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "ON");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "ON");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}
Upvotes: 1