I got this class, and nothing inside the onCreate is happening? Does it not automatically get called upon initialization?
Code is below, Thanks alot! - Micheal
package com.michealbach.livedrops;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
public class AnyClass extends Activity {
double latitude = 0;
double longitude = 0;
String addressString = "Address Initialized";
String postalString = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
postalString = "Why doesn't this String change happen at all?";
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
public String getAddress() {
return addressString;
}
public String getPostal() {
return postalString;
}
}
Doing this results in a "application has stopped unexpectedly. Please try again". If I initialize the string to some set of characters instead of null, it'll just stay that way. Should it not do what is inside the onCreate() and make that change?
onLocationChanged() is just a class method in your program, it's not conected to anything. You have to do
private final LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// Do stuff
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
And then in your onCreate()
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
cr = new Criteria();
cr.setPowerRequirement(Criteria.POWER_LOW);
cr.setAccuracy(Criteria.ACCURACY_FINE);
provider = lm.getBestProvider(cr,true);
lm.requestLocationUpdates(provider, minTIME, minDISTANCE, locationListener );
Related
I´d like to implement a LocationListener in this way.
1 - Activity_first - Call LocationListener, but I have some spinner and this don´t respond when the LocationListener are onStart(). I´d like to implement with a Thread.
2 - When the onLocationChanged() in LocationListener gets called, I call Geocoder, and when the address is different than null, I save it on the shared preferences, and stop it.
Point 2, must be done in the background because the user might be in an Activity when I get the Geocoder's address (about 10 seconds, it depend on connection)
I was testing with IntentService, but of course, I doesn't work.
My Code
public class Carga_1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carga_1);
// Todo Create Spinner, etc
startService(new Intent(Carga_1.this, LocationService2.class));
}
}
public class LocationService2 extends Service {
public static final String BROADCAST_ACTION = "Hello World";
//public LocationManager locationManager; testted
//public MyLocationListener listener; tested
public Location previousBestLocation = null;
Thread my_thread;
#Override
public IBinder onBind(Intent intent) {
Log.d("[GPS_coord]", "????");
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
Runnable r = new Runnable() {
public void run() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}
};
my_thread = new Thread(r);
my_thread.start();
}
#Override
public void onDestroy() {
super.onDestroy();
my_thread.stop();
}
private void get_address(double lat, double longi) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(lat, longi, 1);
//SAVE ON SharedPrefereces
this.stopSelf();
}
catch (IOException e) {
Log.d("[GPS_geocoder]", "Decodificacion Erronea");
}
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(final Location loc) {
Log.i("GPS_Service", "Location changed");
Log.d("GPS_Service", String.valueOf(loc.getLatitude() + " : " + String.valueOf(loc.getLongitude())));
get_address(loc.getLatitude(), loc.getLongitude());
}
public void onProviderDisabled(String provider) {
Log.i("GPS_Service", "DES_habilitado");
}
public void onProviderEnabled(String provider) {
Log.i("GPS_Service", "Habilitado");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
I have looked about and cannot find any direct threads regarding what I am looking for. I am trying to create an Android application which dials out an emergency number at the push of a button (which I have got working) but cannot get the location (displayed in Longitude and Latitude) to display, I have tried doing it with Toast and EditText boxes. I am new to Android development so wanted to start with something easy, but the LongLat part is being troublesome. Any help would be greatly appreciated.
Below is the code I have been tampering with in order to try and get it to grab the Long and Lat, then in another file I have been trying to use a click listener to assign it to a button so when the button is pressed (in main.xml) it will display the Long and Lat either in a textfield or in toast.
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
import android.location.LocationManager;
import android.location.Criteria;
public class EmergencyLocation extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
/** Called when the activity is first created. **/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView);
longitudeField = (TextView) findViewById(R.id.long_lat);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialise the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}
private void TextView() {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}}
First, you need to set up a LocationManager:
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// set preferred provider based on the best accuracy possible
Criteria fineAccuracyCriteria = new Criteria();
fineAccuracyCriteria.setAccuracy(Criteria.ACCURACY_FINE);
String preferredProvider = manager.getBestProvider(fineAccuracyCriteria, true);
Now, you have to create a LocationListener. In this case, it calls the method updateLocation():
LocationListener listener = new LocationListener() {
public void onLocationChanged(Location location) {
// called when a new location is found by the network location provider.
updateLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
EDIT:
Then, you have to register the listener with your LocationManager (and try to get the cached location):
manager.requestLocationUpdates(preferredProvider, 0, 0, listener);
// get a fast fix - cached version
updateLocation(manager.getLastKnownLocation());
And finally, the updateLocation() method:
private void updateLocation(Location location) {
if (location == null)
return;
// save location details
latitude = (float) location.getLatitude();
longitude = (float) location.getLongitude();
}
EDIT2:
OK, just saw your code. In order to make it work, just move around a few bits:
/** Called when the activity is first created. **/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView);
longitudeField = (TextView) findViewById(R.id.long_lat);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
#Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}
Hope it helps!
Quite easy. I use the locationListener as an attribute inside a Location class. Here's how I do it:
package com.rifsoft.android.helper.location;
import com.rifsoft.android.helper.IListener;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class Location implements IListener
{
public final static int IDX_LATITIDE = 0;
public final static int IDX_LONGITUDE = 1;
private double[] lastLocation = {0.0, 0.0};
private LocationManager locationManager = null;
private ILocation activity = null;
private LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(android.location.Location location)
{
// TODO: http://developer.android.com/guide/topics/location/obtaining-user-location.html#BestEstimate
lastLocation[IDX_LATITIDE] = location.getLatitude();
lastLocation[IDX_LONGITUDE] = location.getLongitude();
activity.onLocationChange();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
/**
* Constructor, needs LocationManager
* #param activity Activity that will receive the notification when location has changed
* #param locationManager LocationManager
*/
public Location(final ILocation act, LocationManager lm) throws LocationException
{
if (lm == null)
{
throw new LocationException(LocationException.ERR_NULL_LOCATION_MANAGER);
}
if (act == null)
{
throw new LocationException(LocationException.ERR_NULL_ILOCATION);
}
locationManager = lm;
activity = act;
registerListener();
android.location.Location lastCachedLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastCachedLocation != null)
{
lastLocation[IDX_LATITIDE] = lastCachedLocation.getLatitude();
lastLocation[IDX_LONGITUDE] = lastCachedLocation.getLatitude();
}
}
/**
* Retuns last known most accurate location as latitude, longitude
* #return Latitude, Longitude
*/
public double[] getLastLocation()
{
return lastLocation;
}
#Override
public void registerListener()
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
#Override
public void unRegisterListener()
{
locationManager.removeUpdates(locationListener);
}
}
Then the activity.onLocationChange() goes like
public void onLocationChange()
{
locationUpdated = true;
double[] coordinates = location.getLastLocation();
EditText lon = (EditText) activity.findViewById(R.id.longitude_value);
lon.setText(String.valueOf(coordinates[Location.IDX_LONGITUDE]));
EditText lat = (EditText) activity.findViewById(R.id.latitude_value);
lat.setText(String.valueOf(coordinates[Location.IDX_LATITIDE]));
}
I'm kinda lost here: In my main activity, I register a LocationManager and connect it to a LocationListener to use myLocation.getLatitude() and such.
Now I need to use the Location- methods from another class.
I can't use those object from another class because I cant intantiate the main activity.
I can't use getters to pass the L.Manager or L.Listener around, because those are non- static again.
So, in general, how do i access objects that I created in the main activity?
Any hints on how to organize this better? Is the LocationListener class within the main activity class a stupid thing to do in general?
public class URNavActivity extends Activity
{
public LocationManager mlocManager;
public LocationListener mlocListener;
...
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
actVar=this;
initGraph();
setMap();
gpsEnable();
initMyLocation();
getItems();
initOverlay();
}
public void gpsEnable ()
{
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
myMap.getController().setCenter(new GeoPoint(lati, longi));
}
First and foremost your LocationListener should not be part of an activity. Activities have a clearly defined lifecycle and can come into being, and be destroyed, by the Android framework on an as-needed basis. Therefore the instance variables of your Activity will need to be re-initialised in your activity's onResume() method, making them completely unsuitable for long-term storage.
So. Start by creating a sticky service to manage the starting and stopping of location updates. Being sticky means that the service instance hangs around between invocations and therefore you can reliably use instance variables and know that they will retain their values until the service is terminated. This service should also implement the LocationListener interface, and now it can store the Location notified to it when onLocationChanged is invoked:
public class LocationService extends Service implements LocationListener {
private LocationManager locationManager;
private Location location;
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
Logging.i(CLAZZ, "onHandleIntent", "invoked");
if (intent.getAction().equals("startListening")) {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else {
if (intent.getAction().equals("stopListening")) {
locationManager.removeUpdates(this);
locationManager = null;
}
}
return START_STICKY;
}
#Override
public IBinder onBind(final Intent intent) {
return null;
}
public void onLocationChanged(final Location location) {
this.location = location;
// TODO this is where you'd do something like context.sendBroadcast()
}
public void onProviderDisabled(final String provider) {
}
public void onProviderEnabled(final String provider) {
}
public void onStatusChanged(final String arg0, final int arg1, final Bundle arg2) {
}
}
Now you have a service you can start and stop the location updates as you need them. This also allows you to continue to receive and process location changes even when your application is not in the foreground, if that is what you want.
You now have two choices on how to make that Location information available: Use context.sendBroadcast() to propagate the new Location to (for example) an activity, or use the bound service approach to allow other classes to invoke the exposed API and obtain the Location. See http://developer.android.com/guide/topics/fundamentals/bound-services.html for more details on creating a bound service.
Note that there are many other aspects to listening for location updates that I have not included here, for the sake of clarity.
I would offer two elegant ways to access your object from anywhere:
use a Singleton design pattern
use ProjectApp class. This class can be accessed from any activity simply by calling getApplication().
ProjectApp app = (ProjectApp)getApplication();
I used a combination of the two:
public class MyApp extends Application {
private MyLocation mMyLocation;
#Override
public void onCreate() {
super.onCreate();
mMyLocation = new MyLocation();
mMyLocation.getLocation(this, GlobalData.getInstance(), true);
}
}
You can see that GlobalData is a singleton class that implements LocationResult interface, meaning that it will send the updated location to this object.
When I need to get the updated location, I take it from GlobalData.
Here is MyLocation class implementation (I used some code from here and made some changes:
package com.pinhassi.android.utilslib;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class MyLocation {
private Timer timer1;
private LocationManager lm;
private LocationResult locationResult;
private boolean gps_enabled=false;
private boolean network_enabled=false;
private boolean mContinuesUpdates;
private int decimalAccuracy;
/**
* Class constructor
*/
public MyLocation(){
decimalAccuracy = 0;
}
public boolean getLocation(Context context, LocationResult result, boolean continuesUpdates)
{
mContinuesUpdates = continuesUpdates;
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(getDecimalAccurated(location));
if (!mContinuesUpdates)
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(getDecimalAccurated(location));
if (!mContinuesUpdates)
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
#Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(getDecimalAccurated(gps_loc));
else
locationResult.gotLocation(getDecimalAccurated(net_loc));
return;
}
if(gps_loc!=null){
locationResult.gotLocation(getDecimalAccurated(gps_loc));
return;
}
if(net_loc!=null){
locationResult.gotLocation(getDecimalAccurated(net_loc));
return;
}
locationResult.gotLocation(null);
}
}
/**
* called when the GPS returns a location.
* can be called multiple times as the location is updated
*/
public interface LocationResult {
public void gotLocation(Location location);
}
/**
* sets location result accuracy
* #param n number of places after the point. negative value or 0 means not set.
*/
public void setDecimalAccuracy(int n)
{
this.decimalAccuracy = n;
}
private Location getDecimalAccurated(Location location) {
if (decimalAccuracy > 0){
double accuracy = Math.pow(10, this.decimalAccuracy);
int ix;
ix = (int)(location.getLatitude() * accuracy);
location.setLatitude(((double)ix)/accuracy);
ix = (int)(location.getLongitude() * accuracy);
location.setLongitude(((double)ix)/accuracy);
}
return location;
}
}
I'll talk in general since I had the same issue:
How to manage the LocationListener and lit this listener access the activity ..
This was my try :
The Listener :
public class MyLocationListener implements LocationListener{
ProgressDialog dialog;
LocationManager locManager;
Context context;
public MyLocationListener (Context context,ProgressDialog dialog){
this.context = context;
this.dialog = dialog;
}
public void startSearch() {
locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// If the network provider works run it , else try GPS provider
// TODO : what happens if GPS and Network providers are not suuported ??
if(!locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) )
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
else
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
0, this);
dialog = new ProgressDialog(context);
dialog.setTitle("");
dialog.setMessage(context.getString(R.string.pleaseWait));
dialog.setButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
locManager.removeUpdates(MyLocationListener .this);
dialog.dismiss();
return;
}
});
dialog.show();
}
// Location Listener implementation
// read Android doc for more info
// this methods is triggered when new location ( latitiude and longitude ) is found by the system
private void updateWithNewLocation(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
//THIS IS MY ACTIVITY
MainActivity mainActivity = (MainActivity) context;
mainActivity.init();
} else {
//this.setSummary( "No location found" );
}
// remove the listener , we don't need it anymore
locManager.removeUpdates(this);
dialog.hide();
}
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
init the listener in the MainActivity like this :
ProgressDialog dialog;
.
.
.
new MyLocationListener (this, dialog).startSearch();
I don't know if that help ? but that was my solution ...
I've implemented code that should return present location.
First the code:
public static double[] getLocation(Context context) {
double[] result;
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
MyLocationListener ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
while(!hasLocation) { }
result = ll.getResult();
return result;
}
private static class MyLocationListener implements LocationListener {
double[] result = new double[2];
public double[] getResult() {
return result;
}
#Override
public void onLocationChanged(Location location) {
if(location.getAccuracy() != 0.0 && location.getAccuracy() < 100) {
result[0] = location.getLatitude();
result[1] = location.getLongitude();
hasLocation = true;
lm.removeUpdates(this);
}
}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
The problem is that all stopps on 'while' statement. WHen I've tried to debug this setting breakpoint on the first line in onLocationChanged() nothing happens, but Logcat was showing some logs like:
loc_eng_report_position: vertical_accuracy = 64.000000
DEBUG/libloc(1292): date:2011-08-11, time:10:51:03.372, raw_sec=1313052663, raw_sec2=1313052663,raw_msec=1313052663372
Any ideas?
The while(!hasLocation) {} is blocking your application from doing anything. You'll either need to deal with the location in the callback onLocationChanged, or you'll need to start the location manager a lot earlier and hope that you have a result by the time you need it. You can't busy-wait for the answer.
I'm beta testing my first Android app and have had a few users mention that when they attempt to lookup by GPS it hangs. In order to improve error handling around this I wanted to get the opinion of people who have apps in the wild.
My current activity does the following to kick off the lookup
findViewById(R.id.gpsButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener mlocListener = new MyLocationListener();
Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
mlocManager.requestLocationUpdates(mlocManager.getBestProvider(locationCriteria, true), 0, 0, mlocListener);
}
});
The implementation of my custom location lookup class is below
public class MyLocationListener implements LocationListener {
private boolean alreadyLocatedDevice;
private ProgressDialog dialog;
public MyLocationListener() {
this.dialog = ProgressDialog.show(LocationLookup.this, "", "Loading...");
}
#Override
public void onProviderDisabled(String provider) {
this.dialog.dismiss();
DialogHelper.showDialogWithMessageAndTitle("", "You don't currently have location services turned on", LocationLookup.this);
}
#Override
public void onLocationChanged(android.location.Location location) {
if (!alreadyLocatedDevice) {
alreadyLocatedDevice = true;
Location loc = new Location();
loc.setLng(Double.toString(location.getLongitude()));
loc.setLat(Double.toString(location.getLatitude()));
((AppDelegate) getApplicationContext()).setSelectedLocation(loc);
Intent findKioskLocation = new Intent(LocationLookup.this, FindKioskLocation.class);
this.dialog.dismiss();
startActivity(findKioskLocation);
}
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public void onProviderEnabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
And finally I've added both the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in my manifest file.
Any help would be much appreciated!
Here is a nice implementation that I recnetly looked at. Basically by default it uses GPS to get a location. If no location can be found within a certain time period or no satellites are available it switches to Network.
Hope this helps
A Stacktrace from the logcat would have helped you and other developers here understand where the problem is coming from. Try to ask the users to recreate the problem if possible and find out when it is occurring.
As for guidance with the locaton manager, Google Developers just posted a blog recently and also updated the docs on how to use location manager. Check the documentation here and also the blog post which explains it with an example. That might help you better. The blog post also explains how to use different location providers and how to be user friendly and guidance necessary in most ases when using location in Android applications.
For anyone who might follow this thread -I found a mixture of my own approach (admittedly hackish in this code example) and the one mentioned by #bear to work without any issues (plus the location lookup was fast/accurate and error free)
I found the example listed by #bear to be a little more complex than I needed. For starters I wanted to kick off the GPS lookup when a button was clicked and have a simple async task wrapping this so it would throw up a dialog/etc
Next I wanted the exact latitude and longitude (no need to pass this off to another class because my example was simply to use the lat + lng to locate a resource and plot it)
So if you can follow my untested rather copy/paste approach here goes...
Inside your activity you would spin up the service during an onclick lets say ...
LocationManager networkManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationManager gpsManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationService locationProcessor = new LocationService(YourActivityName.this, networkManager, gpsManager, dialog);
locationProcessor.onStartCommand();
Now the location service itself
package com.epicsoftware.android.global;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import com.epicsoftware.android.activity.LocationLookup;
public class LocationService {
private LocationManager networkLm;
private LocationManager gpsLm;
private LocationListener networkListener;
private LocationListener gpsListener;
private boolean isRunning;
private boolean networkLocDisabled;
private boolean gpsLocDisabled;
private Context activity;
private LocationManager tmpNetworkManager;
private LocationManager tmpGpsManager;
private Handler locationHandler;
private ProgressDialog dialog;
private boolean gpsUpdated;
private boolean done;
public LocationService(final Context activity, LocationManager networkManager, LocationManager gpsManager, ProgressDialog dialog) {
this.tmpNetworkManager = networkManager;
this.tmpGpsManager = gpsManager;
this.activity = activity;
this.dialog = dialog;
}
public void onStartCommand() {
if (!isRunning) {
isRunning = true;
startLocationListeners();
locationHandler = new Handler();
getLocationByZip.start();
}
}
private void startLocationListeners() {
networkListener = new NetworkLocationListener();
gpsListener = new GpsLocationListener();
networkLm = tmpNetworkManager;
gpsLm = tmpGpsManager;
networkLm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
gpsLm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
}
private Thread getLocationByZip = new Thread() {
public void run() {
try {
for (int i = 0; i < 15;) {
if (!locationDisabled() || !gpsUpdated) {
try {
Thread.sleep(1000);
} catch (Exception e) {
break;
}
i++;
} else {
break;
}
}
locationHandler.post(monitorTheNetworkAndGpsProviders);
} catch (Exception e) {
killService();
done = true;
}
}
};
private Runnable monitorTheNetworkAndGpsProviders = new Runnable() {
#Override
public void run() {
killService();
dialog.dismiss();
if (!done) {
done = true;
((LocationLookup) activity).warnUserThatLocationServicesAreDisabledOrFailed();
}
}
};
private boolean locationDisabled() {
if (gpsLocDisabled && networkLocDisabled) {
done = true;
((LocationLookup) activity).warnUserThatLocationServicesAreDisabledOrFailed();
return true;
} else {
return false;
}
}
private void updateDb(Double lat, Double lon) {
done = true;
((LocationLookup) activity).setLocationDataAndSpinUpNextActivity(lat, lon);
}
public void killService() {
networkLm.removeUpdates(networkListener);
gpsLm.removeUpdates(gpsListener);
}
public class NetworkLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
updateDb(location.getLatitude(), location.getLongitude());
}
}
#Override
public void onProviderDisabled(String provider) {
networkLocDisabled = true;
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public class GpsLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
gpsUpdated = true;
updateDb(location.getLatitude(), location.getLongitude());
}
}
#Override
public void onProviderDisabled(String provider) {
gpsLocDisabled = true;
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}