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

calling a dll file in a Java EE application on linux
Do entity classes always need IDs?
Database access in struts 2
Split string based on regex but keep delimiters
Running a jar from within another jar
How do I make an int appear in LogCat in Eclipse with Java/Android?
Layout labels from left to right
Result is not random in Android
Eclipse: Dependencies for .war generation
Correct use of Singleton Pattern
Intent is not able to run
NodeJS TCP Server, onData small chunks
Flipping a multidimensional array java [closed]
How can i get device, manufacturer and model in a device that support J2ME? [closed]
Most efficient way of checking if Date object and Calendar object are in the same month
Write a file that already exists

Categories

HOME
cloud
testng
asp.net-core
deployment
listview
kde
webstorm
getelementsbytagname
sharepoint-designer
google-project-tango
uber-api
paradox
node-notifier
volttron
dax
imacros
iolanguage
oxyplot
crystal-reports-2008
nodatime
plunker
claims-based-identity
h2db
cas
phpfox
opentype
libssl
subdomains
c++-amp
javascriptcore
bosh
create-table
contact-form
overlap
revolution-slider
git-merge
xcode-extension
hue
temporary-files
android-mediaprojection
gpx
menuitem
zip4j
rails-routing
celery-task
dropbox-php
jxcore
pdfclown
gcsfuse
dwscript
cubes
parallel-data-warehouse
color-picker
heidisql
fody
etsy
blackberry-10
packagist
iad
ruby-2.2
wordml
fouc
mono-embedding
processmodel
unity3d-gui
key-management
typekit
android-imagebutton
relocation
asp.net-web-api-odata
applescript-objc
has-many-through
elliptic-curve
hippomocks
industrial
page-layout
xsockets.net
soundtouch
rabl
android-screen-support
mqx
html-editor
cassini-dev
quickdialog
cufon
mercurial-server
self-extracting
jmock
telerik-scheduler
h.323
createwindow
substrings
sef

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