How To Have Service Running After Unbinding Activity
Its recommended that ever perform long task using groundwork Thread or Service. If yous have not thought how to create bones service and Thread then please read these
- How to create simple Thread
- Creating elementary service case in android
Now back to some current 1, we can perform any long task in background using service. Let you are downloading movies in your application and we run one service to get-go download. But if we did not notify near this to user then cease user volition recollect that application is not responsive. We can non update User interface from service directly to show progress in android.
so if you lot are using service in your android application , two step will make sure update of user interface when ever new data loaded inside service
- Create ane service from activity
- Then bind this service to activity using service connection
Remember These some points about service
- Service will not create new instance if information technology is already running
- When you lot demark service it will call onserviceConnected() method
- You tin not unbind or demark service if it is already stopped (either explicitly or implicitly)
Service connection volition be perform using Binder. Whenever you will demark service it will call connected method of service connection. in this method nosotros will get Service class object, and our piece of work washed. Using object of service nosotros can apply any updated value from service course
LocalService localservice ;
ServiceConnection serviceConnection = new ServiceConnection () { @Override
PUBLIC void onServiceDisconnected ( ComponentName proper noun ) {
txt_view.setText ( "Asunder !!!!! bind it for updating UI..." ;
} @Override
PUBLIC void onServiceConnected ( ComponentName name , IBinder SERVICE ) {
localservice = (( LocalService.LocalBinder ) SERVICE ) .getBinder ();
try {
txt_view.setText ( String.valueOf ( localservice. STR ));
localservice. STR = "String changes from Activit"
} grab ( Exception e ) {
}
}
} ; }
In service form nosotros will create a Binder to render the object of current service.
individual IBinder iBinder @Override
PUBLIC IBinder onBind ( Intent intent ) {
RETURN iBinder ;
}
LocalService getBinder () {
Return LocalService.this ;
}
}
Hither onBind() volition return service class object when service will continued . I have taken a string inside the service and set to TextView's background .
on calling bindservice() onserviceConnected() volition call. But it onServiceDisconnected() will never call either you telephone call unbind service or stop service. It volition merely call when service will cease stopped in memory depression situation or by OS. once service disconnected, yous will not get receive upshot any more
![]() |
Service Main Screen |
This screen have only simple UI for starting , binding, unbinding and stopping service. I started the service
private Intent intent = NULL;
private OnClickListener startService = new OnClickListener () { @Override
PUBLIC void onClick ( VIEW v ) {
intent = new Intent ( MainActivity.this ,
LocalService.course );
startService ( intent );
}
} ;
It starting time the service. At present we will bind study how bind and unbind service
Binding Sevice....while binding service we will user ServiceConnection course object
individual OnClickListener boundService = new OnClickListener () { @Override
PUBLIC void onClick ( VIEW v ) {
IF ( intent != Cypher) {
bindService ( intent , serviceConnection , BIND_AUTO_CREATE );
} ELSE {
Toast.makeText ( MainActivity.this ,
"First Satrt service to demark." Toast.LENGTH_LONG )
.evidence ();
}
}
} ;
private OnClickListener unboundService = new OnClickListener () { @Override
PUBLIC void onClick ( VIEW v ) {
IF ( intent != NULL) {
unbindService ( serviceConnection );
txt_view.setText ( "DisConnected !!!!! bind it for updating UI..." ;
} ELSE {
Toast.makeText ( MainActivity.this ,
"First Satrt bind to UnBind." Toast.LENGTH_LONG )
.show ();
}
}
} ;
individual OnClickListener stopService = new OnClickListener () { @Override
PUBLIC void onClick ( VIEW v ) {
IF ( intent != NULL) {
stopService ( intent );
} ELSE {
Toast.makeText ( MainActivity.this , "Starting time Satrt to stop it."
Toast.LENGTH_LONG ) .testify ();
}
}
} ;
Now see some screen shot to become idea how this volition work..
![]() |
Service Asunder After Unbind |
Activity Showed value from Service
Now i recall you got some thought how we can update UI from background service
Download source cod to read complete piece of code
Download All Service Binding Example
Source: http://androidtrainningcenter.blogspot.com/2012/08/updating-ui-from-background-service-in.html
Posted by: harrisonfamannot61.blogspot.com
0 Response to "How To Have Service Running After Unbinding Activity"
Post a Comment