All I wanted to do is to get the current location using GPS before I move to next activity. So I disabled the buttons that takes me to next activities. The buttons are enabled only after getting a location using the GPS. The following code runs perfectly. But I found one bug..
Last time I used the app it was quite right. Then I moved to my University. There I launched the app and then moved to next activity as location was retrieved by then. I got the location of my home which is about 10 kilometers away. Then after closing the application, I launched it once again and this time it retrieved the correct location.
Well, this is happening because of the line below:
location = locationManagerAsync.getLastKnownLocation(providerAsync);
My app is retrieving the last location and enabling the buttons. But it is able to keep the current location to use for the next time I launch the app. As a result, on the second attempt, the location is retrieved perfectly.
I used a code from a question on stackoverflow.com which is given below.
private class MyLocationTracker extends AsyncTask<Void, Void, Void> implements LocationListener {
private Context ContextAsync;
public MyLocationTracker (Context context){
this.ContextAsync = context;
}
Dialog progress;
private String providerAsync;
private LocationManager locationManagerAsync;
double latAsync=0.0;
double lonAsync=0.0;
Location location;
#Override
protected void onPreExecute() {
super.onPreExecute();
progress = ProgressDialog.show(ContextAsync, "Loading location", "Please wait...");
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
locationManagerAsync = (LocationManager) ContextAsync.getSystemService(ContextAsync.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
providerAsync = locationManagerAsync.getBestProvider(criteria, false);
if (locationManagerAsync.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
providerAsync = LocationManager.GPS_PROVIDER;
} else if (locationManagerAsync.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
providerAsync = LocationManager.NETWORK_PROVIDER;
} else if (locationManagerAsync.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
providerAsync = LocationManager.PASSIVE_PROVIDER;
//Toast.makeText(ContextAsync, "Switch On Data Connection!!!!", Toast.LENGTH_LONG).show();
}
location = locationManagerAsync.getLastKnownLocation(providerAsync);
// Initialize the location fields
if (location != null) {
// System.out.println("Provider " + provider + " has been selected.");
latAsync = location.getLatitude();
lonAsync = location.getLongitude();
} else {
//Toast.makeText(ContextAsync, " Location not available", Toast.LENGTH_SHORT).show();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
onLocationChanged(location);
Log.v("latAsync_lonAsync",latAsync+"_"+lonAsync);
myCurrentLatitude = latAsync;
myCurrentLongitude = lonAsync;
// the buttons that were disabled in the onCreate() method
police.setEnabled(true);
medical.setEnabled(true);
fireService.setEnabled(true);
cityCorporation.setEnabled(true);
telePhnCall.setEnabled(true);
getRoute.setEnabled(true);
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
locationManagerAsync.requestLocationUpdates(providerAsync, 0, 0, this);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
And this is the line that I am using to call the AsyncTask from the Oncreate() method..
new MyLocationTracker(TypeOfEmergency.this).execute();
Now the problem is, I do not want the last location to be retrieved and stored in the variables. I tried the code after removing the line
location = locationManagerAsync.getLastKnownLocation(providerAsync);
But the app crashes in that case. Need a quick fix.
Here is the error log :: (Please have a look to the comments before having a look to the error log)
With the code something is basically wrong: the single line of code in onLocationChanged
locationManagerAsync.requestLocationUpdates(providerAsync, 0, 0, this);
needs to be called at the end of doInBackground (which does not need to be done in background). This line of code requests location updates from the provider (e.g. the GPS provider). When a new location is avaiable the onLocationChanged of your location listener is called (which is MyLocationTracker in your example due to the this in the line above. in onLocationChanged you need to react to a new location by enabling or disabling the functionality, you mentioned in your question.
The call to getLastKnownLocation does exectly what the name suggests: It returns the last known position, which may be quite old.
Related
suppoose I have my current location which is changeable and I dont know the hard code value of the latitude and longitude. now I have a string from where I want to get the Address object from that string using getFromLocationName() method, I have used this code:
address = coder.getFromLocationName(strAddress, maxNum);
but this returns list of address from allover the globe, but I want the results near to my locations come first, and then rest of the world, how do I sort it? I have found another method like this:
getFromLocationName (String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude);
but I dont know what to put in these latitude longitude parameters to fulfill my wish. Anyone?
/*********** Create class and implements with LocationListener ******/
public class GpsBasicsAndroidExample extends Activity implements LocationListener {
private LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_basics_android_example);
/********** get Gps location service LocationManager object *****/
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
/* CAL METHOD requestLocationUpdates */
// Parameters :
// First(provider) : the name of the provider with which to register
// Second(minTime) : the minimum time interval for notifications,
// in milliseconds. This field is only used as a hint
// to conserve power, and actual time between location
// updates may be greater or lesser than this value.
// Third(minDistance) : the minimum distance interval for notifications, in meters
// Fourth(listener) : a {#link LocationListener} whose onLocationChanged(Location)
// method will be called for each location update
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
3000, // 3 sec
10, this);
/********* After registration onLocationChanged method **/
/********* called periodically after each 3 sec *****/
}
/************* Called after each 3 sec ****/
#Override
public void onLocationChanged(Location location) {
String str = "Latitude: "+location.getLatitude()+"
Longitude: "+location.getLongitude();
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
}
#Override
public void onProviderDisabled(String provider) {
/******** Called when User off Gps ***/
Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();
}
#Override
public void onProviderEnabled(String provider) {
/******** Called when User on Gps ***/
Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
I am developing an application which lists restaurants closest to the user. When the refresh button is clicked, it lists the restaurants for the user's current location. I am using the Location Manager and requesting updates only when the activity comes to foreground(onResume) to avoid the constant usage of the battery. When the app goes in onPause(), the location updates are stopped. It works fine on the emulator when I pass the updates through the terminal.
Problem:
When I physically change my location (say drive 5 miles away) and I open my app and the activity to show the nearest restaurants, it takes long time(4-5 minutes) for the location to refresh and till then the app continues to show the restaurants for the previous location. But if I physically change my location and access Google Maps and then open my restaurant application, then it works instantaneously. I have ensured that the GPS is switched on. What can I do to get the Location Manager to kick in and refresh the location as soon as I open my activity?
Thank you in advance!
package com.mortley.android.restaurantsaver;
public class NearbyRestaurantActivity extends ListActivity implements OnClickListener, LocationListener{
private Button refreshButton, searchRestaurants;
ImageButton goToSearch;
private double[] lastKnownLocation;
private EditText locationEditText;
private LocationManager locManager;
private LocationListener locListener;
private boolean gps_enabled = false;
private boolean network_enabled = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearbyrestaurants);
refreshButton = (Button)findViewById(R.id.reloadButton);
refreshButton.setOnClickListener(this);
searchRestaurants = (Button)findViewById(R.id.searchButton);
searchRestaurants.setOnClickListener(this);
goToSearch = (ImageButton)findViewById(R.id.goLocationButton);
goToSearch.setOnClickListener(this);
locationEditText = (EditText)findViewById(R.id.addressTextBox);
locationEditText.setVisibility(View.GONE);
goToSearch.setVisibility(View.GONE);
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);//??
locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 100, this);
//checks network connectivity
boolean checkConnection = isNetworkAvailable();
if(!checkConnection){
Toast.makeText(getApplicationContext(), "Check your Network Connectivity", Toast.LENGTH_LONG).show();
}
if(checkConnection){
//sets current location parameters for the user
lastKnownLocation = RestaurantHelper.getLastKnownLocation(this);
//Log.v("NearbyRestaurantActivity", "This"+this);
RestaurantApplication application = (RestaurantApplication) this.getApplication();
RestaurantAdapter restaurantAdapter = new RestaurantAdapter(this, R.layout.restaurantrow, R.id.label,new ArrayList<RestaurantReference>());
restaurantAdapter.setLastKnownLocation(lastKnownLocation);
//set a global variable for the RestaurantAdapter in the RestaurantApplication class.
application.setRestaurantAdapter(restaurantAdapter);
//Set the adapter first and then update it when the RestaurantHttpAsyncTask makes a web service call.
setListAdapter(restaurantAdapter);
//Make a webservice call in a different thread passing Keyword for URL as a string array.
RestaurantHttpAsyncTask m_progressTask;
String[] keywords = {"", "american", "asian", "italian","mexican"};
//String[] keywords = {"indian"};
m_progressTask = new RestaurantHttpAsyncTask(NearbyRestaurantActivity.this, keywords);
m_progressTask.setRestaurantAdapter(restaurantAdapter);
m_progressTask.execute();
}
}
#Override
public void onClick(View v) {
//Refresh button helps to refresh the restaurant list on location change. Again it makes a call to the webservice using Async Task
if(v.getId() == refreshButton.getId() ){
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled) {
Toast.makeText(getApplicationContext(), "Sorry, Location is not determined. Please enable your Network Providers.", Toast.LENGTH_LONG).show();
}
//check network connectivity before refresh
boolean checkConnection = isNetworkAvailable();
if(!checkConnection){
Toast.makeText(getApplicationContext(), "Check your Network Connectivity", Toast.LENGTH_LONG).show();
}
if(checkConnection){
RestaurantApplication application = (RestaurantApplication) this.getApplication();
RestaurantAdapter restaurantAdapter = new RestaurantAdapter(this, R.layout.restaurantrow, R.id.label, new ArrayList<RestaurantReference>());
restaurantAdapter.setLastKnownLocation(lastKnownLocation);
//set a global variable for the RestaurantAdapter in the RestaurantApplication class.
application.setRestaurantAdapter(restaurantAdapter);
//Set the adapter first and then update it when the RestaurantHttpAsyncTask makes a web service call.
setListAdapter(restaurantAdapter);
//Make a webservice call in a different thread passing Keyword for URL as a string array.
RestaurantHttpAsyncTask m_progressTask, m_progressTask1;
String[] keywords = {"", "american", "asian", "italian","mexican", "chinese", "indian"};
//String[] keywords = {"Chinese"};
m_progressTask = new RestaurantHttpAsyncTask(NearbyRestaurantActivity.this, keywords);
m_progressTask.setRestaurantAdapter(restaurantAdapter);
m_progressTask.execute();
}
}
if(v.getId() == goToSearch.getId() ){
Activity child = this;
while(child.getParent() != null){
child = child.getParent();
}
TabGroup1Activity parent = (TabGroup1Activity)getParent();
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(locationEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//changes ** restaurantAdapter to RestaurantAdapter1 to test & application to application1
RestaurantApplication application1 = (RestaurantApplication) this.getApplication();
RestaurantAdapter restaurantAdapter1 = new RestaurantAdapter(this, R.layout.restaurantrow, R.id.label, new ArrayList<RestaurantReference>());
restaurantAdapter1.setLastKnownLocation(lastKnownLocation);
//set a global variable for the RestaurantAdapter in the RestaurantApplication class.
application1.setRestaurantAdapter(restaurantAdapter1);
//Set the adapter first and then update it when the RestaurantHttpAsyncTask makes a web service call.
setListAdapter(restaurantAdapter1);
//Make a webservice call in a different thread passing Keyword for URL as a string array.
RestaurantHttpAsyncTaskTextSearch m_progressTask, m_progressTask1;
String keywords = locationEditText.getText().toString();
if(keywords.equals("")){
keywords = "Pizza in Palo Alto";
}
keywords = keywords.replaceAll(" ", "%20");
keywords = keywords.replaceAll(",", "%20");
m_progressTask = new RestaurantHttpAsyncTaskTextSearch (NearbyRestaurantActivity.this, keywords);
m_progressTask.setRestaurantAdapter(restaurantAdapter1);
m_progressTask.execute();
locationEditText.setVisibility(View.GONE);
goToSearch.setVisibility(View.GONE);
}
if(v.getId() == searchRestaurants.getId() ){
if(goToSearch.isShown() == true){
goToSearch.setVisibility(View.GONE);
locationEditText.setVisibility(View.GONE);
}
else if(goToSearch.isShown() == false){
//check network connectivity before refresh
boolean checkConnection = isNetworkAvailable();
if(!checkConnection){
Toast.makeText(getApplicationContext(), "Check your Network Connectivity", Toast.LENGTH_LONG).show();
}
if(checkConnection){
goToSearch.setVisibility(View.VISIBLE);
locationEditText.setVisibility(View.VISIBLE);
}
}
}
}
//Method to check network connectivity
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting()) {
//Log.d("network", "Network available:true");
return true;
} else {
//Log.d("network", "Network available:false");
return false;
}
}
#Override
protected void onResume() {
super.onResume();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 100, this);
//Log.v("NearbyRestaurantActivity", "In OnResume()");
}
#Override
protected void onPause() {
super.onPause();
locManager.removeUpdates(this);
//Log.v("NearbyRestaurantActivity", "In onPause()");
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
public class RestaurantHelper {
public static double[] getLastKnownLocation(Activity activity){
double lat = 0.0;
double lon = 0.0;
LocationManager lm = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location == null){
lat = 0.0;
lon = 0.0;
}
else{
//Log.v("Latitude", Double.toString(location.getLatitude()));
//Log.v("Longitude", Double.toString(location.getLongitude()));
lat = location.getLatitude();
lon = location.getLongitude();
}
return new double[]{lat,lon};
}
}
The primary reason why you aren't getting updated location information quickly is that you're relying on the NETWORK_PROVIDER in the RestaurantHelper.getLastKnownLocation() method, but registering a LocationListener for the GPS_PROVIDER in onCreate().
So, this code in RestaurantHelper.getLastKnownLocation():
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
...should be changed to:
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
In theory, this should then give you the latest GPS location, which should have been refreshed when you register the listener. Conversely, you could also change to listening to the NETWORK_PROVIDER in onCreate() and leave RestaurantHelper.getLastKnownLocation() as is. It depends on your accuracy requirement - if you want high accuracy locations to return the nearest location to the nearest building level (e.g., 5-30m), you should use the GPS_PROVIDER. But, if you can live with coarser accuracy, the NETWORK_PROVIDER typically returns a new location much faster than GPS, especially if you're indoors, and sometimes this can be fairly accurate if derived from WiFi.
Another approach would be to listen to both GPS_PROVIDER and NETWORK_PROVIDER by registering both via two requestLocationUpdates() lines in onCreate(), and then checking to see most recent timestamp on the Location from lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); and lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);, and using the one that was updated more recently.
I would also recommend the following changes to make your code reliable on a large number of Android devices:
Specify the requestLocationUpdates() minDistance parameter as 0 when listening for GPS or NETWORK location updates - the minDistance parameter has a history of being unreliable and unpredictable in the way its interpreted by OEMs, until Android 4.1.
Switch to the new Fused Location Provider - this should be much more reliable when calling the getLastKnownLocation() method than the Android framework location APIs, and more consistent across different devices. Note that this relies on Google Play Services SDK, which is only available on Android 2.2 and higher.
I have 2 advice for you
LocationClient video, is the new way of doing location stuff. It has improvements over the LocationManager that can be a pain to manage and develop.
If you need to use LocationManager, you must know that requestLocationUpdates is buggy (very buggy). Since all its implementations on custom hardware differ. There is a hack/workaround that works. Before you call requestLocationUpdates, just kick start it with the following
Code :
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, 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(final Location location) {
}
});
requestLocationUpdates is buggy. Use network provider always to trigger onLocationChanged(...)
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 100, this)
only after using network provider use gps provider back to back:
locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 100, this)
do not forget to check if gps is enabled or not before requesting location update by gps.
In my current application, the location data that is returned is outdated. I use a method that gets updates based on min time/distance between updates. However, lets say i turn my phone off, drive to another city, and then turn the phone back on. In this case my GPS reading will be off. How do i force the app to get the current location NOT getLastKnownLocation().
I have heard of a locationListener but everything i have read is very vague on how to use it.
Here is the code I have just in case this clarifies whats going on:
public class GPSHandling extends Service implements LocationListener{
private final Context myContext;
//flag for gps status
public boolean isGPSEnabled = false;
//flag for network status
public boolean isNetworkEnabled = false;
// used to determine if i can get a location either by network or GPS
public boolean canGetLocation = false;
Location myloc;
public double latitude;
public double longitude;
public int MIN_TIME_BTWN_UPDATE = 500*10; // in miliseconds, so 10sec
public int MIN_DISTANCE_BTWN_UPDATE = 10; // in meters
protected LocationManager locManager;
public GPSHandling(Context context){
this.myContext= context;
getLocation();
}
public Location getLocation(){
try{
locManager =(LocationManager) myContext.getSystemService(LOCATION_SERVICE);
// now get gps status
isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//now the same for network
isNetworkEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled){
// do nothing since neither provider are enabled (this.cangetlocation = false but its already set to that by default)
}
else{
this.canGetLocation=true;
//first get values for locManager from the network provider. send parameters telling when to update
if(isNetworkEnabled){
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
Log.d("provider", "network");
//if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
if (locManager != null){
myloc =locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// after getting the current location, attempt to get the latitude and longitude values
if (myloc != null){
latitude = myloc.getLatitude();
longitude = myloc.getLongitude();
}
}
}
//now get values for locManager from the GPS provider. send parameters telling when to update
if(isGPSEnabled){
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
Log.d("provider", "GPS");
}
//if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
if(locManager!= null){
myloc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
// after getting the current location, attempt to get the latitude and longitude values
if(myloc != null){
latitude = myloc.getLatitude();
longitude = myloc.getLongitude();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return myloc;
}
// get an update of the current latitude by using this class method
public double getMyLatitude(){
if (myloc!= null){
latitude = myloc.getLatitude();
}
return latitude;
}
//get an update of the current longitude by using this class method
public double getMyLongitude(){
if (myloc != null ){
longitude = myloc.getLongitude();
}
return longitude;
}
// use this method to find if app can get the current location
public boolean canGetMyLocation(){
return this.canGetLocation;
}
public void showGPSDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(myContext);
// Setting Dialog Title
alert.setTitle("Location Setting");
// Setting Dialog Message
alert.setMessage("GPS is not enabled, do you want to enable this now in the settins menue?");
// setting icon to dialog
//alert.setIcon(R.drawable.)
// on pressing settings button
alert.setPositiveButton("Settins", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
myContext.startActivity(intent);
}
});
//on pressing cancel button
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// showing the alert dialog
alert.show();
}
public void stopUsingGPS(){
if(locManager !=null){
locManager.removeUpdates(GPSHandling.this);
}
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
You are already half using LocationListener in your code. You just didn't fully implement the code.
This method bellow is empty in your code:
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
This is the method that is called everytime GPS produces an location update.
You should complete it doing something like:
#Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
Regards.
Did you read the corresponding docs?
Look at Location API and LocationListener for the start.
LocationListener is actually pretty straight-forward to use and the only (clean) way to reliably retrieve the device's location.
Location data usually take up to 30 seconds to fully warm up. As you asked for the getLastKnownLocation() you, indeed, could have a outdated location returned.
Read more about it at Location strategies by Google itself.
I created this earlier on today, but it is not working. Location manager returns null, and I've even implemented the listener. Any ideas to the problems. thanks.
Edited:
I think this line is the problem
Location location = locationManager.getLastKnownLocation(provider);
Basically, if location is null, it will go into the else part of the if statement below it. Every time I compile the code, it will go into the else statement meaning it location is not updating.
public class Activity1 extends Activity implements LocationListener {
/** Called when the activity is first created. */
JoshTwoActivity main;
Activity2 two;
boolean checkTick = false;
String locationplace = "";
private LocationManager locationManager;
private String provider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
System.out.println(provider);
System.out.println(locationManager.getProviders(criteria, false));
System.out.println(locationManager.getProvider("network"));
System.out.println(locationManager.getAllProviders());
Location location = locationManager.getLastKnownLocation(provider);
System.out.println(locationManager.isProviderEnabled(provider));
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
System.out.println(String.valueOf(lat));
System.out.println(String.valueOf(lng));
} else {
System.out.println("Provider not available");
System.out.println("Provider not available");
}
}
/* Request updates at startup */
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
System.out.println(String.valueOf(lat));
System.out.println(String.valueOf(lng));
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disenabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
getLastKnownLocation() returns a location with the last known location fix for a provider. If the provider returns null, the provider has never had a location fix. It doesn't mean the provider is not available.
Once you call getLastKnownLocation() you should check to see if the results are accurate or recent enough for your purpose. If not you should request location updates using requestLocationUpdates().
This blog post contains everything you need to know about writing code using location providers.
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
Ok my first guess is that you are not giving location fix via command line or by using eclipse DDMS perpective. If this is problem go open DDMS perpecive and give latitude and longtiude you want and send it. So location will not be null from now on if this is the problem.
The following link may help you in understanding how to use emulator in finding the location. The last section of this page has that information.
http://developer.android.com/guide/topics/location/obtaining-user-location.html
Inside
public class IAmHere extends Activity implements LocationListener {
i have
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
and
inside
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iamhere);
i have
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
gpsString = (TextView)findViewById(R.id.gpsString);
String Data = "";
String koordinata1 = Double.toString(gps[0]);
String koordinata2 = Double.toString(gps[1]);
Data = Data + koordinata1 + " | " + koordinata2 + "\n";
gpsString.setText(String.valueOf(Data));
but seems it's not working? Why? I mean even emulator doesn't want to send GPS data - When I click "send" via UI or console, nothing happens...?
Thank you.
First, you implemented LocationListener on IAmHere, then did nothing with that interface.
Second, you are calling getLastKnownLocation(), but you are doing nothing to trigger Android to start collection location data on any provider.
These two problems are related.
Location services are off until something registers to get location data from them. Typically, this is via requestLocationUpdates() on LocationManager...which uses the LocationListener interface you implemented.
The recipe for using LocationManager is:
Register a LocationListener via requestLocationUpdates()
Unregister the LocationListener sometime (e.g., onDestroy()), so that you do not leak memory
When location fixes come into onLocationChanged(), do something useful
Here is a sample project that uses LocationManager and LocationListener in this fashion.