I've written my custom location manager to check for an update in the user's location every 30 seconds. The code is working fine i.e. I'm receiving updates in user's location. But the problem is that the GPS icon is always visible on the status bar on top. I'm guessing that it should be visible only once in 30 seconds. Is this normal or I'm doing something wrong?
public volatile Double mLatitude = 0.0;
public volatile Double mLongitude = 0.0;
int minTime = 30000;
float minDistance = 0;
MyLocationListener myLocListener = new MyLocationListener();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);
String bestProvider = locationManager.getBestProvider(criteria, false);
locationManager.requestSingleUpdate(criteria, myLocListener, null);
locationManager.requestLocationUpdates(bestProvider, minTime, minDistance, myLocListener);
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc)
{
if (loc != null) {
//Do something knowing the location changed by the distance you requested
mLatitude = loc.getLatitude();
mLongitude = loc.getLongitude();
Toast.makeText(mContext, "Location Changed! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String arg0)
{
//Do something here if you would like to know when the provider is disabled by the user
Toast.makeText(mContext, "Provider Disabled! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String arg0)
{
//Do something here if you would like to know when the provider is enabled by the user
Toast.makeText(mContext, "Provider Enabled! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{
//Do something here if you would like to know when the provider status changes
Toast.makeText(mContext, "Provider Status Changed! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();
}
}
As long as 1 or more apps have called requestLocationUpdates for the GPS provider, GPS will stay on. It doesn't turn off between requests. It can't- doing so would cause it to lose satellite lock, which would cause it to have to re-establish. That takes a lot more than 30 seconds sometimes. So GPS will stay on until you unregister for GPS events.
The question is old but still, it would be great to give a better answer.
You need to use a Handler which runs continuously on 5 minutes interval so, you get the location update only once and you release the GPS device, this way your app won't listen to the GPS updates but your handler know when it should be called again to listen the updates.
public static void getLocation(Context context) {
Handler locationHandler = new Handler();
locationHandler.post(new Runnable() {
#Override
public void run() {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// Handle the location update
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}, null);
locationHandler.postDelayed(this, 1000 * 60 * 5);
}
});
}
Related
I have an AsyncTask class that needs to get user's location when this AsyncTask is called.
The idea is to check if location service is enabled or not, if not, the location service toggle will popup for user to turn it on, then it will go back to the task to continue getting the location and finish the job.
However, the code below show NullPointerException on the line lat = location.getLatitude(); near the end of onPreExecute(). They said because getLastKnownLocation gets nothing as it's just been turned on a moment ago, then how can I get the location right after the location service has been turned on?
LocationManager locationManager;
LocationListener locationListener;
double lat;
double longi;
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
location = new Location(LocationManager.NETWORK_PROVIDER);
locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
//I copy the part below from the internet but seems like "onProviderDisabled" and "onLocationChanged" were never be called
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Log.v("GPS CHECKKKKKKK",location.getLatitude()+"_"+location.getLongitude());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
};
locationManager.requestLocationUpdates("gps", 500, 0, locationListener);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,0, locationListener);
lat = location.getLatitude();
longi= location.getLongitude();
Log.v("GPS Cord CHECK",lat+"_"+longi);
}
#Override
protected String doInBackground(final Checkout_Package... params) {
//Doing the job that needs lat and longi value!
}
Thank you for your time!
You cannot create and put a location listener in onPreExecute() because the onChanged handlers will only be invoked much later when onPostExecute or even your AsyncTask has finished.
What you should do instead is start an AsyncTask in that location changed handler.
So in onLocationChanged().
With greenapps's suggest I figured it out. I put the code into the button that call the AsyncTask
checkout_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LocationManager locationManager;
LocationListener locationListener;
gotit = false; //Boolean to start the AsyncTask only once.
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Log.v("GPS CHECKKKKKKK",location.getLatitude()+"_"+location.getLongitude());
lat = location.getLatitude();
longi = location.getLongitude();
if (gotit == false) {
new Checkout_Async(Checkout.this, listener).execute(new Checkout_Package(user, product_all, getTotalCost(product_all), fee, getTotalCost(product_all) + fee, lat, longi));
gotit = true;
pd.dismiss();
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
pd=ProgressDialog.show(Checkout.this,"",getResources().getString(R.string.please_wait),false);
locationManager.requestLocationUpdates("gps",50,0,locationListener);
}
});
Now it works. Thank you!
I want to fetch the current location (latitude,longitude pair) of user and send it to server only when user has traveled 100 Kilo meters away from previous fetched location. What is the best way to do this if there is any?
My purpose is to use least possible resources of user while fetching location.
There is an interface called LocationListener:
http://developer.android.com/reference/android/location/LocationListener.html
You can use your LocationManager like:
public void startLocationUpdates() {
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(bestProvider, 60000, 10, new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onLocationChanged(Location location) {
System.out.println("LOCATION CHANGED: "+location);
}
});
}
public void stopLocationUpdates() {
locationManager.removeUpdates(this);
}
The 10 is the minimum distance, and the 60000 in this case is the minimum time between requests.
I've been getting current location with GoogleApiClient up until now but I've just noticed that it's much simpler to do it with LocationManager using LocationListener since it can even detect when GPS service was turned on or off by the user.
But I have a problem when getting user's first location after the LocationManager was initialized.
LocationManager has 4 listeners but none of them give you your first location. It does have a onLocationChanged listener but it only activates when you move.
This is how I'm using it:
// Init LocationManager (needed to track if GPS is turned on or not
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
end of oncreate......
/*
LocationListener (Listening if GPS service is turned on/off)
*/
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderDisabled(String provider) {
}
On this code i want to get the gps location every 3 seconds. but when i run this program on my device it tells me that latitude and longitude are both 0 every time. how should i handle this problem?
and what is the looper task ?? it gives me error when i donot use it
public class LocationService extends Service {
final static String TAG = "MyService";
double latitude ;
double Longitude ;
LocationManager lm;
LocationListener ll;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread() {
public void run() {
Looper.prepare();
while(true){
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, ll);
//when i log here , it gives me wronganswer
Log.d(TAG,Double.toString(latitude));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
Log.d(TAG, "OnDestroy");
super.onDestroy();
}
class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
//when i log here , it gives me correct answer
double lat = location.getLatitude();
double lon = location.getLongitude();
latitude = lat;
Longitude = lon;
}
#Override
public void onProviderDisabled(String arg0) {
}
#Override
public void onProviderEnabled(String arg0) {
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
}
When you get location from GPS, it can take some time, because GPS module needs to find satellites. So you need to wait couple or few minutes while GPS module find satellites. If WI-FI location provider available you can get location more quick.
Additional info about location providers you can find here: http://developer.android.com/guide/topics/location/strategies.html
Since Android has
GPS_PROVIDER and NETWORK_PROVIDER
you can register to both and start fetch events from onLocationChanged(Location location) from two at the same time. So far so good. Now the question do we need two results or we should take the best. As I know GPS_PROVIDER results have better accuracy than NETWORK_PROVIDER.
for example you have location listener:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//here you get locations from both providers
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
YOu need to do something like that:
LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationListener);
but before registering you need to check is these providers available on this device, like that:
boolean network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
I use the following code to get Current Location from a Network provider in my application:
LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(network_enabled){
Location location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
But it gives a location approximately 300-700 meters away from my actual location.
This is expected from a Network provider. But the problem is:
with only this Network provider enabled, and no GPS, I openned Foursquare
application where it shows my current location exactly where I am. Now
when I come back to my application, it shows the accurate current
location or say the same location which Foursquare showed.
Same thing happens with Google apps like Navigator, Maps etc..,
How can this be done? How are other apps able to get the exact location, based on just the Network provider?
Complete Code:
public class MyLocationActivity extends Activity implements LocationListener {
private LocationManager mgr;
private String best;
Location location;
public static double myLocationLatitude;
public static double myLocationLongitude;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
dumpLocation(location);
}
public void onLocationChanged(Location location) {
dumpLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
protected void onPause() {
super.onPause();
mgr.removeUpdates(this);
}
#Override
protected void onResume() {
super.onResume();
mgr.requestLocationUpdates(best, 15000, 10, this);
}
private void dumpLocation(Location l) {
if (l != null){
myLocationLatitude = l.getLatitude();
myLocationLongitude = l.getLongitude();
}
}
}
Thank You
You are getting just the last known location. You should request location updates from the LocationManager. Use LocationManager.getLocationUpdates.
On your LocationListener on the onLocationChanged(Location location) method, you can check on the Location object, how accurate this location is, like this :
float accuracy=location.getAccuracy();
then, if this Location is accurate enough for you, you can stop receiving location updates from the LocationManager using removeUpdates() , and use the received Location. If the Location is not accurate enough, you can wait for a more precise Location, and stop the updates latter on.