Service won't stop when stopService method is called
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).
stopService doesn't stop's my service.... why?
Have you implemented onDestroy()
? If not, I believe that might be the solution - and you stop your Timer
or whatever you're using to run the service within onDestroy()
.
A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().
See this link for some more information.
Android service does not stop when calling stopService()
you're stopping the service but the thread is still executing..you need to stop the thread .. since you have a while(true)
loop is suggest you assign a boolean variable which will be set to true on start and false on destroy and chenge the while to while(boolean_variable)
PowerShell different services to run in particular sequence (Remotely)
In the first statement:
#Stops Service A and validates its in "Stopped" status
Get-Service 'ServiceNameA' -ComputerName 'ExampleServerA' | Stop-Service -force -PassThru
You ask PowerShell to stop ServiceNameA
on a remote computer.
You then call WaitUntilServices1
which attempts to wait for a service of the same name on your local computer - which is obviously not gonna stop any time soon because you requested stopping a service on a different computer.
Change the function definition to accept a -ComputerName
parameter too and pass that to Get-Service
:
function Wait-ServiceStatus {
param(
[string]$Name,
[string]$ComputerName = $('.'),
[System.ServiceProcess.ServiceControllerStatus]$Status
)
foreach($service in Get-Service -Name $Name -ComputerName $ComputerName){
# If any call to WaitForStatus times out and throws, return $false
try { $service.WaitForStatus($Status, '00:00:30') } catch { return $false }
}
# No errors thrown while waiting, all is good, return $true
return $true
}
Now we can do:
# request the remote SCM stop the service
Get-Service 'ServiceNameA' -ComputerName 'ExampleServerA' | Stop-Service -Force
$success = Wait-ServiceStatus -Name 'ServiceNameA' -ComputerName 'ExampleServerA' -Status Stopped
if(-not $success){
# output an error
Write-Error "failed to complete restart cycle, 'ServiceNameA' on 'ExampleServerA' failed to stop in a timely manner"
# return from this script/function for good measure
return
}
# ... if we've reached this point the wait must have been successful, continue with the restart cycle.
Get-Service 'ServiceNameB' -ComputerName 'ExampleServerB' | Restart-Service -force -PassThru
$success = Wait-ServiceStatus -Name 'ServiceNameB' -ComputerName 'ExampleServerB' -Status Running
if(-not $success){
# ... etc.
}
Set-Service: Cannot stop service because it is dependent on other services
I eventually resolved this problem with the following code, which calls sc
to stop the service and then waits for it to finish stopping. This achieves the same result as expected from Set-Service -Status Stopped
; that is, when it returns the service has been stopped. (sc
on its own starts to stop the service, but does not wait until it has finished stopping.)
Start-Process "$env:WINDIR\system32\sc.exe" \\APPSERVER,stop,MyService -NoNewWindow -Wait
while ((Get-Service -ComputerName APPSERVER -Name MyService |
Select -ExpandProperty Status) -ne 'Stopped') {
Write-Host "Waiting for service to stop..."
Start-Sleep -Seconds 10
}
Related Topics
Java.Lang.Illegalargumentexception: Contains a Path Separator
Cannot Load Library: Reloc_Library[1285]: Cannot Locate 'Rand'
Blue Dot and Circle Is Not Shown on Mylocation Using Android Fused Location API
How to Make an App's Background Image Repeat
How to Handle Handler Messages When Activity/Fragment Is Paused
Unexpected Value from Nativegetenabledtags: 0
What Is "Android.R.Layout.Simple_List_Item_1"
Clear Back Stack Using Fragments
Different Font Size of Strings in the Same Textview
Creating an Android Trial Application That Expires After a Fixed Time Period
How to Apply a Style to All Buttons of an Android Application
How to Enable Tls 1.2 Support in an Android Application (Running on Android 4.1 Jb)
How to Save Hashmap to Shared Preferences
How to Create Turn-By-Turn Gps Navigation App on Android/iOS Using Google Maps