Example usage for android.content Context unbindService

List of usage examples for android.content Context unbindService

Introduction

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

Prototype

public abstract void unbindService(@NonNull ServiceConnection conn);

Source Link

Document

Disconnect from an application service.

Usage

From source file:Main.java

public static void unbindService(Context context, ServiceConnection conn) {
    context.unbindService(conn);
}

From source file:eu.andlabs.studiolounge.gcp.GCPService.java

public static void unbind(Context ctx, Lounge lounge) {
    ctx.unbindService(lounge);
}

From source file:org.simlar.SimlarServiceCommunicator.java

public void unregister(final Context context) {
    LocalBroadcastManager.getInstance(context).unregisterReceiver(mReceiver);
    if (mService != null) {
        context.unbindService(mConnection);
    }/* w ww . j a  v a2s  .co  m*/
}

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

private void unbindFromProviderService(Context context) {
    context.unbindService(mConnection);
}

From source file:nl.sogeti.android.gpstracker.integration.GPSLoggerServiceManager.java

/**
 * Means by which an Activity lifecycle aware object hints about binding and unbinding
 *///www . j  av  a2 s .c o m
public void shutdown(Context context) {
    synchronized (mStartLock) {
        try {
            if (mBound) {
                context.unbindService(this.mServiceConnection);
                GPSLoggerServiceManager.this.mGPSLoggerRemote = null;
                mServiceConnection = null;
                mBound = false;
            }
        } catch (IllegalArgumentException e) {
            Log.w(this, "Failed to unbind a service, prehaps the service disapearded?", e);
        }
    }
}

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

/**
 * unbind HermesService/*from w  w  w .  j  a  va  2 s.  co m*/
 * @param context
 * @param service
 */
public void unbind(Context context, Class<? extends HermesService> service) {
    synchronized (this) {
        Boolean bound = mBounds.get(service);
        if (bound != null && bound) {
            HermesServiceConnection connection = mHermesServiceConnections.get(service);
            if (connection != null) {
                context.unbindService(connection);
            }
            mBounds.put(service, false);
        }
    }
}

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

/**
 * ARSDK????//from   ww  w. ja v a  2s  .  co  m
 * #stopDiscovery???
 */
private void unbindServices() {
    if (ardiscoveryServiceBound) {
        ardiscoveryServiceBound = false;
        final Activity activity = getActivity();
        if (activity == null)
            return;
        final Context app_context = activity.getApplicationContext();
        queueEvent(new Runnable() {
            @Override
            public void run() {
                try {
                    ardiscoveryService.stop();
                    app_context.unbindService(ardiscoveryServiceConnection);
                    discoveryServiceBinder = null;
                    ardiscoveryService = null;
                } catch (final Exception e) {
                    Log.w(TAG, e);
                }
            }
        });
    }
}

From source file:com.mediatek.systemupdate.HttpManager.java

private void doUnbindService(Context context) {
    Xlog.i(TAG, "doUnbindService");
    if (mIsBound) {
        mService = null;//from   w  ww . j  a  v a  2  s  .c  o  m
        if (context != null) {
            context.unbindService(mConnection);
        }
        mIsBound = false;
    }
}

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/*ww w .j  a va 2  s  .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.nick.scalpel.core.AutoBindWirer.java

@Override
public void wire(final Context context, final Object object, final Field field) {
    ReflectionUtils.makeAccessible(field);
    Object fieldObject = ReflectionUtils.getField(field, object);
    if (fieldObject != null)
        return;/*from  w w w  .  j  a  va 2 s .  c  o  m*/

    // FIXME: 21/03/16 Ensure it is an AIDL service.
    boolean isIInterface = field.getType().isInterface();
    Preconditions.checkState(isIInterface,
            "Field:" + field.getName() + " is not an AIDL interface, is:" + field.getType());

    AutoBind autoBind = field.getAnnotation(AutoBind.class);
    String action = autoBind.action();
    String pkg = autoBind.pkg();
    int flags = autoBind.flags();
    String callback = autoBind.callback();
    boolean startService = autoBind.startService();
    boolean isExplicit = !TextUtils.isEmpty(action) && !TextUtils.isEmpty(pkg);
    Preconditions.checkState(isExplicit, "Action and PackageName should be specified");

    boolean autoUnbind = autoBind.autoUnbind();
    boolean isActivity = object instanceof Activity;
    Preconditions.checkState(!autoUnbind || isActivity, "Auto unbind only work for activities.");

    AutoBind.Callback callbackInstance = null;
    if (!TextUtils.isEmpty(callback)) {
        Object callbackObject = null;
        switch (callback) {
        case "this":
            callbackObject = object;
            break;
        default:
            Field callbackField = ReflectionUtils.findField(object, callback);
            if (callbackField != null) {
                ReflectionUtils.makeAccessible(callbackField);
                callbackObject = ReflectionUtils.getField(callbackField, object);
            }
        }
        boolean isCallback = callbackObject instanceof AutoBind.Callback;
        Preconditions.checkState(isCallback, "Field:" + callback + " is not an instance of Callback.");
        callbackInstance = (AutoBind.Callback) callbackObject;
    }

    final Intent intent = new Intent(action);
    intent.setPackage(pkg);

    if (startService)
        context.startService(intent);

    final AutoBind.Callback finalCallbackInstance = callbackInstance;
    final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Class serviceClass = field.getType();
            String stubClassName = serviceClass.getName() + "$Stub";
            try {
                Class stubClass = Class.forName(stubClassName);
                Method asInterface = ReflectionUtils.findMethod(stubClass, "asInterface", IBinder.class);
                Object result = ReflectionUtils.invokeMethod(asInterface, null, service);
                ReflectionUtils.setField(field, object, result);
                // Callback result.
                if (finalCallbackInstance != null)
                    finalCallbackInstance.onServiceBound(name, this, intent);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (finalCallbackInstance != null)
                finalCallbackInstance.onServiceDisconnected(name);
        }
    };
    //noinspection ResourceType
    context.bindService(intent, connection, flags);

    if (autoUnbind) {
        final String fieldName = field.getName();
        boolean registered = mLifeCycleManager
                .registerActivityLifecycleCallbacks(new LifeCycleCallbackAdapter() {
                    @Override
                    public void onActivityDestroyed(Activity activity) {
                        super.onActivityDestroyed(activity);
                        if (activity == object) {
                            logV("unBind service for: " + fieldName);
                            context.unbindService(connection);
                            mLifeCycleManager.unRegisterActivityLifecycleCallbacks(this);
                        }
                    }
                });
        if (!registered) {
            logE("Failed to register life cycle callback!");
        }
    }
}