背景
我正在进行一项研究,涉及开发一个收集 GNSS 测量值和 NMEA 语句的 Android 应用程序。
细节
我已成功接收和注销 GNSS 和 NMEA 数据。它似乎每秒钟更新一次。但现在我试图让它们每 60 秒更新一次,通过使用requestLocationUpdates
60000minTime
和minDistance
1000 或 10000 米,这样它只会因为minTime
.
public void measurementsClick(final View view) {
Log.d(LOG_TAG, "measurementsClick");
if (ContextCompat.checkSelfPermission(PubSubActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
Toast.makeText(PubSubActivity.this, "You have already granted this permission!", Toast.LENGTH_SHORT).show();
// Enable GNSS Listener
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (!gnssSensorStatus) {
gnssSensorStatus = true;
mLocationManager.registerGnssMeasurementsCallback(mGnssMeasurementsEventCallback);
mLocationManager.addNmeaListener(this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 100000, this);
} else {
gnssSensorStatus = false;
mLocationManager.unregisterGnssMeasurementsCallback(mGnssMeasurementsEventCallback);
mLocationManager.removeNmeaListener(this);
mLocationManager.removeUpdates(this);
}
} else {
requestStoragePermission();
}
}
问题
NMEA 的更新似乎有效,因为它对某些类型的 NMEA 句子更新大约 60 秒。但是,GNSS 测量值会继续每秒钟或更短时间更新一次。我尝试过使用不同的值以及改变我的初始化方式GnssMeasurementsEvent.Callback
,但这些都没有帮助。希望有更多见识的人可以帮助我解决这个问题。
相关代码
public class PubSubActivity extends Activity implements LocationListener, OnNmeaMessageListener {
LocationManager mLocationManager;
GnssMeasurementsEvent.Callback mGnssMeasurementsEventCallback;
public void measurementsClick(final View view) {
Log.d(LOG_TAG, "measurementsClick");
if (ContextCompat.checkSelfPermission(PubSubActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
Toast.makeText(PubSubActivity.this, "You have already granted this permission!", Toast.LENGTH_SHORT).show();
// Enable GNSS Listener
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (!gnssSensorStatus) {
gnssSensorStatus = true;
mLocationManager.registerGnssMeasurementsCallback(mGnssMeasurementsEventCallback);
mLocationManager.addNmeaListener(this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 100000, this);
} else {
gnssSensorStatus = false;
mLocationManager.unregisterGnssMeasurementsCallback(mGnssMeasurementsEventCallback);
mLocationManager.removeNmeaListener(this);
mLocationManager.removeUpdates(this);
}
} else {
requestStoragePermission();
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onNmeaMessage(String message, long timestamp) {
String nmeaStream = String.format(Locale.US, "%s, %d", message.trim(), timestamp);
try {
Log.d(LOG_TAG, nmeaStream);
// filterNmeaType(nmeaStream);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize GnssMeasurementEvent callbacks
mGnssMeasurementsEventCallback = new GnssMeasurementsEvent.Callback() {
@Override
public void onGnssMeasurementsReceived(GnssMeasurementsEvent event) {
synchronized (mFileLock) {
GnssClock gnssClock = event.getClock();
for (GnssMeasurement measurement : event.getMeasurements()) {
try {
String result = getGnssMeasurementToString(gnssClock, measurement);
Log.d(LOG_TAG, result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Log.d(LOG_TAG, "onGnssMeasurementReceived");
}
@Override
public void onStatusChanged(int status) {
Log.d(LOG_TAG, "onStatusChanged");
}
};
}
}