Use flag 0 or BIND_AUTO_CREATE for flag bindService

Turning to bindService (Intent service, ServiceConnection conn, int flags)

Can I know when to use 0 for flags , and when should we use BIND_AUTO_CREATE for flags ? The documentation does not explain what the value 0 means for flags.

Example use of 0 as flags

 // Start auto complete service. autoCompleteServiceIntent = new Intent(AutoCompleteService.class.getName()); startService(autoCompleteServiceIntent); bindService(autoCompleteServiceIntent, serviceConnection, 0); 

Example of using BIND_AUTO_CREATE as flags

 mContext.bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE); 
+8
android
source share
3 answers

For the bindService(Intent, ServiceConnection, flag) method, if flag= Context.BIND_AUTO_CREATE , it will bind service and start the service, but if "0" , the method will return true and will not start working until a call of type startService(Intent) to start the service . One common way to use "0" is to connect to the local service if this service is running, otherwise you can start the service.

+10
source share

Semantically use BIND_AUTO_CREATE if you attach to a service whose lifetime is only valid as long as it has clients associated with it. This is because at the moment when all customers are not connected with him, he will decline.

Do not use BIND_AUTO_CREATE - or perhaps I should rephrase: it makes no sense to use BIND_AUTO_CREATE if you really just temporarily BIND_AUTO_CREATE attached to the service to request or control it, and it is reasonable that this service will live after you finish. In these cases, the binding is intended to establish a connection, and the service life cycle should be controlled using startService() and stopService() (or stopSelf() in some cases).

A commonly mentioned example of the latter case is clearly described by Google in related services docs:

"... For example, a music player might find it useful to allow its service to work indefinitely, as well as provide a link. Thus, an action can start a service to play any music, and the music continues to play even if the user leaves the application. Then, when the user returns to the application, the action can be bound to the service to restore playback control. "

In general, I would say that using the flag really distinguishes two very different types of use cases, rather than finely tuned versions of the same.

+9
source share

The answer to this question is Sourab Sharma: # / p>

1. As Saxman noted, BIND_AUTO_CREATE will only create a service if it is not already running. You must call startService () to start the service.

2. bindService() will return true if the service was successfully bound to the component, and false otherwise.

0
source share

All Articles