安卓:如何以编程方式获取GPS的原始数据?(不是js或html5方式)



我正在实现我的应用程序,它可以为用户获取位置。然而,我发现LocationManager并不总是适用于所有设备,你知道,有很多不同的Android设备。然后我在考虑获取原始的gps数据,并将其发送到定位网络服务以获取位置会更好。

在仔细查看了SO之后,我没能找到一个稳健的解决方案,有什么想法吗?我真的很好奇市场上的一些GPS应用程序是如何在每台设备上工作的。

代码段

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener locationListener = new MyLocationListener(locationManager);
locationListener.start();

监听器,

public class MyLocationListener implements LocationListener{
public static int PERMISSION_DENIED = 1;
public static int POSITION_UNAVAILABLE = 2;
public static int TIMEOUT = 3;
protected LocationManager locationManager;
protected boolean running = false;
public MyLocationListener(LocationManager locationManager )
{
    this.locationManager = locationManager;
}

public void onProviderDisabled(String provider) {
    Log.d(TAG, "Location provider '" + provider + "' disabled.");
}
public void onProviderEnabled(String provider) {
    Log.d(TAG, "Location provider "+ provider + " has been enabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d(TAG, "The status of the provider " + provider + " has changed");
    if (status == 0) {
        Log.d(TAG, provider + " is OUT OF SERVICE");
    }
    else if (status == 1) {
        Log.d(TAG, provider + " is TEMPORARILY_UNAVAILABLE");
    }
    else {
        Log.d(TAG, provider + " is AVAILABLE");
    }
}
public void onLocationChanged(Location location) {
    Log.d(TAG, "The location has been updated!");
    Log.d(TAG, "latitude = "+location.getLatitude()+" altitude = "+location.getAltitude());
}
public void start() {
    if (!this.running) {
        if (this.locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
            this.running = true;
            Log.d(TAG,"using gps");
            this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 0, this);
        } else {
              Log.d(TAG, "GPS provider is not available.");
        }
    }
    if (!this.running) {
        if (this.locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
            this.running = true;
            Log.d(TAG,"using network");
            this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 600000, 10, this);
        } else {
           Log.d(TAG, "Network provider is not available.");
        }
    }
}
private void stop() {
    if (this.running) {
        this.locationManager.removeUpdates(this);
        this.running = false;
    }
}
/**
 * Destroy listener.
 */
public void destroy() {
    this.stop();
}

有时,onStatusChanged什么也得不到。

LocationManager是唯一的方法。所有设备都有Android API-并不是所有设备都内置GPS模块。但您也可以使用LocationManager进行检查。

相关内容

最新更新