Service won't stop when stopService method is called
I currently have a Service that runs fine when I start it but when I try to stop it using the stopService method its onDestroy method doesn't get called.
Here is the code I use to try to stop the Service
stop_Scan_Button = (Button)findViewById(R.id.stopScanButton);
stop_Scan_Button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Log.d("DEBUGSERVICE", "Stop Button pressed");
Intent service = new Intent(CiceroEngine. CICERO_SERVICE);
releaseBind();
Log.d("Stop_Scan_Button", "Service: " + service.toString());
stopService(service);
Log.d("Stop_Scan_Button", "Service should stop! ");
}
});
Am I right in thinking that when stopService is used it calls the onDestroy method of the Service? When I press my stop scan button the onDestroy()
method in my Service is not called.
Is there anything else I am missing that I should put in to stop the service?
EDIT: to add onServiceConnected()
gets called when stopService is run instead of onServiceDisconnected()
, why would that be happening?
EDIT:To add more info regards Binding
I call bindService in the onCreate() method and I then have the releaseBind() method unbind the Service.
Here is the code for that method:
public void releaseBind(){
unbindService(this);
}
So I presume that the unbinding is not my problem?
Asked by: Max435 | Posted: 24-01-2022
Answer 1
I am going to guess that your having a method call for releaseBind()
means that you previously called bindService()
on this service and that releaseBind()
is calling unbindService()
. If my guess is incorrect, please ignore this answer.
A service will shut down after all bindService()
calls have had their corresponding unbindService()
calls. If there are no bound clients, then the service will also need stopService()
if and only if somebody called startService()
on the service.
So, there are a few possibilities here:
- You still have bound clients (e.g., other activities), in which case you cannot stop the service until they unbind
- Since both
unbindService()
andstopService()
are asynchronous, something might be going haywire with the timing, in which case you may get better luck if you callstopService()
from yourServiceConnection
'sonServiceDisconnected()
method
Also, bear in mind that the exact timing of the service being destroyed is up to Android and may not be immediate. So, for example, if you are relying upon onDestroy()
to cause your service to stop some work that is being done, consider using another trigger for that (e.g., activity calling a stopDoingStuff()
method through the service binder interface).
Answer 2
Are all your bindings closed?
A service can be used in two ways. The two modes are not entirely separate. You can bind to a service that was started with startService(). For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the service by calling bindService(). In cases like this, stopService() will not actually stop the service until the last binding is closed
.
Answered by: Clark336 | Posted: 25-02-2022Answer 3
hai guys sorry for the late answer but as far as i know i have successfull stop the service in this code: you may check a link here.
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
and in services class:
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
Answered by: Charlie348 | Posted: 25-02-2022
Similar questions
android - stopService doesn't stop's my service.... why?
i have a background service on my android APP that is getting my GPS position and sending it to a remote db. It work's fine.
The problem is when i want to stop the service.... it doesn't stops :S. Also no exception or errors on logcat have appeared... it simply doesn't stops.
this is the code to start my srvice (with a button):
startService(new Intent(GPSLoc.this, MyService.class)); //encien...
java - How to close main class for stopService
I want to close my class for stopService(); , and i am using this method but i know that it's false , please help me:
if (Main_Page.this.finish() == true){
stopService(svc);
}
Thank you ...
android stopservice after 25minutes
I have an android application that gets user location every 2 minutes using requestLocationUpdates service, so now once it reaches 25 minutes I want the running service to be stopped. Is it possible?
Thank you.
java - Android: stopService in another Activity
How can I stop my Service in another Activity?
I start the service in my summaryActivity
SocketServiceIntent = new Intent(this, SocketService.class);
SocketServiceIntent.putExtra("MessageParcelable", mp);
startService(SocketServiceIntent);
And start my statusActivity from my summaryActivity
Intent intent = new Intent(SummaryActivity.this,
StatusActiv...
android - stopService crashing the app when using Intent Actions
I have written a Service that will be running longtime sending location updates on hourly basis to Server. If user doesn't want to send location updates then he will be switching off in the app. So I am starting service (in first Activity) and wanted to stop already running service from a different Activity. So I just declared to actions for the Service in Manifest file as below.
<service android:name="....
android - stopService inside public void onClick
I created a service to start/stop a song, now when I click on "info", service stars correctly but I can't stop it. I should put an intent to stop service within "public void onClick" but I've an error.
this, is the code to stop service: stopService(new Intent(this, SobService.class));
private void Info(){
startService(new Intent(this, SobService.class));
LayoutInflater li = LayoutInflater.f...
Does stopService kill a Android service or does it wait untill it's idle?
I currently have a service that processes some stuff, and it is started with startService.
I was wondering, can I call `stopService immediately after I start the service and expect it to stop the service after the processing is done?
Or does Android kill the service when I call that command?
Android - How can I check the sender class of the "stopService" Intent?
I stop a service from various places. How can I check when the sender of the command "stopService(Intent intent)" was my NetworkReceivar class (extends BroadcastReceiver)??
This is my code for do this more clear:
public class NetworkReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
boolean isNetworkDown = intent.getBooleanExtra(ConnectivityManager.EXTRA...
android - StopService does not call onDestroy immediately
I am using the following statement to stop a download service (that extends IntentService) running in the background:
context.stopService(intent);
But this statement is not calling the onDestory() method of service immediately, It is taking sometime to call onDestroy() method of service.
@Override
public void onDestroy() {
Log.d("JSLog", "on destroy called");
su...
stopService in android not working, what don't I understand?
I have a service I'm running in my Custom View. It looks something like this:
public class CustomListView extends ListView {
private Intent mFetchThumbsIntentService;
private static Context mContext;
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public void onMyCustomViewCreate() {
mFetchThumbsIntentService = new Intent(mContext, Fe...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android