tags:

views:

427

answers:

3

Hey, it's me again, I've got another question regarding to basic Android programming:

How can I access the GPS for getting the current Position of the mobile phone the app is running on? How long can this take to retrieve the information? In this case the GPS might be disabled, how can I enable/disable it again.

Which permissions must be granted in the andorid manifest?

Greetings and thanks for ur answers,

poeschlorn

+4  A: 

Some answers:

Access: see LocationManager and LocationListener.

Permissions: android.permission.ACCESS_FINE_LOCATION

Demo:

public class GPSLocationManager implements LocationObservable {
private static final long INTERVAL = 10*1000;
private LocationManager locationManager;

public GPSLocationManager(LocationManager locationManager) {
this.locationManager = locationManager; 
}

 public void requestLocationUpdates(LocationListener locationListener) {
  locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 
    INTERVAL, 
    0, locationListener);
}

public void removeUpdates(LocationListener locationListener) {
  locationManager.removeUpdates(locationListener);
}

}
mkorpela
hey, thanks for your answer. But can I directly access the GPS-Position als "doubles"?
poeschlorn
+1  A: 

If the GPS is disabled by the user I think you need at least a special permission to enable it again.

Maybe instead of enabling it just show a message to the user and fall back on the network/wifi location. You get the Network location in the same way you get the GPSpositon you just have to ask for a NETWORK_PROVIDER.

Janusz
A: 

hey, thanks for the quick answers :) it helped a lot.

how can I check, that the position of the phone has changed?

I guess there's something like an eventlistener?

poeschlorn