下面列出了android.content.Context#unbindService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean initOpenCV(String Version, final Context AppContext,
final LoaderCallbackInterface Callback)
{
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
Intent intent = new Intent("org.opencv.engine.BIND");
intent.setPackage("org.opencv.engine");
if (AppContext.bindService(intent, helper.mServiceConnection, Context.BIND_AUTO_CREATE))
{
return true;
}
else
{
AppContext.unbindService(helper.mServiceConnection);
InstallService(AppContext, Callback);
return false;
}
}
/**
* Connects to the Custom Tabs warmup service, and initializes the browser.
*
* This convenience method connects to the service, and immediately warms up the Custom Tabs
* implementation. Since service connection is asynchronous, the return code is not the return
* code of warmup.
* This call is optional, and clients are encouraged to connect to the service, call
* <code>warmup()</code> and create a session. In this case, calling this method is not
* necessary.
*
* @param context {@link Context} to use to connect to the remote service.
* @param packageName Package name of the target implementation.
* @return Whether the binding was successful.
*/
public static boolean connectAndInitialize(Context context, String packageName) {
if (packageName == null) return false;
final Context applicationContext = context.getApplicationContext();
CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
@Override
public final void onCustomTabsServiceConnected(
ComponentName name, CustomTabsClient client) {
client.warmup(0);
// Unbinding immediately makes the target process "Empty", provided that it is
// not used by anyone else, and doesn't contain any Activity. This makes it
// likely to get killed, but is preferable to keeping the connection around.
applicationContext.unbindService(this);
}
@Override
public final void onServiceDisconnected(ComponentName componentName) { }
};
try {
return bindCustomTabsService(applicationContext, packageName, connection);
} catch (SecurityException e) {
return false;
}
}
/**
* Connects to the Custom Tabs warmup service, and initializes the browser.
*
* This convenience method connects to the service, and immediately warms up the Custom Tabs
* implementation. Since service connection is asynchronous, the return code is not the return
* code of warmup.
* This call is optional, and clients are encouraged to connect to the service, call
* <code>warmup()</code> and create a session. In this case, calling this method is not
* necessary.
*
* @param context {@link Context} to use to connect to the remote service.
* @param packageName Package name of the target implementation.
* @return Whether the binding was successful.
*/
public static boolean connectAndInitialize(Context context, String packageName) {
if (packageName == null) return false;
final Context applicationContext = context.getApplicationContext();
CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
@Override
public final void onCustomTabsServiceConnected(
ComponentName name, CustomTabsClient client) {
client.warmup(0);
// Unbinding immediately makes the target process "Empty", provided that it is
// not used by anyone else, and doesn't contain any Activity. This makes it
// likely to get killed, but is preferable to keeping the connection around.
applicationContext.unbindService(this);
}
@Override
public final void onServiceDisconnected(ComponentName componentName) { }
};
try {
return bindCustomTabsService(applicationContext, packageName, connection);
} catch (SecurityException e) {
return false;
}
}
/**
* 停止replay
*/
public void onDestroy(Context context) {
this.provider = null;
eventService.stopTrackAccessibilityEvent();
if (watcher != null) {
watcher.stop();
watcher = null;
}
if (connection != null) {
context.unbindService(connection);
connection = null;
binder = null;
}
runningExecutor.shutdownNow();
LauncherApplication.getInstance().stopServiceByName(HighLightService.class.getName());
}
public static boolean initOpenCV(String Version, final Context AppContext,
final LoaderCallbackInterface Callback)
{
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
Intent intent = new Intent("org.opencv.engine.BIND");
intent.setPackage("org.opencv.engine");
if (AppContext.bindService(intent, helper.mServiceConnection, Context.BIND_AUTO_CREATE))
{
return true;
}
else
{
AppContext.unbindService(helper.mServiceConnection);
InstallService(AppContext, Callback);
return false;
}
}
@Test
public void testStartBindUnbindStop() throws InterruptedException {
Context context = InstrumentationRegistry.getTargetContext();
context.startService(mServiceIntent);
awaitAndAssertEvents(ON_CREATE, ON_START);
ServiceConnection connection = bindToService();
// Precaution: give a chance to dispatch events
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
// still the same events
awaitAndAssertEvents(ON_CREATE, ON_START);
context.unbindService(connection);
// Precaution: give a chance to dispatch events
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
// service is still started (stopServices/stopSelf weren't called)
awaitAndAssertEvents(ON_CREATE, ON_START);
context.stopService(mServiceIntent);
awaitAndAssertEvents(ON_CREATE, ON_START, ON_STOP, ON_DESTROY);
}
public void disconnect(Context context) {
unregisterServiceCallback();
if (mAlreadyConnected) {
try {
context.unbindService(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
mAlreadyConnected = false;
if (mListenToDeath && mBinder != null) {
mBinder.unlinkToDeath(this, 0);
}
mBinder = null;
mTrojanService = null;
mCallback = null;
}
}
public static boolean initOpenCV(String Version, final Context AppContext,
final LoaderCallbackInterface Callback)
{
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
Intent intent = new Intent("org.opencv.engine.BIND");
intent.setPackage("org.opencv.engine");
if (AppContext.bindService(intent, helper.mServiceConnection, Context.BIND_AUTO_CREATE))
{
return true;
}
else
{
AppContext.unbindService(helper.mServiceConnection);
InstallService(AppContext, Callback);
return false;
}
}
public static boolean initOpenCV(String Version, final Context AppContext,
final LoaderCallbackInterface Callback)
{
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
Intent intent = new Intent("org.opencv.engine.BIND");
intent.setPackage("org.opencv.engine");
if (AppContext.bindService(intent,
helper.mServiceConnection, Context.BIND_AUTO_CREATE))
{
return true;
}
else
{
AppContext.unbindService(helper.mServiceConnection);
InstallService(AppContext, Callback);
return false;
}
}
/**
* Connects to the Custom Tabs warmup service, and initializes the browser.
*
* This convenience method connects to the service, and immediately warms up the Custom Tabs
* implementation. Since service connection is asynchronous, the return code is not the return
* code of warmup.
* This call is optional, and clients are encouraged to connect to the service, call
* <code>warmup()</code> and create a session. In this case, calling this method is not
* necessary.
*
* @param context {@link Context} to use to connect to the remote service.
* @param packageName Package name of the target implementation.
* @return Whether the binding was successful.
*/
public static boolean connectAndInitialize(Context context, String packageName) {
if (packageName == null) return false;
final Context applicationContext = context.getApplicationContext();
CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
@Override
public final void onCustomTabsServiceConnected(
ComponentName name, CustomTabsClient client) {
client.warmup(0);
// Unbinding immediately makes the target process "Empty", provided that it is
// not used by anyone else, and doesn't contain any Activity. This makes it
// likely to get killed, but is preferable to keeping the connection around.
applicationContext.unbindService(this);
}
@Override
public final void onServiceDisconnected(ComponentName componentName) { }
};
try {
return bindCustomTabsService(applicationContext, packageName, connection);
} catch (SecurityException e) {
return false;
}
}
public static boolean initOpenCV(String Version, final Context AppContext,
final LoaderCallbackInterface Callback)
{
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
Intent intent = new Intent("org.opencv.engine.BIND");
intent.setPackage("org.opencv.engine");
if (AppContext.bindService(intent, helper.mServiceConnection, Context.BIND_AUTO_CREATE))
{
return true;
}
else
{
AppContext.unbindService(helper.mServiceConnection);
InstallService(AppContext, Callback);
return false;
}
}
public static synchronized void deInit(Context context) {
if (instance != null) {
Log.i(TAG, "deInit...");
context.unbindService(mRemoteConnection);
instance = null;
}
}
@Override
protected void onStop() {
Context context = MainApp.instance().getApplicationContext();
context.unbindService(serviceConnection);
serviceRunning = false;
disposable.clear();
super.onStop();
}
/**
* Unbinds this service connection from the given context.
* @param context The context to be unbound from.
*/
public void unbindFromContext(Context context) {
if (isBoundToService()) {
context.unbindService(this);
mService = null;
}
}
private void unbindVPNService(Context context) {
if (bound && serviceConnection != null && context != null) {
try {
context.unbindService(serviceConnection);
} catch (Exception e) {
Log.w(LOG_TAG, "DNSCryptFragmentPresenter unbindVPNService exception " + e.getMessage() + " " + e.getCause());
}
bound = false;
}
}
public static void disable(Context context) {
context.unbindService(sConnection);
}
public void stop(Context context) {
sensorMgr.cancelTriggerSensor(sensorListener, bumpSensor);
context.unbindService(mConnection);
}
public void stop(Context context) {
sensorMgr.unregisterListener(this);
context.unbindService(mConnection);
}
/**
* Stop the remote ButlerService to indicate the test run has completed.
* <p>
* This method should be called from a subclass of {@link Instrumentation#finish(int, Bundle)}, BEFORE
* calling super.finish(). Often this will be a subclass of AndroidJUnitRunner.
* <p>
* This will handle re-enabling animations on the device, as well as allow system popups to be shown when
* apps crash or ANR on the emulator.
*
* @param context the "target context"; i.e. Context of the app under test (not the test apk context!)
*/
public static void teardown(@NonNull Context context) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.linkedin.android.testbutler",
"com.linkedin.android.testbutler.ButlerService"));
context.unbindService(serviceConnection);
serviceStarted = new CountDownLatch(1);
}
/**
* Unbinds this service connection from the given context.
* @param context The context to be unbound from.
*/
public void unbindFromContext(Context context) {
context.unbindService(this);
}