Getting the Position of an Android phone by using GPS

∥☆過路亽.° 提交于 2019-12-01 01:00:25

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

}

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.

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