List of usage examples for android.content ServiceConnection ServiceConnection
ServiceConnection
From source file:nl.sogeti.android.gpstracker.integration.GPSLoggerServiceManager.java
/** * Means by which an Activity lifecycle aware object hints about binding and unbinding * * @param onServiceConnected Run on main thread after the service is bound */// ww w . ja v a 2s. co m public void startup(Context context, final Runnable onServiceConnected) { synchronized (mStartLock) { if (!mBound) { mOnServiceConnected = onServiceConnected; mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { synchronized (mStartLock) { GPSLoggerServiceManager.this.mGPSLoggerRemote = IGPSLoggerServiceRemote.Stub .asInterface(service); mBound = true; } if (mOnServiceConnected != null) { mOnServiceConnected.run(); mOnServiceConnected = null; } } @Override public void onServiceDisconnected(ComponentName className) { synchronized (mStartLock) { mBound = false; } } }; if (ContextCompat.checkSelfPermission(context, ExternalConstants.permission.TRACKING_CONTROL) == PackageManager.PERMISSION_GRANTED) { context.bindService(createServiceIntent(), this.mServiceConnection, Context.BIND_AUTO_CREATE); } else { Log.e(this, "Did not bind service because required permission is lacking"); } } else { Log.w(this, "Attempting to connect whilst already connected"); } } }
From source file:kr.ac.kaist.resl.sensorservice.BluetoothService.java
public void invokeBLEservice() { if (null != serviceConnection && null != bleService) return;/*from w ww. j ava2 s . c o m*/ serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder service) { bleService = ((BluetoothLeService.LocalBinder) service).getService(); if (!bleService.initialize()) { Log.e(TAG, "Unable to initialize Bluetooth"); } } @Override public void onServiceDisconnected(ComponentName componentName) { bleService.close(); bleService = null; serviceConnection = null; } }; // Invoke bind the service Intent gattServiceIntent = new Intent(this, BluetoothLeService.class); bindService(gattServiceIntent, serviceConnection, BIND_AUTO_CREATE); }
From source file:org.scratch.microwebserver.MicrowebserverService.java
public void connectDispatcher() { final Intent i = new Intent("org.scratch.microwebserver"); i.setComponent(new ComponentName("org.scratch.microwebserver", "org.scratch.microwebserver.RemoteDispatcherService")); dispatchConn = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { // This is called when the connection with the service has been // established, giving us the service object we can use to // interact with the service. We are communicating with our // service through an IDL interface, so get a client-side // representation of that from the raw service object. disptachMessenger = new Messenger(service); // We want to monitor the service for as long as we are // connected to it. try { System.err.println("SEND SERVER HELLO"); Message msg = new Message(); msg.what = MessageTypes.MSG_SERVER_HELLO.ordinal(); msg.replyTo = mMessenger; disptachMessenger.send(msg); } catch (RemoteException e) { // In this case the service has crashed before we could even // do anything with it; we can count on soon being // disconnected (and then reconnected if it can be restarted) // so there is no need to do anything here. //HANDLE !!! e.printStackTrace();/*w w w .j a v a2 s .com*/ } // As part of the sample, tell the user what happened. //Toast.makeText(Binding.this, "remote service connected",Toast.LENGTH_SHORT).show(); } public void onServiceDisconnected(ComponentName className) { // This is called when the connection with the service has been // unexpectedly disconnected -- that is, its process crashed. disptachMessenger = null; // As part of the sample, tell the user what happened. // Toast.makeText(Binding.this, R.string.remote_service_disconnected, // Toast.LENGTH_SHORT).show(); } }; bindService(i, dispatchConn, Context.BIND_AUTO_CREATE); }
From source file:com.oasisfeng.nevo.decorators.bundle.BundleDecorator.java
@Override public void onCreate() { final IntentFilter filter = new IntentFilter(ACTION_BUNDLE_EXPAND); filter.addAction(ACTION_BUNDLE_CLEAR); filter.addDataScheme(SCHEME_BUNDLE); registerReceiver(mOnBundleAction, filter); mNotificationManager = NotificationManagerCompat.from(getApplication()); if (!bindService(new Intent(INotificationBundle.class.getName()).setPackage(getPackageName()), mBundlesConnection = new ServiceConnection() { @Override//from w ww .j a v a 2s . com public void onServiceConnected(final ComponentName name, final IBinder service) { mBundles = INotificationBundle.Stub.asInterface(service); } @Override public void onServiceDisconnected(final ComponentName name) { /* Should never happen */} }, BIND_AUTO_CREATE)) Log.e(TAG, "Failed to bind bundle service."); }
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"); }//ww w . j a v a 2 s . c o m 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.service.Sandbox.java
private Sandbox(int id) { mID = id;//from w w w . ja va 2s . c om mApplication = OASISApplication.getInstance(); try { Class<?> clazz = Class.forName(String.format(SandboxService.SERVICE_FORMAT, id)); PackageManager pm = mApplication.getPackageManager(); mComponent = new ComponentName(mApplication, clazz); ServiceInfo svcInfo = pm.getServiceInfo(mComponent, 0); if ((svcInfo.flags & ServiceInfo.FLAG_ISOLATED_PROCESS) == 0) { throw new RuntimeException("Sandbox " + id + " is not isolated!"); } } catch (ClassNotFoundException cnfe) { Log.e(TAG, "Could not load sandbox class", cnfe); throw new RuntimeException(cnfe); } catch (PackageManager.NameNotFoundException nnfe) { Log.e(TAG, "Service class not declared in manifest", nnfe); throw new RuntimeException(nnfe); } mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { handleConnected(service); } @Override public void onServiceDisconnected(ComponentName name) { handleDisconnected(); } }; mSandboxService = null; mTaintSet = null; mIsRestarting = false; g_onCreated.fire(this, null); }
From source file:edu.umich.flowfence.service.Sandbox.java
private Sandbox(int id) { mID = id;/* w w w . j ava 2 s .co m*/ mApplication = FlowfenceApplication.getInstance(); try { Class<?> clazz = Class.forName(String.format(SandboxService.SERVICE_FORMAT, id)); PackageManager pm = mApplication.getPackageManager(); mComponent = new ComponentName(mApplication, clazz); ServiceInfo svcInfo = pm.getServiceInfo(mComponent, 0); if ((svcInfo.flags & ServiceInfo.FLAG_ISOLATED_PROCESS) == 0) { throw new RuntimeException("Sandbox " + id + " is not isolated!"); } } catch (ClassNotFoundException cnfe) { Log.e(TAG, "Could not load sandbox class", cnfe); throw new RuntimeException(cnfe); } catch (PackageManager.NameNotFoundException nnfe) { Log.e(TAG, "Service class not declared in manifest", nnfe); throw new RuntimeException(nnfe); } mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { handleConnected(service); } @Override public void onServiceDisconnected(ComponentName name) { handleDisconnected(); } }; mSandboxService = null; mTaintSet = null; mIsRestarting = false; g_onCreated.fire(this, null); }
From source file:org.opensmc.mytracks.cyclesmc.RecordingActivity.java
void cancelRecording() { Intent rService = new Intent(this, RecordingService.class); ServiceConnection sc = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { }/*w ww . ja va2 s.c om*/ public void onServiceConnected(ComponentName name, IBinder service) { IRecordService rs = (IRecordService) service; rs.cancelRecording(); stopUpdates(); unbindService(this); } }; // This should block until the onServiceConnected (above) completes. bindService(rService, sc, Context.BIND_AUTO_CREATE); }
From source file:de.qspool.clementineremote.ui.ConnectActivity.java
/** * Connect to clementine// w w w . j a v a2s . co m */ private void connect() { // Do not connect if the activity has finished! if (this.isFinishing()) { return; } if (!mKnownIps.contains(mEtIp.getText().toString())) { mKnownIps.add(mEtIp.getText().toString()); } final String ip = mEtIp.getText().toString(); // Save the data SharedPreferences.Editor editor = mSharedPref.edit(); editor.putString(SharedPreferencesKeys.SP_KEY_IP, ip); editor.putInt(SharedPreferencesKeys.SP_LAST_AUTH_CODE, mAuthCode); editor.putStringSet(SharedPreferencesKeys.SP_KNOWN_IP, mKnownIps); editor.apply(); // Create a progress dialog mPdConnect = new MaterialDialog.Builder(this).cancelable(true).cancelListener(oclProgressDialog) .content(R.string.connectdialog_connecting).progress(true, -1).show(); // Start the service so it won't be stopped on unbindService Intent serviceIntent = new Intent(this, ClementineService.class); startService(serviceIntent); bindService(serviceIntent, new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { ClementineService.ClementineServiceBinder clementineServiceBinder = (ClementineService.ClementineServiceBinder) service; clementineServiceBinder.getClementineService().setUiHandler(mHandler); Intent connectIntent = new Intent(ConnectActivity.this, ClementineService.class); connectIntent.putExtra(ClementineService.SERVICE_ID, ClementineService.SERVICE_START); connectIntent.putExtra(ClementineService.EXTRA_STRING_IP, ip); connectIntent.putExtra(ClementineService.EXTRA_INT_PORT, getPort()); connectIntent.putExtra(ClementineService.EXTRA_INT_AUTH, mAuthCode); clementineServiceBinder.getClementineService().handleServiceAction(connectIntent); unbindService(this); } @Override public void onServiceDisconnected(ComponentName name) { } }, Context.BIND_AUTO_CREATE); }