java


Don't destroy a bound Service on Activity destroy


Currently, I need a bound (Music)Service, because I need to interact with it. But I also want it to not be stopped, even when all components have unbound themselves.
As the Android Developer Guide says
"[...] Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed."
The Guide also says
"[...] your service can work both ways—it can be started (to run indefinitely) and also allow binding."
In my Application, the Service is started when the Application starts.
I want to have this service destroyed only by a user-click on a close-button I am displaying in a custom notification.But currently, when I am destroying my MainActivity the service also stops.
I've also read Answers to similar Questions this topic i found here on Stackoverflow and google, but they couldn't help me. Maybe I am missing something but I can't figure it out.
This is where I am now, this is called when I want to create my Service:
public void createServiceConnection(){
musicConnection = new ServiceConnection(){
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder)service;
musicSrv = binder.getService();
attachMusicService();
}
};
}
...wich calls this:
public void attachMusicService(){
playerFragment.setMusicService(musicSrv);
musicSrv.attach(context); //need this for my listeners, nevermind
bindService(context);
}
...wich calls this:
public void bindService(Context act){
if(playIntent==null){
playIntent = new Intent(act, MusicService.class);
act.startService(playIntent);
act.bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
}else{
act.startService(playIntent);
act.bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
}
//finished. I can do stuff with my Service here.
}
Have I misunderstood something?
I feel like the service should keep running, even the activity is destroyed, because I first made an started Service and then bound to it.

Bind to your service from custom Application class. I don't think you can keep service alive after activity that's bound to it is destroyed (when onDestroy is called). You can keep service alive if activity pauses (onPause) by calling startForeground from service

Seems like the code was correct.
According to this Question I found out that my problem was the notification I displayed, wich is pretty interesting.
Seems like that a Service that is created for running indefinitely needs to have a Notification wich is displayed by startForeground(NOTIFY_ID, notification);.
I showed my notification with notificationmanager.notify(NOTIFY_ID, notification); before, now I have
`notificationmanager.notify(NOTIFY_ID, notification);
startForeground(NOTIFY_ID, notification);`
and the service won't stop anymore after all my bound Activities are destroyed.


Related Links

JSOUP Redirect Script
Change number of split
Can´t create entity in MongoDB
Log4j2 LogManager.getLogger() with Spring
How to add information to existing text file in java
Logging Features/Functions Used In an Application
Hibernate OneToOne BiDirectional Optional Relationship: Works when inserted without optional object, Breaks when updated with new optional object
getDocument() constantly returns a null value
How to capture requestor IP address in play java framework?
Why is the bottom of my fragment not displaying?
Java Android Studio libGDX Api 16 Crash
My JFrame shows up after running a “while” loop. I want the JFrame to show while it's running
Bad vs good practice field access android
Ignoring invalid SSL certificates on Cordova for Android
Connecting to WebSocket server via .net.Socket java\android
Assetmanager and Skin dispose of Font(/Skin)

Categories

HOME
sendgrid
ionic-framework
keras
iterator
iot
spagobi
jira
infragistics
c#-2.0
dxl
blueprintjs
ezpublish
serverspec
podio
applepay
gitpitch
here-api
nano-server
numeral.js
spring-tool-suite
try-catch
google-cloud-speech
firefox-webextensions
scichart
emulator
predix
functional-testing
mps
extjs5
kudan
pepper
google-cloud-nl
librato
microsoft-chart-controls
siesta-swift
scorm2004
http-get
winrt-xaml-toolkit
android-kernel
io-redirection
azure-ml
android-tabhost
gzipstream
texmaker
vao
netcdf4
slick-3.0
convertapi
btrace
qwt
fault
hibernate-tools
strptime
jedis
dropbox-php
tizen-native-app
integrity
gabor-filter
forever
csound
font-size
np-complete
akka-cluster
gcsfuse
eventkit
jquery-filter
sts-springsourcetoolsuite
etsy
plottable.js
simplewebrtc
pickadate
asp.net5
pyke
ios8-today-widget
icu4j
django-unittest
wordml
fouc
oberon
lustre
qcodo
googlemock
fluentautomation
jubula
valuechangelistener
comexception
marmalade
datagridviewcolumn
android-2.2-froyo
typo3-neos
fmod
ccss
dir
opcache
sqlperformance
mqx
eventlistener
isnullorempty
dotnethighcharts
ohm
punbb
zpt
external-accessory
deobfuscation
rfc1123
yui-datatable
xfbml
compiler-specific
project-hosting
thread-local-storage
w3wp





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm