当我不动的时候,安卓GPS仍然会获得位置

时间:2014-05-20 11:47:47

标签: android gps

我正在开发跟踪应用。我有GPS模块的问题。应用必须记录路线。应用程序工作正常,但有时当设备不移动时,GPS仍然会收到 连续坐标不指示我的位置,误差在20米范围内,当我再次移动时工作正常。 请给我一些提示,可以帮助我解决这个问题。非常感谢。

我有3个calsses

1 - 这里的GPSReceiver是获取位置的方法

public void getMyLoction(){ 
    _locationManager = (LocationManager) _context.getSystemService(LOCATION_SERVICE);
    _isGPSEnabled    =_locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);     
    if (_isGPSEnabled) {
        if (_location == null) {
            _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
            if (_locationManager != null) {                 
                _location = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                setLocation(_location);
            }
        }
    }

}

2 RecordingActivity(接受coordonates形式的服务和流程)工作正常,方法评论他们做什么。

public class RecordingActivity extends FragmentActivity {

public final static String BROADCAST_ACTION = "map.trackv";
public BroadcastReceiver receiver;

private GoogleMap map;
private TextView _messageToUser;
private Coordinate _pointFromService;
private long _timeWhenStartButtonWasPressed;

private List<Coordinate> _unprocessedCoords;
private List<Coordinate> _processedCoords;

private Button _stopButton;
private Button _startButton;

private String _startRecordingDate;
private String _stopRecordingDate;
private GPSReceiver _gps;

private DataBaseOperations _dataSource;

private boolean _recording;
private boolean _gpsStatus;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recording_route);
    initActvity();

    checkIfGPSisOn();
    try {
        Runtime.getRuntime().exec("logcat -f" + " /sdcard/Logcat.txt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("nu pot", "DDDDD");
        e.printStackTrace();
    }



    receveirWork();
    IntentFilter intentFilt = new IntentFilter(BROADCAST_ACTION);
    registerReceiver(receiver, intentFilt);

}

public void checkIfGPSisOn() {
    //check on start
}

public void receveirWork() {

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            // request points and process then, 
        }

    };
}



@Override
protected void onDestroy() {
    super.onDestroy();  

    if (_stopButton.isEnabled())
    {
        stopService(new Intent(this, RecordingService.class));
        _unprocessedCoords = null;
        _processedCoords   = null;
    }
    unregisterReceiver(receiver);
}

@Override
protected void onResume() {

    if (!_stopButton.isEnabled()) {
        _startButton.setEnabled(true);
        _messageToUser.setText(Constants.PRESS_START_BUTTON);
        map.clear();
    }
    super.onResume();
}

// actiune buton start;
public void startButtonEvent(View V) {
    buttonsStateAndMessageToShow(false, true, Constants.MESSAGE_TO_WAIT);
    _timeWhenStartButtonWasPressed = System.currentTimeMillis();        
    startService(new Intent(this, RecordingService.class));

// start service to get position
}



public void stopButtonEvent(View V) {
    stopService(new Intent(this, RecordingService.class));
    // stop service         
    // save route in BD     
    // resetData;
}   
public void initActvity() {
    // init date    

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // save state       

}

}

3 RecordingServices类,ii认为这是问题所在。

public class RecordingService extends Service {

private Thread _backgroundWork;
private boolean _threadCanRun;
private GPSReceiver _gps;
private Coordinate _pointToSent;

public void onCreate() {
    super.onCreate();
    _threadCanRun = true;
    _backgroundWork = new Thread(new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            getLocationFromGPS();
            Looper.loop();

        }
    });

}

public int onStartCommand(Intent intent, int flags, int startId) {//    
    _backgroundWork.start();
    return super.onStartCommand(intent, flags, startId);
}

public void onDestroy() {
    _threadCanRun = false;
    super.onDestroy();
}

public IBinder onBind(Intent intent) {
    return null;
}

public void getLocationFromGPS() {

    while (_threadCanRun) {
        Intent _intent = new Intent(RecordingActivity.BROADCAST_ACTION);
        _gps = new GPSReceiver(this);       

        _gps.getMyLoction();
        if (_gps.getIsGPSEnabled()) {

        try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {}             
            sentPoint(_intent);

        } else {

            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {}

            _intent.putExtra("latitude", 0);
            _intent.putExtra("longitude", 0);
            _intent.putExtra("time", 0);
            _intent.putExtra("GPSstatus", false);
            sendBroadcast(_intent);
        }
    }
}


private void sentPoint(Intent _intent) {
    _pointToSent = new Coordinate(_gps.getLatitude(), _gps.getLongitude(),  _gps.getTime());
    _intent.putExtra("latitude", _pointToSent.getLatitude());
    _intent.putExtra("longitude", _pointToSent.getlongitude());
    _intent.putExtra("time", _pointToSent.getTime());
    _intent.putExtra("GPSstatus", _gps.getIsGPSEnabled());

    sendBroadcast(_intent);
    _pointToSent = null;

}

}

1 个答案:

答案 0 :(得分:0)

重复位置更新请求取决于您如何实施跟踪系统 但一般情况下(不推荐,只需更改您的请求更新率以节省客户电池使用量)您可以找到location1.distanceTo(location2)之间的距离,如果距离小于30米,则将新位置移开< / p>