나는 내 입장에 착수 중이며 사용자 위치 정보를지도에 표시하는 앱을 개발 중입니다. 우리는 매 x 변수 (x는 사용자가 5, 10, 15, 30, 60 분 중에서 선택하여 입력 한 설정)를 가져와 나중에 공유 할 수 있도록 최상의 위치를 데이터베이스에 보냅니다. 앱의 기본 흐름은 사용자가 앱을 열 때마다지도가 표시되며 최대한 빨리 현재 위치를 표시한다는 것입니다. 그 위치는 다른 사람들과 공유하기 위해 데이터베이스로 전송됩니다. 그들이 위치를 얻은 후 타이머가 시작되어 위치가 매번 다시 시작되고 30 분이 필요하다고 말한 다음 해당 위치를 DB로 보내고 타이머를 다시 시작합니다. 사용자가 앱을 시작할 때 즉시 위치를 알 수 있기 때문에 사용자가 애플리케이션에 액세스 할 때마다 타이머를 재설정해야합니다. 내가 겪고있는 문제는 백그라운드에서 자신의 위치를 얻을 수있는 응용 프로그램에 DB를 보낼 타이머를 만드는 것입니다. 또한 위치를 얻은 후 짧은 시간 동안 업데이트 수신을 중지한다는 논리를 구현하려고합니다.Android : 타이머를 사용하여 1 분 간격으로 30 분마다 위치 정보 가져 오기
현재 requestLocationUpdates() 메소드를 이해하고 사용자가 앱에 액세스 할 때 GPS와 NETWORK에서 위치 정보를 가져오고 최소 시간은 60 초, 거리는 100 피트 또는 30.48 미터입니다. . 또한 사용자가 앱을 일시 중지하고 사용자가 앱을 다시 탐색 할 때 시작됩니다. 나는 우리가 이미 업데이트를 받았음에도 불구하고 우리가 계속 업데이트를 요청하고 있음을 알 수 있습니다. 이로 인해 requestLocationUpdates는 업데이트 변경 사항을 수신 할 때 배터리 수명을 계속 사용하고 있다고 생각합니다.
위치를받은 후에 업데이트를 중지하고 특정 시간이 지나면 위치 청취를 시작하려면 어떻게해야합니까? requestLocationUpdates의 현재 안드로이드 메소드를 읽는 minTime과 minDistance는 단지 "힌트"일 뿐이지 만, 그것들을 따르지는 않습니다. 설정 매개 변수를 따르려면 locationUpdater가 필요합니다. 당신이 필요
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Session Creation and Checker */
session = new SessionManagement(getApplicationContext());
// Check if the user is logged in
session.checkLogin();
/** Setup basic session and content view */
// Set the Content View
setContentView(R.layout.activity_main);
/** Start of the MapView Elements */
// extract MapView from layout
mapView = (MapView) findViewById(R.id.mapview);
// Set a mapController
mapController = mapView.getController();
// set the map to have built in zoom controls
mapView.setBuiltInZoomControls(true);
/** Getting the device location */
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
geoCoder = new Geocoder(this);
isEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if the GPS is enabled
if (!isEnabled) {
alert.showAlertDialog(MainActivity.this, 12);
}
// Retrieve a list of location providers that have fine accuracy, no
// monetary cost, etc
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
// If no suitable provider is found, null is returned.
if (provider != null) {
provider = locationManager.getBestProvider(criteria, false);
BCT = (session.getBCT() * 60000);
// Initialize with last known location
locationManager.requestLocationUpdates(provider, BCT, 31,
locationListener);
// Check the map settings
if (session.isSatellite()) {
SatelliteChecked = true;
mapView.setSatellite(true);
}
if (session.isTraffic()) {
TrafficChecked = true;
mapView.setTraffic(true);
}
} else {
alert.showAlertDialog(getApplicationContext(), 15);
locationManager.removeUpdates(locationListener);
}
}
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location
// provider.
Toast.makeText(getApplicationContext(), location.toString(),
Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), provider.toUpperCase() + " enabled.", Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), provider.toUpperCase() + " disabled.", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onResume() {
super.onResume();
BCT = (session.getBCT() * 60000);
// when our activity resumes, we want to register for location updates
locationManager.requestLocationUpdates(
provider, BCT, 31, locationListener);
}
@Override
protected void onPause() {
super.onPause();
// when our activity pauses, we want to remove listening for location
// updates
locationManager.removeUpdates(locationListener);
}