我有问题。我只需要一个关于位置的信息。我想点击我的按钮,获取位置并停止GPS。但在我的情况下,它每5秒给我一次关于位置的信息。它可以使它工作,我想要它?因为我想在sqlite数据库中保存关于道路起点的数据。所以我只需要一个关于位置的信息。就像我的第一个想法,或者我能做的最简单的事情,就是让时间间隔大于5秒,就像5 000 000秒。但这不是我认为最好的解决方案。 :) 这是我的代码
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent dbmanager = new Intent(MainActivity.this, AndroidDatabaseManager.class);
startActivity(dbmanager);
}
});
locLIST = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
gps1.append("\n " + location.getLatitude() + " " + location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent1);
}
};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.INTERNET
},10);
return;
}else{
configureButton();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case 10:
if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED)
configureButton();
return;
}
}
private void configureButton() {
buttonGPS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
locman.requestLocationUpdates("gps", 0, 0, locLIST);
}
});
}