Example usage for android.content Context BIND_AUTO_CREATE

List of usage examples for android.content Context BIND_AUTO_CREATE

Introduction

In this page you can find the example usage for android.content Context BIND_AUTO_CREATE.

Prototype

int BIND_AUTO_CREATE

To view the source code for android.content Context BIND_AUTO_CREATE.

Click Source Link

Document

Flag for #bindService : automatically create the service as long as the binding exists.

Usage

From source file:com.radicaldynamic.groupinform.activities.LauncherActivity.java

@Override
protected void onResume() {
    super.onResume();

    final String tt = t + "onResume(): ";

    if (Collect.getInstance().getIoService() == null) {
        if (bindService(new Intent(LauncherActivity.this, InformOnlineService.class), mOnlineConnection,
                Context.BIND_AUTO_CREATE)) {
            if (Collect.Log.DEBUG)
                Log.d(Collect.LOGTAG, tt + "bound to InformOnlineService");
        }/*ww w  .j ava2s  .  c o  m*/
    }

    if (Collect.getInstance().getDbService() == null) {
        if (bindService(new Intent(LauncherActivity.this, DatabaseService.class), mDatabaseConnection,
                Context.BIND_AUTO_CREATE)) {
            if (Collect.Log.DEBUG)
                Log.d(Collect.LOGTAG, tt + "bound to DatabaseService");
        }
    }
}

From source file:com.sonymobile.dronecontrol.activity.MainActivity.java

private void initServices() {
    if (discoveryServiceBinder == null) {
        Intent intent = new Intent(getApplicationContext(), ARDiscoveryService.class);
        startService(intent);//w ww  .ja  va  2s  .com
        getApplicationContext().bindService(intent, ardiscoveryServiceConnection, Context.BIND_AUTO_CREATE);
    } else {
        ardiscoveryService = ((ARDiscoveryService.LocalBinder) discoveryServiceBinder).getService();
        ardiscoveryServiceBound = true;
        ardiscoveryService.start();
    }
}

From source file:com.owncloud.android.ui.preview.PreviewMediaFragment.java

private void bindMediaService() {
    Log.d(TAG, "Binding to MediaService...");
    if (mMediaServiceConnection == null) {
        mMediaServiceConnection = new MediaServiceConnection();
    }/*w  w  w  . j ava 2  s .  com*/
    getActivity().bindService(new Intent(getActivity(), MediaService.class), mMediaServiceConnection,
            Context.BIND_AUTO_CREATE);
    // follow the flow in MediaServiceConnection#onServiceConnected(...)
}

From source file:ee.ioc.phon.android.speak.RecognizerIntentActivity.java

void doBindService() {
    // This can be called also on an already running service
    startService(new Intent(this, RecognizerIntentService.class));

    bindService(new Intent(this, RecognizerIntentService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;//from www .  j a v  a  2  s .c  om
    Log.i(LOG_TAG, "Service is bound");
}

From source file:com.jesusla.google.BillingService.java

/**
 * Binds to the MarketBillingService and returns true if the bind
 * succeeded./*from  w  w w .java  2  s.c  o m*/
 * @return true if the bind succeeded; false otherwise
 */
private boolean bindToMarketBillingService() {
    try {
        if (Consts.DEBUG) {
            Log.i(TAG, "binding to Market billing service");
        }
        boolean bindResult = bindService(new Intent(Consts.MARKET_BILLING_SERVICE_ACTION), this, // ServiceConnection.
                Context.BIND_AUTO_CREATE);

        if (bindResult) {
            return true;
        } else {
            Log.e(TAG, "Could not bind to service.");
        }
    } catch (SecurityException e) {
        Log.e(TAG, "Security exception: " + e);
    }
    return false;
}

From source file:com.ebridgevas.android.ebridgeapp.messaging.mqttservice.MqttAndroidClient.java

/**
 * Connects to an MQTT server using the specified options.
 * <p>//from  w ww.  j  a v  a 2  s. c  om
 * The server to connect to is specified on the constructor. It is
 * recommended to call {@link #setCallback(MqttCallback)} prior to
 * connecting in order that messages destined for the client can be accepted
 * as soon as the client is connected.
 * </p>
 * <p>
 * The method returns control before the connect completes. Completion can
 * be tracked by:
 * <ul>
 * <li>Waiting on the returned token {@link IMqttToken#waitForCompletion()}
 * or</li>
 * <li>Passing in a callback {@link IMqttActionListener}</li>
 * </ul>
 * </p>
 * 
 * @param options
 *            a set of connection parameters that override the defaults.
 * @param userContext
 *            optional object for used to pass context to the callback. Use
 *            null if not required.
 * @param callback
 *            optional listener that will be notified when the connect
 *            completes. Use null if not required.
 * @return token used to track and wait for the connect to complete. The
 *         token will be passed to any callback that has been set.
 * @throws MqttException
 *             for any connected problems, including communication errors
 */

@Override
public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttActionListener callback)
        throws MqttException {

    IMqttToken token = new MqttTokenAndroid(this, userContext, callback);

    connectOptions = options;
    connectToken = token;

    /*
     * The actual connection depends on the service, which we start and bind
     * to here, but which we can't actually use until the serviceConnection
     * onServiceConnected() method has run (asynchronously), so the
     * connection itself takes place in the onServiceConnected() method
     */
    if (mqttService == null) { // First time - must bind to the service
        Intent serviceStartIntent = new Intent();
        serviceStartIntent.setClassName(myContext, SERVICE_NAME);
        Object service = myContext.startService(serviceStartIntent);
        if (service == null) {
            IMqttActionListener listener = token.getActionCallback();
            if (listener != null) {
                listener.onFailure(token, new RuntimeException("cannot start service " + SERVICE_NAME));
            }
        }

        // We bind with BIND_SERVICE_FLAG (0), leaving us the manage the lifecycle
        // until the last time it is stopped by a call to stopService()
        myContext.bindService(serviceStartIntent, serviceConnection, Context.BIND_AUTO_CREATE);

        if (!receiverRegistered)
            registerReceiver(this);
    } else {
        pool.execute(new Runnable() {

            @Override
            public void run() {
                doConnect();

                //Register receiver to show shoulder tap.
                if (!receiverRegistered)
                    registerReceiver(MqttAndroidClient.this);
            }

        });
    }

    return token;
}

From source file:com.vk.sdk.payments.VKIInAppBillingService.java

/**
 * Method for save transaction if you can't use
 * VKIInAppBillingService mService = new VKIInAppBillingService(IInAppBillingService.Stub.asInterface(service));
 * WARNING!!! this method must call before consume google and is it returned true
 *
 * @param apiVersion - version google apis
 * @param purchaseToken - purchase token
 * @return true is send is ok/*from w  ww.  j a  v  a 2s  . c o  m*/
 * @throws android.os.RemoteException
 */
public static boolean consumePurchaseToVk(final int apiVersion, @NonNull final String purchaseToken)
        throws android.os.RemoteException {
    if (Looper.getMainLooper().equals(Looper.myLooper())) {
        throw new RuntimeException("Network on main thread");
    }
    final Context ctx = VKUIHelper.getApplicationContext();
    if (ctx == null) {
        return false;
    }

    final PurchaseData purchaseData = new PurchaseData();

    if (!VKPaymentsServerSender.isNotVkUser()) {
        final SyncServiceConnection serviceConnection = new SyncServiceConnection() {
            @Override
            public void onServiceConnectedImpl(ComponentName name, IBinder service) {
                Object iInAppBillingService = null;

                final Class<?> iInAppBillingServiceClassStub;
                try {
                    iInAppBillingServiceClassStub = Class
                            .forName("com.android.vending.billing.IInAppBillingService$Stub");
                    Method asInterface = iInAppBillingServiceClassStub.getMethod("asInterface",
                            android.os.IBinder.class);
                    iInAppBillingService = asInterface.invoke(iInAppBillingServiceClassStub, service);
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(PARAMS_ARE_NOT_VALID_ERROR);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

                try {
                    purchaseData.purchaseData = getPurchaseData(iInAppBillingService, apiVersion,
                            ctx.getPackageName(), purchaseToken);
                } catch (Exception e) {
                    Log.e("VKSDK", "error", e);
                    purchaseData.hasError = true;
                }
            }

            @Override
            public void onServiceDisconnectedImpl(ComponentName name) {

            }
        };

        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        if (!ctx.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
            // bind
            ctx.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
            // wait bind
            synchronized (serviceConnection.syncObj) {
                while (!serviceConnection.isFinish) {
                    try {
                        serviceConnection.syncObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            // unbind
            ctx.unbindService(serviceConnection);
        } else {
            return false;
        }
    } else {
        return true;
    }

    if (purchaseData.hasError) {
        return false;
    } else if (!TextUtils.isEmpty(purchaseData.purchaseData)) {
        VKPaymentsServerSender.getInstance(ctx).saveTransaction(purchaseData.purchaseData);
    }

    return true;
}

From source file:com.rafali.flickruploader.billing.IabHelper.java

/**
 * Starts the setup process. This will start up the setup process asynchronously. You will be notified through the listener when the setup process is complete. This method is safe to call from a
 * UI thread.//w ww.j  a  va 2  s  .  c  om
 * 
 * @param listener
 *            The listener to notify when the setup process is complete.
 */
public synchronized void ensureSetup(final OnIabSetupFinishedListener listener) {
    // If already set up, can't do it again.
    if (mSetupDone) {
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
        }
        return;
    }

    // Connection to IAB service
    LOG.debug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            LOG.debug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LOG.debug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                LOG.debug("Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onIabSetupFinished(
                                new IabResult(response, "Error checking for billing v3 support."));

                    // if in-app purchases aren't supported, neither are subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                LOG.debug("In-app billing version 3 supported for " + packageName);

                // check for v3 subscriptions support
                response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                if (response == BILLING_RESPONSE_RESULT_OK) {
                    LOG.debug("Subscriptions AVAILABLE.");
                    mSubscriptionsSupported = true;
                } else {
                    LOG.debug("Subscriptions NOT AVAILABLE. Response: " + response);
                }

                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
                            "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }

            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    "Billing service unavailable on device."));
        }
    }
}

From source file:cm.aptoide.pt.ManageRepos.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_repos);

    View searchView = findViewById(R.id.search_button);
    searchView.setVisibility(View.GONE);

    loading = findViewById(R.id.loading);
    empty = findViewById(android.R.id.empty);
    empty.setVisibility(View.INVISIBLE);

    this.ctx = this;

    this.reposInserting = new HashMap<Integer, ViewDisplayRepo>();

    this.reposManager = new ReposManager();

    if (!serviceDataIsBound) {
        bindService(new Intent(this, AptoideServiceData.class), serviceDataConnection,
                Context.BIND_AUTO_CREATE);
    }//from  w  ww.  j  a  v  a  2s  . c  o m

    registerForContextMenu(this.getListView());

}

From source file:com.coinblesk.client.KeyboardFragment.java

@Override
public void onStart() {
    super.onStart();
    Intent intent = new Intent(this.getActivity(), WalletService.class);
    this.getActivity().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}