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
- From your activity, call
startService(intent)
and pass it some data representing a key to stop the service. - From your service, call
stopForeground(true)
- 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
How to Detect When the User Has Changed the Clock Time on Their Device
Android Update Textview in Thread and Runnable
Viewpager Detect When User Is Trying to Swipe Out of Bounds
Check Whether Activity Is Active
Listfragment Onlistitemclick Not Being Called
How to Open Standard Google Map Application from My Application
Broadcast Receiver Not Working After Device Reboot in Android
Android Studio 3.0 Execution Failed for Task: Unable to Merge Dex
No Good Example About Recyclerview and Staggeredgridlayoutmanager in Android Docs
Difference Between Ontouch and Onclick Android
Apply Custom Filters to Camera Output
Android Listview with Onclick Items
How to Get Data from Service to Activity
How to Create Android Spinner Without Down Triangle on the Right Side of the Widget
How to Handle Multiple Countdown Timers in Listview
How to Use Holo.Light Theme, and Fall Back to 'Light' on Pre-Honeycomb Devices
Android Listview with Checkbox and All Clickable
How to Use a Tablayout with Toolbar Inside Collapsingtoolbarlayout