MilanNz
MilanNz

Reputation: 1341

Stop service on Android

I can't stop my service with stopService(). I have tryed everything but it doesn't work. I stop my service only when I uninstall app.

public class Refresh 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) {
        Toast.makeText(getApplicationContext(), "NESTO!", Toast.LENGTH_LONG).show();
        Toast.makeText(this, "Your location is: "+MyLocation(), Toast.LENGTH_LONG).show();
        return START_NOT_STICKY;
    }

    @Override
    public void onCreate() {
         Intent myService = new Intent(this, Refresh.class);
         PendingIntent pendingIntent = PendingIntent.getService(this, 0,myService, 0);
         AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 20* 1000, pendingIntent);
        super.onCreate();
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

      public LatLng MyLocation(){
            LocationManager locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
            Location location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            return new LatLng(location.getLatitude(), location.getLongitude());
        }

}

And this is methods in main activity:

 public void startService(){
            startService(new Intent(getBaseContext(), Refresh.class));
        }
        public void stopService(){
            stopService(new Intent(getBaseContext(),Refresh.class));    
        }

And this is how I call methods start and stop service:

public void onClick(View V){
    stopService();
    }

Upvotes: 0

Views: 530

Answers (4)

QArea
QArea

Reputation: 4981

Alarm can restart the service. You should cancel it.

Upvotes: 0

Dawid Czerwinski
Dawid Czerwinski

Reputation: 680

You have to cancel your AlarmManager, so service will not restart every 20sec. After that you can use stopService() just please insert what service you want to stop.

Create a intent...

Intent intent = new Intent(getBaseContext(), Refresh .class);

When you want to start the service:

startService(intent);

When you want to stop the service:

stopService(intent);

and again, cancel your AlarmManager :) do it before you call stopService();

   alarm.cancel(pendingIntent);

Upvotes: 2

nobalG
nobalG

Reputation: 4620

I think the alarm manager you are using to start the service is restarting the service for you,You have to stop that alarm,Stop the alarm like this

 Intent intent = new Intent(this, YourService.class);
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
     alarmManager.cancel(pendingIntent);

pass the SAME ID FOR THE PENDING INTENT which you used while creating the pending intent

Upvotes: 2

FunkTheMonk
FunkTheMonk

Reputation: 10938

You kill the service, but the alarm is still set, you have to cancel the pending intent from the alarm manager in onDestroy

Upvotes: 2

Related Questions