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

Gaussian Filter without using ConvolveOp
Soap query database
How to remove numbers from TokenStream in Lucene?
Facing difficulty while using BufferedInputStream inplace of Scanner
Sign a java applet with multiple certificates
Activity thread access
Error converting a HTTP POST response to JSON
JTable ArrayIndexOutOfBoundsException
How to edit comment template and add type or function comment in JetBrains IntelliJ IDEA?
Java unbounded counted semaphore?
I want to open source my Google App Engine Java project, is there any auth info in the Eclipse project?
CDI - Injecting Classes at runtime
how to setup a call to method of mocked object in mockito without calling the original method itself
Alternatives to java.util.concurrent.ConcurrentSkipListSet
How to like a page on fanpage on facebook without a mouse
Convert int[] array to short[] array in Java

Categories

HOME
openmp
isabelle
nullpointerexception
smarty
iterator
lodash
framework7
tizen-web-app
electronics
gps
ezpublish
enterprise-library-5
multiple-records
append
dacpac
circular-dependency
event-handling
designer
tostring
evopdf
nhibernate-envers
openedx
firefox-webextensions
scichart
autoconf
web-sql
dcevm
autosys
apache-commons-io
dynamic-featured-image
intel-pin
internet-explorer-9
nhapi
fopen
social-media
msys2
jaxb2
gitignore
unboundid
skia
binary-data
phonegap
google-cloud-endpoints-v2
titanium-mobile
gtrendsr
environment-modules
http-digest
dynamics-crm-2013
abstract-class
hilbert-curve
impersonation
document.write
arena-simulation
heightmap
reactive-cocoa-5
executenonquery
wso2carbon
user-accounts
logparser
python-webbrowser
celery-task
tropo
pg-dump
hittest
yt-project
spring-android
intrusion-detection
iad
tarjans-algorithm
appfabric-cache
unity5.2.3
id3v2
code-access-security
phpthumb
gridcontrol
sqlperformance
spring-validator
tinn-r
libstdc++
gil
coderush
venn-diagram
armcc
uiviewanimation-curve
netdna-api
adk
eclipse-templates
spyware
heartbeat
coredump
xmlspy
gdlib
html-input
revisions
database-management
django-notification
ajax-forms

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