I am writing an application related to the weather, and I need to get the coordinates so that the phone automatically adjusts the location, and accordingly gets the results for the area.
I wrote down something like, with the help of a button. (when pressed, the coordinates are checked). But you need to do without buttons (so that the application itself checks the current coordinates, with some interval of time). And also at the entrance to synchronize with the data (also receive coordinates at the time of entry).
I tried to cut through asintask, but something bad I probably own it (nothing happened).
Any ideas? (Desirable code)
ps - coordinates I get using LocationListener.
private void startGPS() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10, 10, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 10, 10, locationListener); } private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { lat = location.getLatitude(); lon = location.getLongitude(); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } };
requestLocationUpdatesshould be called once with the required type of provider. The second point that confuses me wildly is the empty and uselessif. Thirdly, therequestLocationUpdatesmust be inside your empty condition so that it does not crash on android 6.0+. And finally, if you subscribe to such a thing, you also need to unsubscribe, and then pick up the memory leak. Why is there an asinktask ?. RunstartGPSinonResumeand no buttons will be needed. But again, you must unsubscribe, do not forget about it. - iFr0z