Hello, tell me where to dig what classes I need.

Is it possible to track the location of the device in the background mode? And how to track play services or LocationListener, but I tried without services and it cannot even find a place with the Internet, and play services track perfectly but not everyone has (or how to combine it? Or better decide on it so as not to consume the battery ?) And you can write coordinates to the database in the background, and when you connect to the Internet, did it upload them?

    1 answer 1

    The LocationListener interface and the LocationManager class are used to determine the location. First you need to create a listener object that implements the LocationListener interface:

    LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { //вызывается при обновлении данных о местоположении } @Override public void onStatusChanged(String s, int i, Bundle bundle) { //вызывается при изменении статуса указанного провайдера } @Override public void onProviderEnabled(String provider) { //вызывается при включении указанного провайдера } @Override public void onProviderDisabled(String provider) { //вызывается при выключении указанного провайдера } }; 

    In order for the listener to receive the location data, you need to request it from the system as follows:

     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); locationManager.requestLocationUpdates(provider, minTime, minDistance, locationListener); 

    The provider is one of the location providers. There are two main providers: LocationManager.GPS_PROVIDER and LocationManager.NETWORK_PROVIDER. The list of available providers can be obtained using the function:

     List<String> providersList = locationManager.getAllProviders(); 

    minTime - the time interval in milliseconds after which location updates should arrive.

    minDistance - the minimum distance at which the location is updated.

    locationListener is our location listener.

    For the application to work, you must define the following permissions in the manifest:

     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    You can check for these permissions during program execution as follows:

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //у приложения нет разрешений на определение местоположения return; } locationManager.requestLocationUpdates(provider, minTime, minDistance, locationListener); 

    To track a location in the background, services are used (class Service). Using the service, you can write data to files, and if you have an Internet connection, upload them to the server. Check Internet availability:

      ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnected()) { //интернет доступен } 
    • Because of such advisors, I have to rebuild the applications, cutting out getActiveNetworkInfo () from them, or simply refusing them in favor of others that are not "zavirous". And you can’t tell if the application needs a network to work the declared part, or to monitor and drain private information, which location information is. - bukkojot
    • @bukkojot, What do you mean by that? I want to use this application for myself. not for distribution. Are you talking about it? - Sergay
    • This is not about you, but about an adviser to surf the Internet when it is not required. - bukkojot
    • @bukkojot, in this answer there was not a word about when to surf the Internet, and when not. This is just a way to check the connection status. - Anton