Example usage for android.content Context bindService

List of usage examples for android.content Context bindService

Introduction

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

Prototype

public abstract boolean bindService(@RequiresPermission Intent service, @NonNull ServiceConnection conn,
        @BindServiceFlags int flags);

Source Link

Document

Connect to an application service, creating it if needed.

Usage

From source file:org.simlar.SimlarServiceCommunicator.java

private void startServiceAndRegister(final Context context, final Class<?> activity, boolean onlyRegister) {
    mActivity = activity;/*from   ww  w.j  a  v  a2 s .  co  m*/
    final Intent intent = new Intent(context, SimlarService.class);
    if (!onlyRegister) {
        context.startService(intent);
    }
    context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    LocalBroadcastManager.getInstance(context).registerReceiver(mReceiver,
            new IntentFilter(SimlarServiceBroadcast.BROADCAST_NAME));
}

From source file:com.example.helloworldlinked.backend.HelloWorldService.java

private void bindToProviderService(Context context) {
    Utility.logDebug(TAG, "bindToProviderService");
    Intent intent = new Intent(HELLO_WORLD_PROVIDER_SERVICE_NAME);
    context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

From source file:com.idlegandalf.ledd.helper.LedDHelper.java

public LedDHelper(LedDDaemon ledDDaemon, Context appl) {
    this.context = appl;
    this.dRequests = new LinkedBlockingQueue<>();
    this.ledDDaemon = ledDDaemon;

    Intent intent = new Intent(appl, ColorService.class);
    appl.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

From source file:org.opensilk.music.playback.control.PlaybackController.java

public static PlaybackServiceConnection bindService(Context context) throws InterruptedException {
    if (context == null) {
        throw new NullPointerException("context == null");
    }/*w ww  . j  a  v  a 2 s . c om*/
    ensureNotOnMainThread(context);
    final BlockingQueue<IPlaybackService> q = new LinkedBlockingQueue<IPlaybackService>(1);
    ServiceConnection keyChainServiceConnection = new ServiceConnection() {
        volatile boolean mConnectedAtLeastOnce = false;

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (!mConnectedAtLeastOnce) {
                mConnectedAtLeastOnce = true;
                try {
                    q.put(IPlaybackService.Stub.asInterface(service));
                } catch (InterruptedException e) {
                    // will never happen, since the queue starts with one available slot
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    Intent intent = new Intent(context, PlaybackService.class);
    boolean isBound = context.bindService(intent, keyChainServiceConnection, Context.BIND_AUTO_CREATE);
    if (!isBound) {
        throw new AssertionError("could not bind to KeyChainService");
    }
    return new PlaybackServiceConnection(context, keyChainServiceConnection, q.take());
}

From source file:edu.umich.oasis.client.OASISConnection.java

public static ServiceConnection bind(final Context context, final Callback callback) {
    final ServiceConnection connection = new ServiceConnection() {
        private OASISConnection conn;

        @Override//from w w w .  ja va2s  .  com
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IOASISService service = IOASISService.Stub.asInterface(iBinder);
            conn = new OASISConnection(context, this, service);
            try {
                callback.onConnect(conn);
            } catch (Exception e) {
                Log.e(TAG, "Unhandled exception in onConnect", e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "Lost Binder connection to OASIS");
            if (callback instanceof DisconnectCallback) {
                try {
                    ((DisconnectCallback) callback).onDisconnect(conn);
                } catch (Exception e) {
                    Log.e(TAG, "Unhandled exception in onDisconnect", e);
                }
            }
            conn.closeInternal();
        }
    };

    Intent serviceIntent = new Intent();
    serviceIntent.setComponent(OASISFramework.getServiceComponent(context));
    if (context.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE
            | Context.BIND_ADJUST_WITH_ACTIVITY | Context.BIND_ABOVE_CLIENT | Context.BIND_IMPORTANT)) {
        return connection;
    } else {
        return null;
    }
}

From source file:net.kourlas.voipms_sms.Billing.java

/**
 * Initializes an instance of the Billing class.
 *
 * @param applicationContext The application context.
 *//* ww w . j ava 2s . c  o  m*/
public Billing(Context applicationContext) {
    this.applicationContext = applicationContext;

    serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            billingService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            billingService = IInAppBillingService.Stub.asInterface(service);
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    applicationContext.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}

From source file:com.swisscom.safeconnect.activity.DashboardActivity.java

private void bindVpnService() {
    Context context = getApplicationContext();
    context.bindService(new Intent(context, VpnStateService.class), mServiceConnection, BIND_AUTO_CREATE);
}

From source file:jp.co.rediscovery.arflight.ManagerFragment.java

/***
 * ARSDK????/*from  w ww  . ja  v  a 2 s  .com*/
 * #startDiscovery???
 */
private void bindServices() {
    if (discoveryServiceBinder == null) {
        final Context app = getActivity().getApplicationContext();
        final Intent intent = new Intent(app, ARDiscoveryService.class);
        app.bindService(intent, ardiscoveryServiceConnection, Context.BIND_AUTO_CREATE);
    } else if (!ardiscoveryServiceBound) {
        ardiscoveryService = ((ARDiscoveryService.LocalBinder) discoveryServiceBinder).getService();
        ardiscoveryServiceBound = true;

        ardiscoveryService.start();
    }
}

From source file:xiaofei.library.hermes.internal.Channel.java

/**
 * bind HermesService//from w  ww  .j a va 2  s .  c o m
 * @param context
 * @param packageName
 * @param service
 */
public void bind(Context context, String packageName, Class<? extends HermesService> service) {
    HermesServiceConnection connection;
    synchronized (this) {
        if (getBound(service)) {
            return;
        }
        Boolean binding = mBindings.get(service);
        if (binding != null && binding) {
            return;
        }
        mBindings.put(service, true);
        connection = new HermesServiceConnection(service);
        mHermesServiceConnections.put(service, connection);
    }
    Intent intent;
    if (TextUtils.isEmpty(packageName)) {
        intent = new Intent(context, service);
    } else {
        intent = new Intent();
        intent.setClassName(packageName, service.getName());
    }
    context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
}