How to determine whether GPS position has been acquired on android? - android

Let's say I have a button which calls another activity only if the system knows the EXACT user's location. I made a dialog to ask the user to turn on GPS if it's disabled, but when the user enables GPS and pushes the button again, I will probably not have the exact location yet. Can anyone help me with this ?

You need to create a LocationListener, and wait for the callback. It is pretty well described in the api docs:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// When you get here you have a location...
useTheNewLocationToEnableTheButton(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

You can listen for GpsStatus events, to make sure that user actually enabled GPS (GPS_EVENT_STARTED), and to be notified when GPS gets position fix (GPS_EVENT_FIRST_FIX).

If you don't know coordinates, the best way is to display a ProgressDialog while retrieving a location and, when you got it, dismiss ProgressDialog and launch your activity.
final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//here you can get coordinates via location.getLongitude() and
//location.getLatitude() methods, dismiss the progress dialog and
//start your activity
locationManager.removeUpdates(this);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

Related

How to get initial location with LocationManager?

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) {
}

Android locationManager requestLocationUpdates not called

I'm making an android application which keeps track of your location to eventually draw your route on a google map. Everything seems to be working except for one thing which, strangely enough, did work before. I think I did not change anything that matters to the function call and most examples I found did it the same way I did.
In my onCreate method I do the following:
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener);
later i call upon the LocationListener:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
but somehow this does not get called. Tried logging everywhere, everything returned a value except for the ones in the onLocationChanged. Doesn't work with GPS_PROVIDER either
All the relevant code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracking);
setUpMapIfNeeded();
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Tell the program to get location updates every 2 seconds(2000ms)
// I sadly don't get called..
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener);
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
System.out.Println("I'm just here for debugging purposes");
System.out.Println("Sadly I don't appear in Logcat");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
Did you add permission in manifest file?

How to observe Gps Provider continually?

I want to know when the GPS on the device is tuned on, and the location is changed.
Here is what I've done:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
Toast.makeText( getApplicationContext(),"onlocation changed",Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"providerdisabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"provideenabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
It is working fine when my app runs in the foreground, but when I close it, I won't get any more updates. I'd like to know how to get this information continuosly until my application is uninstalled.
You need to put this in a service and start the service sticky.
Also you should think about using the FusedLocationManager by using GooglePlayServices.
For more info about GooglePlayServices and locations read here

Unable to get the location updates in monodroid

I am trying to get the location in my Android app as follows
LocationManager lmanager = (LocationManager)context.GetSystemService(Context.LocationService);
if (lmanager.IsProviderEnabled(LocationManager.NetworkProvider))
{
Location currentLoc = lmanager.GetLastKnownLocation(LocationManager.NetworkProvider);
}
I tried with LocationManager.gpsProvider also instead of the Network provider.
In all the cases I am getting a null reference exception for currentLoc.
The GetLastKnownLocation() method returns the last location it knew about, which could be null if it has never received any updates before. I think what you want to do here is listen for location updates, and then respond to them once they come in. You can do this by implementing the ILocationListener interface:
class MyLocationListener : Java.Lang.Object, ILocationListener
{
public void OnLocationChanged(Location location)
{
}
public void OnProviderDisabled(string provider)
{
}
public void OnProviderEnabled(string provider)
{
}
public void OnStatusChanged(string provider, Availability status, Bundle extras)
{
}
}
In your activity you can declare that you'd like to use this class to start listening for updates like this:
var locationManager = (LocationManager)GetSystemService(LocationService);
var locationListener = new MyLocationListener();
locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 5000, 2, locationListener);

How to get user's GPS coordinates by clicking a button

i want to make application like this :
User clicks the button
The application show the user's coordinates (Latitude and Longitude)
i'm following the steps here
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
double glat = location.getLatitude();
double glong = location.getLongitude();
Toast.makeText(getApplicationContext(), "Your position\n"+glat+"\n"+glong, Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
how to implement it to button click event?
if the user clicks the button, the toast appear showing the position :)
UPDATE
I implement it using startActivityForResult() but the result is empty or null
here's my code :
this is the code on the button that i want to click
btn_obj_useKor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), MyLocationListener.class);
startActivityForResult(intent, 0);
}
});
and this is my MyLocationListener class :
public class MyLocationListener extends Activity{
Intent intent;
String output = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
double glat = location.getLatitude();
double glong = location.getLongitude();
output = glat+","+glong;
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
intent.putExtra("returnedData", output);
setResult(RESULT_OK, intent);
finish();
}
}
When i click the button it showing WTF! that means the result is null or empty.
what's the problem and what should i do then?
It wont return a result immediately since it takes time for the GPS provider to start and find your location. This is why you listen for a callback event.
Currently your code is listening for the network location, which is not very accurate. Change the LocationManager to GPS_Provider (on your last line) to use GPS if it is enabled.
Responding to an onClick is a user event, the LocationListener onLocationChanged is also an event. If you trigger the user event before the LocationListener event, there will be no position available to display.
My suggestion is to call startActivityForResult on another activity in the button onClick handler, the 2nd activity has all the LocationManager & LocationListener code in it, in the onLocationChanged event handler you pass the position back to the 1st activity.

Categories

Resources