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

implementing a “window manager” for HTML in a JSP custom tag
Making a very large Java array
Using excel as UI without VB
Scala and interfaces
JDBC Thin Driver: Invalid Packet Lenght [sic]
Standard Entitlements Model
Why is there no GIL in the Java Virtual Machine? Why does Python need one so bad?
different ways to define a method in an interface
How to tell which SHIFT key was pressed?
Best practice for parameter naming in Java constructors and simple setters
how do I change persistence.xml at run time
Is there any speed/efficiency benefit to setting a class' instance variables in a constructor rather than in their declaration if they'll have a “default” value?
Copying bulk documents (pdf files) from server to client machine with one click?
Hibernate one to zero or one mapping
Deriving Class from Generic T
Formatting file sizes in Java/JSTL [duplicate]

Categories

HOME
testing
blogger
amazon-ec2
mediawiki
lodash
dot
webstorm
paw-app
electronics
ip
retrofit
yum
paradox
ssl-client-authentication
kentor-authservices
etl
moonmail
commonmark
dtrace
fortumo
scichart
csrf-protection
chromium-embedded
format-specifiers
crosstab
emgucv
opennlp
phpfox
fifo
strncpy
unboundid
madlib
catel
asset-pipeline
environment-modules
mapdb
wixsharp
plsql-psp
web-mining
objectlistview
android-fingerprint-api
lumberjack
email-templates
smartcontracts
celery-task
datastax-startup
yii2-extension
magma
root-framework
abcpdf9
deadbolt-2
radtreelist
adxstudio-portals
gridview-sorting
connect-by
persist
master-slave
python-stackless
pickadate
fadeout
photobucket
pervasive-sql
separator
jsapi
lustre
kineticjs
csquery
nstableviewcell
camanjs
relocation
google-reader
android-radiobutton
p4java
dir
tws
pushbackinputstream
robotics-studio
browser-detection
unc
frameset
armcc
appconkit
removeclass
window-management
locationlistener
yui-datatable
actionview
dentrix
nsviewanimation
javap
ctp4
thread-local-storage
multiple-languages
django-notification

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