Stop Service in Android

stop service in android

onDestroyed()

is wrong name for

onDestroy()  

Did you make a mistake only in this question or in your code too?

How do you properly terminate a Service, stopService() not working?

If you want to stop the serivce there are two ways you can do it,

1) Call stopself() within in the service class.

2) Using stopService().

IN your case , Saying that your service class is in a diferrent package (even if it's in the same package thats fine) it's better you use aa intent filter for your service, which makes it easy to stop and start a service like the below.

<service android:name=".services.MyService" android:enabled="true">
<intent-filter android:label="@string/myService" >
<action android:name="custom.MY_SERVICE"/>
</intent-filter>
</service>

and add this in your service class

public static final String ServiceIntent = "custom.MY_SERVICE"

and then you can start or stop the service like the below.

startService(new Intent(MyService.ServiceIntent));
stopService(new Intent((MyService.ServiceIntent));

Hope this is helpfull. Thankyou

how to force stop android service programmatically

Here's a simplified description of how to stop services :

stopSelf() is used to always stop the current service.

stopSelf(int startId) is also used to stop the current service, but only if startId was the ID specified the last time the service was started.

stopService(Intent service) is used to stop services, but from outside the service to be stopped.

visit this link for more details

please replace

return START_STICKY;

by

return START_NOT_STICKY;

Difference:

START_STICKY

the system will try to re-create your service after it is killed

START_NOT_STICKY

the system will not try to re-create your service after it is killed

How to stop service by itself?

By saying "doesn't work", I guess you mean that the onDestroy()-method of the service is not invoked.

I had the same problem, because I bound some ServiceConnection to the Service itself using the flag BIND_AUTO_CREATE.
This causes the service to be kept alive until every connection is unbound.

Once I change to use no flag (zero), I had no problem killing the service by itself (stopSelf()).

Example code:

final Context appContext = context.getApplicationContext();
final Intent intent = new Intent(appContext, MusicService.class);
appContext.startService(intent);
ServiceConnection connection = new ServiceConnection() {
// ...
};
appContext.bindService(intent, connection, 0);

Killing the service (not process):

this.stopSelf();

Hope that helped.

What is the proper way to stop a service running as foreground

  1. From your activity, call startService(intent) and pass it some data representing a key to stop the service.
  2. From your service, call stopForeground(true)
  3. and then stopSelf() right after it.

Stop Service with intent

It is stopped exactly as it was started.

final Intent music_intent=new Intent(this, MediaService.class);
stopService(music_intent);

Stop service and close app

This is what I do in my app. onDestroy() method from the activity will be called when you close your app.

private ServiceConnection musicServiceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.LocalBinder binder = (MusicService.LocalBinder) service;
musicService = binder.getService();
musicService.setCallbacks(MainActivity.this);
musicServiceBound = true;
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "MusicService service disconnected (unbinded)");
musicServiceBound = false;
}
};

@Override
protected void onStart() {
super.onStart();
Intent intent1 = new Intent(this, MusicService.class);
bindService(intent1, musicServiceConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onDestroy() {
super.onDestroy()
if(musicServiceBound){
musicService.stopSelf();
unbindService(musicServiceConnection);
}
}

You wrote myService(), where you are creating another service using ().
For closing your app programmatically you can refer to this question.



Related Topics



Leave a reply



Submit