Getting the Position of an Android phone by using GPS

混江龙づ霸主 提交于 2019-11-30 19:52:47

问题


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


回答1:


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

}



回答2:


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.



来源:https://stackoverflow.com/questions/2651499/getting-the-position-of-an-android-phone-by-using-gps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!