GPS不会更新

时间:2011-04-29 18:10:11

标签: android gps

大家晚上好,

我正在做一个应用程序,它可以获取不同传感器的值,如加速度计,接近传感器,指南针和GPS,我将用于机器人项目的值。然而,我有一个GPS的问题,当我更改位置而不关闭活动时,GPS不会更新。实际上,如果我停止应用程序然后在另一个地方重新启动它,那么coords将是正确的,因此这确实是一个更新问题。请注意,当屏幕改变其方向时,将重新计算位置(横向 - >纵向)。但我希望它能在不触及任何东西的情况下工作,呵呵。

所以这是我的代码:

package com.pIndus.sensors;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class Sensors extends Activity implements SensorEventListener{

SensorManager sm;
LocationManager lm;
LocationListener ls;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    boolean accelSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);

    if(!accelSupported)
    {
        sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
        ((TextView)findViewById(R.id.acc)).setText("Accéléromètre non disponible");
    }

    boolean compassSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);

    if(!compassSupported)
    {
        sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION));
        ((TextView)findViewById(R.id.compass)).setText("Boussole non disponible");
    }

    boolean proxySupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_FASTEST);

    if(!proxySupported)
    {
        sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY));
        ((TextView)findViewById(R.id.proxy)).setText("Capteur de proximité non disponible");
    }

    ls = new MyLocationListener();


    lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, ls);

}

@Override
public void onStop()
{
    super.onStop();
    lm.removeUpdates(ls);
    sm.unregisterListener(this);
}

@Override
public void onPause()
{
    super.onStop();
    lm.removeUpdates(ls);
    sm.unregisterListener(this);
}



@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) 
{
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) 
{
    switch(event.sensor.getType())
    {
        case Sensor.TYPE_ACCELEROMETER:
            onAccelChanged(event);
        break;

        case Sensor.TYPE_ORIENTATION:
            onCompassChanged(event);
        break;

        case Sensor.TYPE_PROXIMITY:
            onProxyChanged(event);
        break;  
    }

}

public void onAccelChanged(SensorEvent event)
{
    float aX,aY,aZ;

    aX = event.values[0];
    aY = event.values[1];
    aZ = event.values[2];

    ((TextView)findViewById(R.id.axeX)).setText("Axe X : " + aX);
    ((TextView)findViewById(R.id.axeY)).setText("Axe Y : " + aY);
    ((TextView)findViewById(R.id.axeZ)).setText("Axe Z : " + aZ);

}

public void onCompassChanged(SensorEvent event)
{
    float azimuth,pitch,roll;

    azimuth = event.values[0];
    pitch = event.values[1];
    roll = event.values[2];

    ((TextView)findViewById(R.id.azimuth)).setText("Azimuth : " + azimuth);
    ((TextView)findViewById(R.id.pitch)).setText("Pitch : " + pitch);
    ((TextView)findViewById(R.id.roll)).setText("Roll : " + roll);
}

public void onProxyChanged(SensorEvent event)
{
    float x;

    x = event.values[0];

    ((TextView)findViewById(R.id.prox)).setText("Proximité : " + x);

}

public class MyLocationListener implements LocationListener

{

    @Override
    public void onLocationChanged(Location location) {
        double longitude, lattitude;

        longitude = location.getLongitude();
        lattitude = location.getLatitude();

        ((TextView)findViewById(R.id.longi)).setText("Longitude : " + longitude);
        ((TextView)findViewById(R.id.latti)).setText("Lattitude : " + lattitude);

         lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, ls);


    }

    @Override
    public void onProviderDisabled(String provider) {

        ((TextView)findViewById(R.id.warnGPS)).setText("GPS Desactivé");

    }

    @Override
    public void onProviderEnabled(String provider) {
        ((TextView)findViewById(R.id.warnGPS)).setText("GPS Activé");

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

}

有人对我的问题有所了解吗?

非常感谢你的帮助,祝你有一个美好的一天/傍晚/晚上。

1 个答案:

答案 0 :(得分:0)

将以下行替换为

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,1.0f,ls);