方法onLocationChanged()没有获得不同的位置

时间:2013-07-06 11:34:25

标签: android gps location

    package com.example.pointkeeper;
        import java.util.ArrayList;

    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.util

    public class ServicePointKeeper extends Service implements LocationListener{

    double latitude;
    double longitude;
    private LocationManager lm;
    ArrayList<Point> pt;
    Point p;
    private Context context;
    private Location loc;
    private final static long TEMPO_DE_ATUALIZACAO = 1 * 60 * 1000 ;

    private final static float DISTANCIA_DE_ATUALIZACAO = 1 ;

    public void setGPS(){

        Criteria criteria = new Criteria();
        criteria.setAccuracy( Criteria.ACCURACY_FINE );
        criteria.setAltitudeRequired(true);
        String provider = lm.getBestProvider(criteria, true);

        if ( provider == null ) {
            Log.d("SistemaGPS.ativar", "Nenhum provedor encontrado.");
        } else {
            Log.d("SistemaGPS.ativar", "Provedor utilizado: " + provider);

            //lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEMPO_DE_ATUALIZACAO, DISTANCIA_DE_ATUALIZACAO , this);
            lm.requestLocationUpdates(provider, TEMPO_DE_ATUALIZACAO, DISTANCIA_DE_ATUALIZACAO , this);
        }
    }

    public void updateList(){
        p.setLatitude(loc.getLatitude());
        p.setLongitude(loc.getLongitude());
        pt.add(p);
    }


        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            loc = location;
            Toast.makeText(getApplicationContext(), "Lat: " + loc.getLatitude() + "Long: " + loc.getLongitude(), Toast.LENGTH_LONG).show();
            updateList();

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

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

        }       

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Serviço iniciado", Toast.LENGTH_SHORT).show();
        this.loc = null;
        pt = new ArrayList<Point>();
        p = new Point();
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        setGPS();
        Toast.makeText(getBaseContext(), "GPS setado", Toast.LENGTH_SHORT).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Serviço parado", Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), "Lat: " + latitude + "Long: " + longitude, Toast.LENGTH_SHORT).show();
        Intent it = new Intent(getApplicationContext(), ShowPoints.class);
        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle b = new Bundle();
        b.putParcelableArrayList("points", pt);
        it.putExtras(b);
        startActivity(it);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }



}

所以,在这段代码中,我的目的是将Points列表保存在稍后将使用的PointsList中。但所有点(纬度和经度)都具有相同的值,一旦我有第一个值,所有其他值都是相同的,它似乎永远不会调用onLocationChanged。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

你有一个全局点p,你正在覆盖每次调用updateList。由于您没有创建新点,因此会覆盖旧值。这意味着列表中的每个元素都将始终具有最新值,而不是当时的值。

另外,为什么在各处使用类变量而不是将参数传递给函数?我有一种感觉,你不太了解Java或参考。

编辑:

以下是您的代码应该是什么样子,正确使用了本地人:

package com.example.pointkeeper;
    import java.util.ArrayList;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util

public class ServicePointKeeper extends Service implements LocationListener{

private LocationManager lm;
ArrayList<Point> pt;
private final static long TEMPO_DE_ATUALIZACAO = 1 * 60 * 1000 ;

private final static float DISTANCIA_DE_ATUALIZACAO = 1 ;

public void setGPS(){

    Criteria criteria = new Criteria();
    criteria.setAccuracy( Criteria.ACCURACY_FINE );
    criteria.setAltitudeRequired(true);
    String provider = lm.getBestProvider(criteria, true);

    if ( provider == null ) {
        Log.d("SistemaGPS.ativar", "Nenhum provedor encontrado.");
    } else {
        Log.d("SistemaGPS.ativar", "Provedor utilizado: " + provider);

        //lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEMPO_DE_ATUALIZACAO, DISTANCIA_DE_ATUALIZACAO , this);
        lm.requestLocationUpdates(provider, TEMPO_DE_ATUALIZACAO, DISTANCIA_DE_ATUALIZACAO , this);
    }
}

public void updateList(Location loc){
    Point p = new Point();
    p.setLatitude(loc.getLatitude());
    p.setLongitude(loc.getLongitude());
    pt.add(p);
}


    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Lat: " + location.getLatitude() + "Long: " + location.getLongitude(), Toast.LENGTH_LONG).show();
        updateList(location);

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

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

    }       

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "Serviço iniciado", Toast.LENGTH_SHORT).show();
    pt = new ArrayList<Point>();
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    setGPS();
    Toast.makeText(getBaseContext(), "GPS setado", Toast.LENGTH_SHORT).show();
    return START_STICKY;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "Serviço parado", Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(), "Lat: " + latitude + "Long: " + longitude, Toast.LENGTH_SHORT).show();
    Intent it = new Intent(getApplicationContext(), ShowPoints.class);
    it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Bundle b = new Bundle();
    b.putParcelableArrayList("points", pt);
    it.putExtras(b);
    startActivity(it);
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}