文章目录
问题描述
在Android 8.0上获取定位数据,在程序切换到后台时,系统无定位数据输出。
$GNGGA,080930.174,2259.0118,N,11322.0587,E,1,24,0.56,39.0,M,-6.0,M,,*5B
$GNGGA,080931.174,2259.0118,N,11322.0587,E,1,24,0.56,38.8,M,-6.0,M,,*53
$GNGGA,080932.175,2259.0119,N,11322.0589,E,1,24,0.56,38.8,M,-6.0,M,,*5E
$GNGGA,080933.174,2259.0120,N,11322.0589,E,1,24,0.56,38.5,M,-6.0,M,,*59
$GNGGA,080934.175,2259.0120,N,11322.0589,E,1,24,0.56,38.4,M,-6.0,M,,*5E
$GNGGA,080949.377,2259.0120,N,11322.0589,E,0,0,,38.4,M,-6.0,M,,*7E
$GNGGA,080950.378,2259.0120,N,11322.0589,E,1,21,0.59,38.3,M,-6.0,M,,*5E
$GNGGA,080951.878,2259.0123,N,11322.0588,E,1,23,0.57,37.3,M,-6.0,M,,*55
$GNGGA,080952.879,2259.0125,N,11322.0588,E,1,23,0.57,36.9,M,-6.0,M,,*5A
$GNGGA,080953.879,2259.0126,N,11322.0589,E,1,24,0.54,36.6,M,-6.0,M,,*52
在9分34秒后开始丢失数据,到9分49秒重新切换程序到前台时恢复。
MainActivity.java
public class MainActivity extends AppCompatActivity {
/**
* 位置管理器
*/
private LocationManager locationManager = null;
/**
* 保存NMEA
*/
private FileOutputStream fos = null;
private TextView tvProvider = null;
private TextView tvTime = null;
private TextView tvLatitude = null;
private TextView tvLongitude = null;
private TextView tvAltitude = null;
private TextView tvBearing = null;
private TextView tvSpeed = null;
private TextView tvAccuracy = null;
private CheckBox cbSaveNmea = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
App.getInstance().setMainActivity(this);
tvProvider = findViewById(R.id.tv_provider);
tvTime = findViewById(R.id.tv_time);
tvLatitude = findViewById(R.id.tv_latitude);
tvLongitude = findViewById(R.id.tv_longitude);
tvAltitude = findViewById(R.id.tv_altitude);
tvBearing = findViewById(R.id.tv_bearing);
tvSpeed = findViewById(R.id.tv_speed);
tvAccuracy = findViewById(R.id.tv_accuracy);
cbSaveNmea = findViewById(R.id.cb_save);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 注册位置服务,获取系统位置
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
// 监听NMEA
locationManager.addNmeaListener(nmeaListener);
}
@Override
protected void onDestroy() {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
fos = null;
}
locationManager.removeNmeaListener(nmeaListener);
locationManager.removeUpdates(locationListener);
super.onDestroy();
}
/**