下面列出了android.content.Context#bindService ( ) 实例代码,或者点击链接到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;
}
}
public static void initialize(final Context context) {
// Start the service in its own thread to avoid an ANR
if (tracingService == null) {
Thread initThread = new Thread() {
@Override
public void run() {
if (tracingService == null) {
Log.i(SharedClassesSettings.TAG, "Binding to tracing service...");
Intent serviceIntent = new Intent(context, TracingService.class);
serviceIntent.setAction(TracingService.ACTION_NULL);
context.startService(serviceIntent);
if (context.bindService(serviceIntent, tracingConnection, Context.BIND_AUTO_CREATE))
Log.i(SharedClassesSettings.TAG, "Tracing service bound.");
else
Log.i(SharedClassesSettings.TAG, "bindService() returned false.");
}
}
};
initThread.start();
}
}
/**
* 开始展示数据
*/
private void start() {
connection = new DisplayConnection(this);
Context context = LauncherApplication.getContext();
injectorService = LauncherApplication.getInstance().findServiceByName(InjectorService.class.getName());
runningFlag = true;
context.bindService(new Intent(context, FloatWinService.class), connection, Context.BIND_AUTO_CREATE);
// 定时更新
executorService.schedule(new Runnable() {
@Override
public void run() {
// 如果还在运行
if (runningFlag) {
executorService.schedule(this, 500, TimeUnit.MILLISECONDS);
}
updateDisplayInfo();
}
}, 500, TimeUnit.MILLISECONDS);
}
private boolean connectAndWait() {
Intent intent = new Intent(getContext(), XmppConnectionService.class);
intent.setAction(this.getClass().getSimpleName());
Context context = getContext();
if (context != null) {
synchronized (this) {
if (mXmppConnectionService == null && !mBindingInProcess) {
Log.d(Config.LOGTAG, "calling to bind service");
context.bindService(intent, this, Context.BIND_AUTO_CREATE);
this.mBindingInProcess = true;
}
}
try {
waitForService();
return true;
} catch (InterruptedException e) {
return false;
}
} else {
Log.d(Config.LOGTAG, "context was null");
return false;
}
}
/**
* Bind client to camera connection service
*/
private void doBindService() {
if (DEBUG) Log.v(TAG, "doBindService:");
final Context context = mWeakContext.get();
if (context != null) {
synchronized (mServiceSync) {
if ((mState == STATE_UNINITIALIZED) && (mService == null)) {
mState = STATE_BINDING;
final Intent intent = createServiceIntent(context);
if (DEBUG) Log.v(TAG, "call Context#bindService");
final boolean result = context.bindService(intent,
mServiceConnection, Context.BIND_AUTO_CREATE);
if (!result) {
mState = STATE_UNINITIALIZED;
Log.w(TAG, "failed to bindService");
}
}
}
}
}
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;
}
}
@Override
protected void onStart() {
super.onStart();
Context context = MainApp.instance().getApplicationContext();
Intent intent = new Intent(context, getServiceClass());
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
serviceRunning = true;
disposable.add(RxBus.INSTANCE
.toObservable(EventAppExit.class)
.observeOn(Schedulers.io())
.subscribe(event -> {
MainApp.instance().getApplicationContext().unbindService(serviceConnection);
}, FabricPrivacy::logException)
);
onStartCustomActions();
}
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;
}
}
public static boolean initOpenCV(String Version, final Context AppContext,
final LoaderCallbackInterface Callback)
{
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
if (AppContext.bindService(new Intent("org.opencv.engine.BIND"),
helper.mServiceConnection, Context.BIND_AUTO_CREATE))
{
return true;
}
else
{
AppContext.unbindService(helper.mServiceConnection);
InstallService(AppContext, Callback);
return false;
}
}
DownloadAdapter(Context context) {
mContext = context;
mSelectedItems = new SparseArray<>();
context.startService(DownloadManager.getActionStartService());
Intent mIntent = new Intent(context, DownloadManager.class);
context.bindService(mIntent, mConnection, Context.BIND_ABOVE_CLIENT);
}
public void onCreate(Context context) {
LogUtil.i(TAG, "onCreate");
LauncherApplication application = LauncherApplication.getInstance();
eventService = application.findServiceByName(EventService.class.getName());
operationService = application.findServiceByName(OperationService.class.getName());
injectorService = application.findServiceByName(InjectorService.class.getName());
highLightService = application.findServiceByName(HighLightService.class.getName());
operationStepService = application.findServiceByName(OperationStepService.class.getName());
injectorService.register(this);
// 启动CmdHandler
cmdExecutor = Executors.newSingleThreadScheduledExecutor();
windowManager = (WindowManager) LauncherApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
// 获取截图服务
captureService = application.findServiceByName(ScreenCaptureService.class.getName());
PermissionUtil.grantHighPrivilegePermission(LauncherApplication.getContext());
currentRecordId = StringUtil.generateRandomString(10);
setServiceToNormalMode();
// 启动悬浮窗
connection = new RecordFloatConnection(this);
listener = new FloatClickListener(this);
stopListener = new FloatStopListener();
context.bindService(new Intent(context, FloatWinService.class), connection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStart() {
Context context = MainApp.instance().getApplicationContext();
Intent intent = new Intent(context, DanaRv2ExecutionService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
disposable.add(RxBus.INSTANCE
.toObservable(EventAppExit.class)
.observeOn(Schedulers.io())
.subscribe(event -> {
MainApp.instance().getApplicationContext().unbindService(mConnection);
}, FabricPrivacy::logException)
);
super.onStart();
}
@Override
public void connect(Context c) {
mContext = c;
Intent bindIntent = new Intent(c, mDownloaderServiceClass);
bindIntent.putExtra(PARAM_MESSENGER, mMessenger);
if ( !c.bindService(bindIntent, mConnection, Context.BIND_DEBUG_UNBIND) ) {
if ( Constants.LOGVV ) {
Log.d(Constants.TAG, "Service Unbound");
}
} else {
mBound = true;
}
}
private boolean bindRemoteSSOService(Context context) {
if (!isWeiboAppInstalled()) {
return false;
}
String pkgName = this.mWeiboInfo.getPackageName();
Intent intent = new Intent(DEFAULT_WEIBO_REMOTE_SSO_SERVICE_NAME);
intent.setPackage(pkgName);
return context.bindService(intent, this.mConnection, 1);
}
protected void startService(final Context context,final Intent intent){
final ServiceConnection connection = mServiceConnectionBox.get();
if(connection != null){
context.bindService(intent,connection, mFlag);
}else {
context.startService(intent);
}
}
/** {@inheritDoc} */
@Override
public void connect(Context context) {
Intent intent = new Intent(serviceName);
intent.setPackage(servicePackage);
if (!context.bindService(intent, connection, Service.BIND_AUTO_CREATE)) {
throw new RuntimeException("Cannot connect to " + serviceName);
}
}
/**
* Bind to a {@link CustomTabsService} using the given package name and
* {@link ServiceConnection}.
* @param context {@link Context} to use while calling
* {@link Context#bindService(Intent, ServiceConnection, int)}
* @param packageName Package name to set on the {@link Intent} for binding.
* @param connection {@link CustomTabsServiceConnection} to use when binding. This will
* return a {@link CustomTabsClient} on
* {@link CustomTabsServiceConnection
* #onCustomTabsServiceConnected(ComponentName, CustomTabsClient)}
* @return Whether the binding was successful.
*/
public static boolean bindCustomTabsService(Context context,
String packageName, CustomTabsServiceConnection connection) {
Intent intent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
if (!TextUtils.isEmpty(packageName)) intent.setPackage(packageName);
return context.bindService(intent, connection,
Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);
}
/**
* Bind to a {@link CustomTabsService} using the given package name and
* {@link ServiceConnection}.
* @param context {@link Context} to use while calling
* {@link Context#bindService(Intent, ServiceConnection, int)}
* @param packageName Package name to set on the {@link Intent} for binding.
* @param connection {@link CustomTabsServiceConnection} to use when binding. This will
* return a {@link CustomTabsClient} on
* {@link CustomTabsServiceConnection
* #onCustomTabsServiceConnected(ComponentName, CustomTabsClient)}
* @return Whether the binding was successful.
*/
public static boolean bindCustomTabsService(Context context,
String packageName, CustomTabsServiceConnection connection) {
Intent intent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
if (!TextUtils.isEmpty(packageName)) intent.setPackage(packageName);
return context.bindService(intent, connection,
Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY);
}