android.content.Context.bindService()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(208)

本文整理了Java中android.content.Context.bindService()方法的一些代码示例,展示了Context.bindService()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.bindService()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:bindService

Context.bindService介绍

暂无

代码示例

代码示例来源:origin: oasisfeng/condom

@Override public boolean bindService(Intent service, ServiceConnection conn, int flags) {
  return mBase.bindService(service, conn, flags);
}

代码示例来源:origin: WVector/AppUpdate

public static void bindService(Context context, ServiceConnection connection) {
  Intent intent = new Intent(context, DownloadService.class);
  context.startService(intent);
  context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
  isRunning = true;
}

代码示例来源:origin: TeamNewPipe/NewPipe

public static void checkForRunningMission(Context context, String location, String name, DMChecker check) {
  Intent intent = new Intent();
  intent.setClass(context, DownloadManagerService.class);
  context.bindService(intent, new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName cname, IBinder service) {
      try {
        ((DMBinder) service).getDownloadManager().checkForRunningMission(location, name, check);
      } catch (Exception err) {
        Log.w(TAG, "checkForRunningMission() callback is defective", err);
      }
      // TODO: find a efficient way to unbind the service. This destroy the service due idle, but is started again when the user start a download.
      context.unbindService(this);
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
    }
  }, Context.BIND_AUTO_CREATE);
}

代码示例来源:origin: guardianproject/haven

public BumpMonitor(Context context) {
  context.bindService(new Intent(context,
      MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);
  sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
  bumpSensor = sensorMgr.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
  if (bumpSensor == null) {
    Log.i("BumpMonitor", "Warning: no significant motion sensor");
  } else {
    boolean registered = sensorMgr.requestTriggerSensor(sensorListener, bumpSensor);
    Log.i("BumpMonitor", "Significant motion sensor registered: "+registered);
  }
}

代码示例来源:origin: stackoverflow.com

final Context appContext = context.getApplicationContext();
final Intent intent = new Intent(appContext, MusicService.class);
appContext.startService(intent);
ServiceConnection connection = new ServiceConnection() {
 // ...
};
appContext.bindService(intent, connection, 0);

代码示例来源:origin: guardianproject/haven

public BarometerMonitor(Context context) {
  prefs = new PreferenceManager(context);
  context.bindService(new Intent(context,
      MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);
  sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
  sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_PRESSURE);
  if (sensor == null) {
    Log.i("Pressure", "Warning: no barometer sensor");
  } else {
    sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
  }
}

代码示例来源:origin: guardianproject/haven

public AmbientLightMonitor(Context context) {
  prefs = new PreferenceManager(context);
  context.bindService(new Intent(context,
      MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);
  sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
  //noinspection RedundantCast
  sensor = (Sensor) sensorMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
  if (sensor == null) {
    Log.i("AccelerometerFrament", "Warning: no accelerometer");
  } else {
    sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

void bind() {
  Log.v(TAG, "initiating bind to authenticator type " + mAuthenticatorInfo.desc.type);
  Intent intent = new Intent();
  intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT);
  intent.setClassName(mAuthenticatorInfo.serviceInfo.packageName, mAuthenticatorInfo.serviceInfo.name);
  intent.putExtra("_VA_|_user_id_", mUserId);
  if (!mContext.bindService(intent, this, Context.BIND_AUTO_CREATE)) {
    Log.d(TAG, "bind attempt failed for " + toDebugString());
    onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure");
  }
}

代码示例来源:origin: oasisfeng/condom

}}, new Consumer<Intent>() { @Override public void accept(final Intent intent) {
      condom.bindService(intent, SERVICE_CONNECTION, 0);
    }}
};

代码示例来源:origin: stackoverflow.com

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;
  }
}

代码示例来源:origin: qiujuer/Genius-Android

/**
 * Start bind Service
 */
private static void bindService() {
  synchronized (Command.class) {
    if (!IS_BIND) {
      Context context = Cmd.getContext();
      if (context == null) {
        throw new NullPointerException("Context not should null. Please call Cmd.init(Context)");
      } else {
        // Cmd service
        context.bindService(new Intent(context, CommandService.class), I_CONN, Context.BIND_AUTO_CREATE);
        IS_BIND = true;
      }
    }
  }
}

代码示例来源:origin: facebook/facebook-android-sdk

private static void startTracking() {
  if (!isTracking.compareAndSet(false, true)) {
    return;
  }
  Context context = FacebookSdk.getApplicationContext();
  if (context instanceof Application) {
    Application application = (Application) context;
    application.registerActivityLifecycleCallbacks(callbacks);
    context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

public boolean bindServiceAsUser(Intent service, ServiceConnection connection, int flags, VUserHandle user) {
  service = new Intent(service);
  if (user != null) {
    service.putExtra("_VA_|_user_id_", user.getIdentifier());
  }
  return VirtualCore.get().getContext().bindService(service, connection, flags);
}

代码示例来源:origin: guardianproject/haven

public AccelerometerMonitor(Context context) {
  prefs = new PreferenceManager(context);
  /*
   * Set sensitivity value
   */
try {
    shakeThreshold = Integer.parseInt(prefs.getAccelerometerSensitivity());
  }
  catch (Exception e)
  {
    shakeThreshold = 50;
  }
  context.bindService(new Intent(context,
      MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);
  sensorMgr = (SensorManager) context.getSystemService(AppCompatActivity.SENSOR_SERVICE);
  accelerometer = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  if (accelerometer == null) {
    Log.i("AccelerometerFrament", "Warning: no accelerometer");
  } else {
    sensorMgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  }
}

代码示例来源:origin: AltBeacon/android-beacon-library

/**
 * Method reserved for system use
 */
@Override
public boolean bindService(Intent intent, ServiceConnection conn, int arg2) {
  this.serviceIntent = intent;
  application.getApplicationContext().startService(intent);
  return application.getApplicationContext().bindService(intent, conn, arg2);
}

代码示例来源:origin: facebook/facebook-android-sdk

public boolean start() {
  if (running) {
    return false;
  }
  // Make sure that the service can handle the requested protocol version
  int availableVersion = NativeProtocol.getLatestAvailableProtocolVersionForService(
      protocolVersion);
  if (availableVersion == NativeProtocol.NO_PROTOCOL_AVAILABLE) {
    return false;
  }
  Intent intent = NativeProtocol.createPlatformServiceIntent(context);
  if (intent == null) {
    return false;
  } else {
    running = true;
    context.bindService(intent, this, Context.BIND_AUTO_CREATE);
    return true;
  }
}

代码示例来源:origin: anjlab/android-inapp-billing-v3

private void bindPlayServices()
{
  try
  {
    getContext().bindService(getBindServiceIntent(), serviceConnection, Context.BIND_AUTO_CREATE);
  }
  catch (Exception e)
  {
    Log.e(LOG_TAG, "error in bindPlayServices", e);
    reportBillingError(Constants.BILLING_ERROR_BIND_PLAY_STORE_FAILED, e);
  }
}

代码示例来源:origin: facebook/facebook-android-sdk

private static AttributionIdentifiers getAndroidIdViaService(Context context) {
  GoogleAdServiceConnection connection = new GoogleAdServiceConnection();
  Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
  intent.setPackage("com.google.android.gms");
  if(context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
    try {
      GoogleAdInfo adInfo = new GoogleAdInfo(connection.getBinder());
      AttributionIdentifiers identifiers = new AttributionIdentifiers();
      identifiers.androidAdvertiserId = adInfo.getAdvertiserId();
      identifiers.limitTracking = adInfo.isTrackingLimited();
      return identifiers;
    } catch (Exception exception) {
      Utility.logd("android_id", exception);
    } finally {
      context.unbindService(connection);
    }
  }
  return null;
}

代码示例来源:origin: firebase/firebase-jobdispatcher-android

@Test
public void testHandleMessage_doesntCrashOnBadJobData() {
 JobInvocation j =
   new JobInvocation.Builder()
     .setService(TestJobService.class.getName())
     .setTag("tag")
     .setTrigger(Trigger.NOW)
     .build();
 executionDelegator.executeJob(j);
 // noinspection WrongConstant
 verify(mockContext).bindService(intentCaptor.capture(), connCaptor.capture(), anyInt());
 Intent executeReq = intentCaptor.getValue();
 assertEquals(JobService.ACTION_EXECUTE, executeReq.getAction());
}

代码示例来源:origin: oasisfeng/condom

runInSeparateProcess(new TestService.Procedure() { @Override public void run(final Context context) {
  final Intent intent = new Intent(context, TestService.class);
  final ServiceConnection connection = new ServiceConnection() {
    @Override public void onServiceConnected(final ComponentName name, final IBinder service) {}
    @Override public void onServiceDisconnected(final ComponentName name) {}
  };
  assertTrue("Test service is not properly setup.", context.bindService(intent, connection, Context.BIND_AUTO_CREATE));
  context.unbindService(connection);
  installCondomProcess(context, new CondomOptions().setOutboundJudge(sBlockAllJudge));
  withFakeSelfPackageName(new Runnable() { @Override public void run() {
    assertFalse(context.bindService(intent, connection, Context.BIND_AUTO_CREATE));
  }});    // Block by outbound judge
  context.unbindService(connection);
}});
// TODO: More cases

相关文章

Context类方法