Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove the location updates in service class in android?
    text
    copied!<p>I have a location listener class and a service class. In my location listener class I am starting GPS and I am keep getting the location every 10 seconds. My requirement is to stop the GPS location updates every day by 7PM . I am trying to stop the GPS updates in service using removeupdates but it is not working. How can i achieve this. Below is my code.</p> <p>LocationGPS.java</p> <pre><code>public class LocationGPS extends Activity implements LocationListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); public static LocationManager locationManager; locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*1000 , 0 ,this); Intent myIntent = new Intent(this, PIService.class); PendingIntent sevenPMIntent = PendingIntent.getService(this, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 19); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, sevenPMIntent); } @Override public void onLocationChanged(Location location) { Toast.makeText(getApplicationContext(), "got new location", Toast.LENGTH_SHORT).show(); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } </code></pre> <p>PIService.java</p> <pre><code>public class PIService extends Service{ @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand (Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); //Get the system current time. Date dt = new Date(); int hours = dt.getHours(); //Compare the current time with 7PM if(hours == 19){ //if current time is 7PM Toast.makeText(getApplicationContext(), "Now the time is 7PM", Toast.LENGTH_LONG).show(); LocationGPS.locationManager.removeUpdates(new LocationGPS()); //here is the place i am trying to remove the location updates. } } return START_STICKY; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload