Binding a Service only once in Android
<p>I'm implementing the in app purchase for Google Play. Basically it works binding your service to the Google Play app and making the request. The idea is</p>

<pre><code>public class BillingService extends Service implements ServiceConnection {

    ..........          

    public boolean checkBillingSupported() {
        if (boundToService){
            // make request
        }else {
            bindToPlayService();
            // queue the request
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = IMarketBillingService.Stub.asInterface(service);
        runPendingRequests();
    }

    private boolean bindToMarketBillingService() {
        try {
            boolean bindResult = bindService(
                    new Intent(Consts.MARKET_BILLING_SERVICE_ACTION),
                    this,  // ServiceConnection.
                    Context.BIND_AUTO_CREATE);

            if (bindResult) {
                return true;
            }
        } catch (SecurityException e) {
            Log.e(TAG, "Security exception: " + e);
        }
        return false;
    }
    ..........
</code></pre>

<p>But the problem is when I call checkBillingSupported() two times at the same time (while the service is unbound). Then the <code>bindToMarketBillingService()</code> is also called twice. Is there some problem in calling it twice? or should I avoid it?</p>

<p>In the second case I've considered the following solution:</p>

<pre><code>    boolean binding=false;

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = IMarketBillingService.Stub.asInterface(service);
        binding=false;
        runPendingRequests();
    }

    private boolean bindToMarketBillingService() {
        try {
            if (!binding){
                binding=true;
            }else {
                return true;
            }
            boolean bindResult = bindService(
                    new Intent(Consts.MARKET_BILLING_SERVICE_ACTION),
                    this,  // ServiceConnection.
                    Context.BIND_AUTO_CREATE);

            if (bindResult) {
                return true;
            }
        } catch (SecurityException e) {
            Log.e(TAG, "Security exception: " + e);
        }
        binding=false;
        return false;
    }
</code></pre>

<p>Is that approach correct? is guaranteed that after <code>bindResult==true</code> on <code>onServiceConnected</code> method will be called for clearing the flag? 
Thanks</p>
