Java Code Examples for android.content.ServiceConnection

The following are top voted examples for showing how to use android.content.ServiceConnection. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: boohee_v5.6   File: br.java   View source code 10 votes vote down vote up
private static a b(Context context) throws Exception {
    try {
        context.getPackageManager().getPackageInfo("com.android.vending", 0);
        ServiceConnection bVar = new b();
        Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
        intent.setPackage("com.google.android.gms");
        if (context.bindService(intent, bVar, 1)) {
            try {
                c cVar = new c(bVar.a());
                a aVar = new a(cVar.a(), cVar.a(true));
                context.unbindService(bVar);
                return aVar;
            } catch (Exception e) {
                throw e;
            } catch (Throwable th) {
                context.unbindService(bVar);
            }
        } else {
            throw new IOException("Google Play connection failed");
        }
    } catch (Exception e2) {
        throw e2;
    }
}
 
Example 2
Project: springreplugin   File: PluginServiceDispatcherManager.java   View source code 6 votes vote down vote up
public ServiceDispatcher get(ServiceConnection c, Context context, Handler handler, int flags, int process) {
    synchronized (SERVICES_LOCKER) {
        ServiceDispatcher sd = null;
        ArrayMap<ServiceConnection, ServiceDispatcher> map = mServices.get(context);
        if (map != null) {
            sd = map.get(c);
        }
        if (sd == null) {
            sd = new ServiceDispatcher(c, context, handler, flags, process);
            if (map == null) {
                map = new ArrayMap<>();
                mServices.put(context, map);
            }
            map.put(c, sd);
        } else {
            sd.validate(context, handler);
        }
        return sd;
    }
}
 
Example 3
Project: springreplugin   File: PluginContext.java   View source code 6 votes vote down vote up
@Override
public void unbindService(ServiceConnection conn) {
    if (mLoader.mPluginObj.mInfo.getFrameworkVersion() <= 2) {
        // 仅框架版本为3及以上的才支持
        super.unbindService(conn);
        return;
    }
    // 先走一遍系统的逻辑
    try {
        super.unbindService(conn);
    } catch (Throwable e) {
        // Ignore
    }
    // 再走插件的unbindService
    // NOTE 由于不应重新调用context.unbind命令,故传进去的是false
    PluginServiceClient.unbindService(this, conn, false);
}
 
Example 4
Project: DroidPlugin   File: PluginManager.java   View source code 6 votes vote down vote up
@Override
public void onServiceDisconnected(ComponentName componentName) {
    Log.i(TAG, "onServiceDisconnected disconnected!");
    mPluginManager = null;

    Iterator<WeakReference<ServiceConnection>> iterator = sServiceConnection.iterator();
    while (iterator.hasNext()) {
        WeakReference<ServiceConnection> wsc = iterator.next();
        ServiceConnection sc = wsc != null ? wsc.get() : null;
        if (sc != null) {
            sc.onServiceDisconnected(componentName);
        } else {
            iterator.remove();
        }
    }
    //服务连接断开,需要重新连接。
    connectToService();
}
 
Example 5
Project: boohee_v5.6   File: ServiceChecker.java   View source code 6 votes vote down vote up
public static boolean isServiceSupport(Context context) {
    Intent intent = ServiceTalker.getAuthServiceIntent();
    ServiceConnection serviceConnection = new EmptyServiceConnection();
    boolean binded = context.bindService(intent, serviceConnection, 1);
    context.unbindService(serviceConnection);
    return binded;
}
 
Example 6
Project: TPlayer   File: ServiceConnectionDelegate.java   View source code 6 votes vote down vote up
public static IServiceConnection getDelegate(Context context, ServiceConnection connection,int flags) {
    IServiceConnection sd = null;
    if (connection == null) {
        throw new IllegalArgumentException("connection is null");
    }
    try {
        Object activityThread = ActivityThread.currentActivityThread.call();
        Object loadApk = ContextImpl.mPackageInfo.get(VirtualCore.get().getContext());
        Handler handler = ActivityThread.getHandler.call(activityThread);
        sd = LoadedApk.getServiceDispatcher.call(loadApk, connection, context, handler, flags);
    } catch (Exception e) {
        Log.e("ConnectionDelegate", "getServiceDispatcher", e);
    }
    if (sd == null) {
        throw new RuntimeException("Not supported in system context");
    }
    return getDelegate(sd);
}
 
Example 7
Project: ThunderMusic   File: MusicUtils.java   View source code 6 votes vote down vote up
public static ServiceToken bindToService(Activity context,
                                         ServiceConnection callback) {
    Activity realActivity = context.getParent();
    if (realActivity == null) {
        realActivity = context;
    }
    ContextWrapper cw = new ContextWrapper(realActivity);
    cw.startService(new Intent(cw, MediaPlaybackService.class));
    ServiceBinder sb = new ServiceBinder(callback);
    if (cw.bindService(
            (new Intent()).setClass(cw, MediaPlaybackService.class), sb, 0)) {
        sConnectionMap.put(cw, sb);
        return new ServiceToken(cw);
    }
    return null;
}
 
Example 8
Project: Phoenix-for-VK   File: UploadUtils.java   View source code 5 votes vote down vote up
/**
 * @param context  The {@link Context} to use
 * @param callback The {@link ServiceConnection} to use
 * @return The new instance of {@link ServiceToken}
 */
public static ServiceToken bindToService(final Context context, final ServiceConnection callback) {
    ContextWrapper contextWrapper = new ContextWrapper(context);
    ServiceBinder binder = new ServiceBinder(callback);
    Intent intent = new Intent().setClass(contextWrapper, UploadService.class);

    if (contextWrapper.bindService(intent, binder, 0)) {
        mConnectionMap.put(contextWrapper, binder);
        return new ServiceToken(contextWrapper);
    }

    Logger.d(TAG, "bindToService, count: " + mConnectionMap.size());
    return null;
}
 
Example 9
Project: Phoenix-for-VK   File: MusicUtils.java   View source code 5 votes vote down vote up
public static ServiceToken bindToServiceWithoutStart(final Activity realActivity, final ServiceConnection callback) {
    final ContextWrapper contextWrapper = new ContextWrapper(realActivity);
    final ServiceBinder binder = new ServiceBinder(callback);

    if (contextWrapper.bindService(new Intent().setClass(contextWrapper, MusicPlaybackService.class), binder, 0)) {
        mConnectionMap.put(contextWrapper, binder);
        return new ServiceToken(contextWrapper);
    }

    return null;
}
 
Example 10
Project: MiPushFramework   File: CondomContext.java   View source code 5 votes vote down vote up
@Override public boolean bindService(final Intent intent, final ServiceConnection conn, final int flags) {
	final boolean result = mCondom.proceed(OutboundType.BIND_SERVICE, intent, Boolean.FALSE, new CondomCore.WrappedValueProcedure<Boolean>() { @Override public Boolean proceed() {
		return CondomContext.super.bindService(intent, conn, flags);
	}});
	if (result) mCondom.logIfOutboundPass(TAG, intent, CondomCore.getTargetPackage(intent), CondomCore.CondomEvent.BIND_PASS);
	return result;
}
 
Example 11
Project: Chidori   File: ChidoriClient.java   View source code 5 votes vote down vote up
private void disconnect(final ServiceConnection serviceConnection) {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            synchronized (ChidoriClient.this) {
                try {
                    if (ApplicationHolder.getAppContext() != null && serviceConnection != null) {
                        ApplicationHolder.getAppContext().unbindService(serviceConnection);
                    }
                } catch (Exception e) {
                }
            }
        }
    }, FIVE_MINUTES);
}
 
Example 12
Project: mapbox-events-android   File: TelemetryServiceTest.java   View source code 5 votes vote down vote up
@Test
public void checksLocationReceiverIsUpWhenServiceStarted() throws Exception {
  Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), TelemetryService.class);
  final TelemetryService[] boundService = new TelemetryService[1];
  final CountDownLatch latchConnected = new CountDownLatch(1);
  ServiceConnection serviceConnection = setupServiceConnection(boundService, latchConnected);

  startService(serviceIntent);
  waitUntilServiceIsBound(serviceIntent, latchConnected, serviceConnection);

  assertLocationReceiverRegistered(boundService);
}
 
Example 13
Project: mapbox-events-android   File: TelemetryServiceTest.java   View source code 5 votes vote down vote up
@Test
public void checksTelemetryReceiverIsUpWhenServiceStarted() throws Exception {
  Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), TelemetryService.class);
  final TelemetryService[] boundService = new TelemetryService[1];
  final CountDownLatch latchConnected = new CountDownLatch(1);
  ServiceConnection serviceConnection = setupServiceConnection(boundService, latchConnected);

  startService(serviceIntent);
  waitUntilServiceIsBound(serviceIntent, latchConnected, serviceConnection);

  assertTelemetryReceiverRegistered(boundService);
}
 
Example 14
Project: mapbox-events-android   File: TelemetryServiceTest.java   View source code 5 votes vote down vote up
@Test
public void checksLocationReceiverIsDownWhenServiceStopped() throws Exception {
  Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), TelemetryService.class);
  final TelemetryService[] boundService = new TelemetryService[1];
  final CountDownLatch latchConnected = new CountDownLatch(1);
  ServiceConnection serviceConnection = setupServiceConnection(boundService, latchConnected);
  startService(serviceIntent);
  waitUntilServiceIsBound(serviceIntent, latchConnected, serviceConnection);

  stopService(serviceIntent, boundService);
  waitUntilServiceIsDestroyed();

  assertLocationReceiverNotRegistered(boundService);
}
 
Example 15
Project: mapbox-events-android   File: TelemetryServiceTest.java   View source code 5 votes vote down vote up
@Test
public void checksTelemetryReceiverIsDownWhenServiceStopped() throws Exception {
  Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), TelemetryService.class);
  final TelemetryService[] boundService = new TelemetryService[1];
  final CountDownLatch latchConnected = new CountDownLatch(1);
  ServiceConnection serviceConnection = setupServiceConnection(boundService, latchConnected);
  startService(serviceIntent);
  waitUntilServiceIsBound(serviceIntent, latchConnected, serviceConnection);

  stopService(serviceIntent, boundService);
  waitUntilServiceIsDestroyed();

  assertTelemetryReceiverNotRegistered(boundService);
}
 
Example 16
Project: mapbox-events-android   File: TelemetryServiceTest.java   View source code 5 votes vote down vote up
@Test
public void checksLocationReceiverIsDownWhenOnBackgroundCalled() throws Exception {
  Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), TelemetryService.class);
  final TelemetryService[] boundService = new TelemetryService[1];
  final CountDownLatch latchConnected = new CountDownLatch(1);
  ServiceConnection serviceConnection = setupServiceConnection(boundService, latchConnected);
  startService(serviceIntent);
  waitUntilServiceIsBound(serviceIntent, latchConnected, serviceConnection);

  backgroundService(boundService);

  assertLocationReceiverNotRegistered(boundService);
}
 
Example 17
Project: mapbox-events-android   File: TelemetryServiceTest.java   View source code 5 votes vote down vote up
@Test
public void checksTelemetryReceiverIsUpWhenOnForegroundCalled() throws Exception {
  Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), TelemetryService.class);
  final TelemetryService[] boundService = new TelemetryService[1];
  final CountDownLatch latchConnected = new CountDownLatch(1);
  ServiceConnection serviceConnection = setupServiceConnection(boundService, latchConnected);
  startService(serviceIntent);
  waitUntilServiceIsBound(serviceIntent, latchConnected, serviceConnection);
  backgroundService(boundService);

  foregroundService(boundService);

  assertLocationReceiverRegistered(boundService);
}
 
Example 18
Project: mapbox-events-android   File: TelemetryServiceTest.java   View source code 5 votes vote down vote up
@Test
public void checksOnTaskRemovedCallbackWhenOnTaskRemovedCalled() throws Exception {
  Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), TelemetryService.class);
  final TelemetryService[] boundService = new TelemetryService[1];
  final CountDownLatch latchConnected = new CountDownLatch(1);
  ServiceConnection serviceConnection = setupServiceConnection(boundService, latchConnected);
  startService(serviceIntent);
  waitUntilServiceIsBound(serviceIntent, latchConnected, serviceConnection);
  ServiceTaskCallback mockedCallback = mock(ServiceTaskCallback.class);
  boundService[0].injectServiceTask(mockedCallback);

  boundService[0].onTaskRemoved(serviceIntent);

  verify(mockedCallback, times(1)).onTaskRemoved();
}
 
Example 19
Project: chromium-for-android-56-debug-video   File: ClientManager.java   View source code 5 votes vote down vote up
/** Unbind from the KeepAlive service for a client. */
public synchronized void dontKeepAliveForSession(CustomTabsSessionToken session) {
    SessionParams params = mSessionParams.get(session);
    if (params == null || params.getKeepAliveConnection() == null) return;
    ServiceConnection connection = params.getKeepAliveConnection();
    params.setKeepAliveConnection(null);
    mContext.unbindService(connection);
}
 
Example 20
Project: letv   File: ApkManager.java   View source code 5 votes vote down vote up
public void removeServiceConnection(ServiceConnection sc) {
    Iterator<WeakReference<ServiceConnection>> iterator = this.sServiceConnection.iterator();
    while (iterator.hasNext()) {
        if (((WeakReference) iterator.next()).get() == sc) {
            iterator.remove();
        }
    }
}
 
Example 21
Project: letv   File: MediaBrowserCompat.java   View source code 5 votes vote down vote up
public void connect() {
    if (this.mState != 0) {
        throw new IllegalStateException("connect() called while not disconnected (state=" + getStateLabel(this.mState) + ")");
    } else if (this.mServiceBinderWrapper != null) {
        throw new RuntimeException("mServiceBinderWrapper should be null. Instead it is " + this.mServiceBinderWrapper);
    } else if (this.mCallbacksMessenger != null) {
        throw new RuntimeException("mCallbacksMessenger should be null. Instead it is " + this.mCallbacksMessenger);
    } else {
        this.mState = 1;
        Intent intent = new Intent(MediaBrowserServiceCompat.SERVICE_INTERFACE);
        intent.setComponent(this.mServiceComponent);
        final ServiceConnection thisConnection = new MediaServiceConnection();
        this.mServiceConnection = thisConnection;
        boolean bound = false;
        try {
            bound = this.mContext.bindService(intent, this.mServiceConnection, 1);
        } catch (Exception e) {
            Log.e(MediaBrowserCompat.TAG, "Failed binding to service " + this.mServiceComponent);
        }
        if (!bound) {
            this.mHandler.post(new Runnable() {
                public void run() {
                    if (thisConnection == MediaBrowserServiceImplBase.this.mServiceConnection) {
                        MediaBrowserServiceImplBase.this.forceCloseConnection();
                        MediaBrowserServiceImplBase.this.mCallback.onConnectionFailed();
                    }
                }
            });
        }
    }
}
 
Example 22
Project: Blockly   File: CodeGeneratorManager.java   View source code 5 votes vote down vote up
public CodeGeneratorManager(Context context) {
    this.mContext = context;
    this.mStoredRequests = new LinkedList<>();

    this.mCodeGenerationConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder binder) {
            try {
                if (!mResumed) {
                    unbind();
                } else {
                    mGeneratorService = ((CodeGeneratorService.CodeGeneratorBinder) binder)
                            .getService();

                    while (!mStoredRequests.isEmpty()) {
                        executeCodeGenerationRequest(mStoredRequests.poll());
                    }
                }
            } finally {
                mIsConnecting = false;
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mGeneratorService = null;
        }
    };
}
 
Example 23
Project: condom   File: CondomContext.java   View source code 5 votes vote down vote up
@Override public boolean bindService(final Intent intent, final ServiceConnection conn, final int flags) {
	final boolean result = mCondom.proceed(OutboundType.BIND_SERVICE, intent, Boolean.FALSE, new CondomCore.WrappedValueProcedure<Boolean>() { @Override public Boolean proceed() {
		return CondomContext.super.bindService(intent, conn, flags);
	}});
	if (result) mCondom.logIfOutboundPass(TAG, intent, CondomCore.getTargetPackage(intent), CondomCore.CondomEvent.BIND_PASS);
	return result;
}
 
Example 24
Project: condom   File: CondomApplication.java   View source code 5 votes vote down vote up
@Override public boolean bindService(final Intent intent, final ServiceConnection conn, final int flags) {
	final boolean result = mCondom.proceed(OutboundType.BIND_SERVICE, intent, Boolean.FALSE, new CondomCore.WrappedValueProcedure<Boolean>() { @Override public Boolean proceed() {
		return mApplication.bindService(intent, conn, flags);
	}});
	if (result) mCondom.logIfOutboundPass(TAG, intent, CondomCore.getTargetPackage(intent), CondomCore.CondomEvent.BIND_PASS);
	return result;
}
 
Example 25
Project: springreplugin   File: ServiceDispatcher.java   View source code 5 votes vote down vote up
ServiceDispatcher(ServiceConnection conn,
                  Context context, Handler activityThread, int flags, int process) {
    mIServiceConnection = new InnerConnection(this);
    mConnection = conn;
    mContext = context;
    mActivityThread = activityThread;
    mLocation = new ServiceConnectionLeaked(null);
    mLocation.fillInStackTrace();
    mFlags = flags;
    mProcess = process;
}
 
Example 26
Project: springreplugin   File: PluginContext.java   View source code 5 votes vote down vote up
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
    if (mLoader.mPluginObj.mInfo.getFrameworkVersion() <= 2) {
        // 仅框架版本为3及以上的才支持
        return super.bindService(service, conn, flags);
    }
    try {
        return PluginServiceClient.bindService(this, service, conn, flags, true);
    } catch (PluginClientHelper.ShouldCallSystem e) {
        // 若打开插件出错,则直接走系统逻辑
        return super.bindService(service, conn, flags);
    }
}
 
Example 27
Project: DroidPlugin   File: PluginManager.java   View source code 5 votes vote down vote up
public void removeServiceConnection(ServiceConnection sc) {
    Iterator<WeakReference<ServiceConnection>> iterator = sServiceConnection.iterator();
    while (iterator.hasNext()) {
        WeakReference<ServiceConnection> wsc = iterator.next();
        if (wsc.get() == sc) {
            iterator.remove();
        }
    }
}
 
Example 28
Project: boohee_v5.6   File: MediaBrowserCompat.java   View source code 5 votes vote down vote up
public void connect() {
    if (this.mState != 0) {
        throw new IllegalStateException("connect() called while not disconnected (state=" + getStateLabel(this.mState) + SocializeConstants.OP_CLOSE_PAREN);
    } else if (this.mServiceBinderWrapper != null) {
        throw new RuntimeException("mServiceBinderWrapper should be null. Instead it is " + this.mServiceBinderWrapper);
    } else if (this.mCallbacksMessenger != null) {
        throw new RuntimeException("mCallbacksMessenger should be null. Instead it is " + this.mCallbacksMessenger);
    } else {
        this.mState = 1;
        Intent intent = new Intent(MediaBrowserServiceCompat.SERVICE_INTERFACE);
        intent.setComponent(this.mServiceComponent);
        final ServiceConnection thisConnection = new MediaServiceConnection();
        this.mServiceConnection = thisConnection;
        boolean bound = false;
        try {
            bound = this.mContext.bindService(intent, this.mServiceConnection, 1);
        } catch (Exception e) {
            Log.e(MediaBrowserCompat.TAG, "Failed binding to service " + this.mServiceComponent);
        }
        if (!bound) {
            this.mHandler.post(new Runnable() {
                public void run() {
                    if (thisConnection == MediaBrowserServiceImplBase.this.mServiceConnection) {
                        MediaBrowserServiceImplBase.this.forceCloseConnection();
                        MediaBrowserServiceImplBase.this.mCallback.onConnectionFailed();
                    }
                }
            });
        }
    }
}
 
Example 29
Project: TPlayer   File: ServiceConnectionDelegate.java   View source code 5 votes vote down vote up
public static IServiceConnection removeDelegate(Context context, ServiceConnection conn) {
    IServiceConnection connection = null;
    try{
        Object loadApk = ContextImpl.mPackageInfo.get(VirtualCore.get().getContext());
        connection = LoadedApk.forgetServiceDispatcher.call(loadApk, context, conn);
    }catch (Exception e){
        Log.e("ConnectionDelegate", "forgetServiceDispatcher", e);
    }
    if(connection == null){
        return null;
    }
    return ServiceConnectionDelegate.removeDelegate(connection);
}
 
Example 30
Project: TPlayer   File: VActivityManager.java   View source code 5 votes vote down vote up
public int bindService(Context context, Intent service, ServiceConnection connection, int flags) {
    try {
        IServiceConnection conn = ServiceConnectionDelegate.getDelegate(context, connection, flags);
        return getService().bindService(null, null, service, null, conn, flags, 0);
    } catch (RemoteException e) {
        return VirtualRuntime.crash(e);
    }
}
 
Example 31
Project: TPlayer   File: VActivityManager.java   View source code 5 votes vote down vote up
public boolean unbindService(Context context, ServiceConnection connection) {
    try {
        IServiceConnection conn = ServiceConnectionDelegate.removeDelegate(context, connection);
        return getService().unbindService(conn, VUserHandle.myUserId());
    } catch (RemoteException e) {
        return VirtualRuntime.crash(e);
    }
}
 
Example 32
Project: android-apkbox   File: ApkContext.java   View source code 5 votes vote down vote up
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
    if (isApkLoaded()) {
        service.putExtra(ApkConstant.EXTRA_APK_PATH, loaded.getApkPath());
        service = HookService_Component.modify(service);
    }
    return super.bindService(service, conn, flags);
}
 
Example 33
Project: omnicrow-android   File: ScanFragment.java   View source code 5 votes vote down vote up
@Override
public void unbindService(ServiceConnection serviceConnection) {
    Log.d("OMNI", "scan fragment unbound from beacon service");
    if (mBeaconManager.isBound(this)) {
        getActivity().unbindService(serviceConnection);
    }
    isReadyForScan = false;
    isScanning = false;
}
 
Example 34
Project: android-runas   File: CliContext.java   View source code 4 votes vote down vote up
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
    return false;
}
 
Example 35
Project: Phoenix-for-VK   File: UploadUtils.java   View source code 4 votes vote down vote up
public ServiceBinder(final ServiceConnection callback) {
    mCallback = callback;
}
 
Example 36
Project: Phoenix-for-VK   File: MusicUtils.java   View source code 4 votes vote down vote up
public ServiceBinder(final ServiceConnection callback) {
    mCallback = callback;
}
 
Example 37
Project: Musicoco   File: PlayServiceManager.java   View source code 4 votes vote down vote up
public void bindService(ServiceConnection connection) {
    Intent intent = new Intent(context, PlayService.class);
    context.bindService(intent, connection, Service.BIND_AUTO_CREATE);
}
 
Example 38
Project: Orin   File: MusicPlayerRemote.java   View source code 4 votes vote down vote up
public ServiceBinder(final ServiceConnection callback) {
    mCallback = callback;
}
 
Example 39
Project: MiPushFramework   File: PseudoContextWrapper.java   View source code 4 votes vote down vote up
@Override public boolean bindService(Intent service, ServiceConnection conn, int flags) {
	return mBase.bindService(service, conn, flags);
}
 
Example 40
Project: MiPushFramework   File: PseudoContextWrapper.java   View source code 4 votes vote down vote up
@Override public void unbindService(ServiceConnection conn) {
	mBase.unbindService(conn);
}