我正在研究我的頂點,我們正在開發一個應用程序,獲取用戶位置並將其繪製在地圖上。我們試圖獲取每個x變量的位置(x是用戶從5,10,15,30和60分鐘的選擇中輸入的設置),並將最佳位置發送到數據庫以供稍後分享。應用程序的基本流程是每次用戶打開應用程序時,他們都會看到一張地圖,並儘快顯示其當前位置。該位置然後被髮送到數據庫以與其他人共享。在他們獲得位置後,一個計時器開始每隔一段時間再次獲取他們的位置,讓他們想要30分鐘,然後將該位置發送到數據庫並再次啓動計時器。每當用戶訪問應用程序時,計時器都需要重置,因爲他們會在啓動應用程序時立即獲取它們的位置。我遇到的問題是在後臺創建一個計時器,它將獲取它們的位置,而不是在應用程序中發送到數據庫。我也想實現邏輯,在我們獲得他們的位置後,我們會停止在少量時間內收聽更新。Android:使用計時器每隔30分鐘獲取一個位置,使用1分鐘的窗口
我目前瞭解requestLocationUpdates()方法並已實現它,以便用戶訪問該應用程序,他們從GPS和NETWORK獲得位置,並且它具有60秒的最短時間和100英尺或30.48米的距離。當用戶暫停應用並在用戶導航迴應用時啓動時,這也會停止。我注意到,儘管我們已經收到更新,但我們仍然要求更新。這使我相信requestLocationUpdates在更新更新時會繼續使用電池壽命。
如何創建此邏輯以在收到位置後停止更新,但在某段時間後開始偵聽位置?讀取requestLocationUpdates的當前android方法,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);
}