I have tried with IntentService but listener never is called (the same code in on activity works fines)
public class ISSubProcesoGps extends IntentService { // TODO: Rename actions, choose action names that describe tasks that this // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS public static int hacambiado = 0; LocationManager lm; double longitude; double latitude; Location location = null; Criteria crit = new Criteria(); public static volatile boolean shouldContinue = true; int contador = 0; // TODO: Rename parameters public ISSubProcesoGps() { super("ISSubProcesoGps"); } /** * Starts this service to perform action Foo with the given parameters. If * the service is already performing a task this action will be queued. * * @see IntentService */ @Override protected void onHandleIntent(Intent mIntent) { LocationManager lm; double longitude1; double latitude1; Location location = null; lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } LocationListener listener = new LocationListener() { @Override public void onLocationChanged(Location location) { latitude=location.getLatitude(); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { String x =s; } }; lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener ); latitude = 0; longitude = 0; Intent intent = new Intent(getBaseContext(), MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_menu_gallery) // the status icon .setTicker("hola muy myintentservice") // the status text .setWhen(System.currentTimeMillis()) // the time stamp .setContentTitle("content title") // the label of the entry .setContentText("content teext") // the contents of the entry .setContentIntent(pendIntent) // The intent to send when the entry is clicked .build(); startForeground(1, notification); while(true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
This code is in a button (in a fragment and it does work )
LocationManager lm; Location location = null; lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } LocationListener listener = new LocationListener() { @Override public void onLocationChanged(Location location) { latitude=location.getLatitude(); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { String x =s; } }; lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener );
Problem is I need user can close the app and continues saving the location (battery is not a problem, the app is though in be connected all time)