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

Mongo match in aggregate not working for date using java
Java method arguments of two types
Neo4j queries slows down but after restart are fast again
Execute linux command in java and display output to html table
Java Annotations javaArgs and LiteralArguments are deprecated
NoSuchMethodError while parsing xsd to generate classes using JAXB
Hibernate using sql call for each row to fetch relationship - instead of aggregating to one IN clause
How do I make a local variable increase consistently in a recursive function having more than one calls?
Intellij IDEA : Spark code running results in java.lang.VerifyError
JavaParser comparison
How To use setOnItemClickListner multiple times in a ListView?
NullPointer exception in a Spring Rest Controller test - Java
Most suitable Java Collection for duplicate elements
Android Custom SeekBar with custom circles
Jython--Exception in thread “main” IOError: (2, 'File not found ')
black activity giving error android studio

Categories

HOME
wso2
keras
azure-data-factory
comparison
session
bpmn
jira
setup-deployment
filtering
paw-app
win32gui
mvvmcross
constraint-programming
medical
python-unittest
spring-xd
offline
dax
vb.net-2010
windows-server-2012
vaadin7
highlight.js
ef-migrations
lucene.net
swiftlint
reverse-proxy
kryo
clickonce
crystal-reports-2010
django-storage
saas
wtx
kendo-datasource
contact-form
ghost4j
devextreme
vxworks
bitbucket-pipelines
libraries
webix-treetable
eigenvalue
s
flickr-api
taffy
windows-mobile-6.5
windows-iot-core-10
namecoin
sqldf
url-pattern
login-control
nsviewcontroller
whois
ford-fulkerson
intellij-14
ibaction
createprocessasuser
network-interface
rdl
responsive-slides
ceil
xceed-datagrid
terminal-services
multiprocessor
xsockets.net
sitemesh
apc
pygit2
eventlistener
html-editor
robotics-studio
assembly-loading
gcj
subgurim-maps
recent-documents
lpeg
w3wp.exe
zend-translate
coda-slider
mirah
database-management
substrings
lzh

Resources

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
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App