通过使用GPS获得安卓手机的位置[英] Getting the Position of an Android phone by using GPS

本文是小编为大家收集整理的关于通过使用GPS获得安卓手机的位置的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个关于基本Android编程的问题:

如何访问GPS以获取应用程序正在运行的手机的当前位置?检索信息需要多长时间? 在这种情况下,GPS可能被禁用,我如何再次启用/禁用它.

在Andorid清单中必须授予哪些权限?

问候,感谢您的回答,

poeschlorn

推荐答案

一些答案​​:

访问: 请参阅 locationManager locationListener .

权限:android.permission.access_fine_location

演示:

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

}

其他推荐答案

如果用户禁用GPS,我认为您至少需要特别的权限才能再次启用它.

也许不是启用它,而只是向用户显示一条消息,然后倒在网络/wifi位置.您以相同的方式获得网络位置,您只需要要求网络_Provider即可.

本文地址:https://www.itbaoku.cn/post/102501.html

问题描述

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

推荐答案

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.