下面列出了android.content.Context#bindServiceAsUser ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void bindService(Context context) {
Intent intent = new Intent();
final Resources resources = context.getApplicationContext().getResources();
final ComponentName keyguardComponent = ComponentName.unflattenFromString(
resources.getString(com.android.internal.R.string.config_keyguardComponent));
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
intent.setComponent(keyguardComponent);
if (!context.bindServiceAsUser(intent, mKeyguardConnection,
Context.BIND_AUTO_CREATE, mHandler, UserHandle.SYSTEM)) {
Log.v(TAG, "*** Keyguard: can't bind to " + keyguardComponent);
mKeyguardState.showing = false;
mKeyguardState.showingAndNotOccluded = false;
mKeyguardState.secure = false;
synchronized (mKeyguardState) {
// TODO: Fix synchronisation model in this class. The other state in this class
// is at least self-healing but a race condition here can lead to the scrim being
// stuck on keyguard-less devices.
mKeyguardState.deviceHasKeyguard = false;
}
} else {
if (DEBUG) Log.v(TAG, "*** Keyguard started");
}
}
static void sendOnUnsyncableAccount(@NonNull Context context,
@NonNull RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo,
@UserIdInt int userId, @NonNull OnReadyCallback onReadyCallback) {
OnUnsyncableAccountCheck connection = new OnUnsyncableAccountCheck(syncAdapterInfo,
onReadyCallback);
boolean isBound = context.bindServiceAsUser(
getAdapterBindIntent(context, syncAdapterInfo.componentName, userId),
connection, SYNC_ADAPTER_CONNECTION_FLAGS, UserHandle.of(userId));
if (isBound) {
// Unbind after SERVICE_BOUND_TIME_MILLIS to not leak the connection.
(new Handler(Looper.getMainLooper())).postDelayed(
() -> context.unbindService(connection),
OnUnsyncableAccountCheck.SERVICE_BOUND_TIME_MILLIS);
} else {
/*
* The default implementation of adapter.onUnsyncableAccount returns true. Hence if
* there the service cannot be bound, assume the default behavior.
*/
connection.onReady();
}
}
@VisibleForTesting
public void bind(Context context) {
if (!mBound) {
Intent service = new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS);
service.setComponent(mAppData.getRecommendationServiceComponent());
mBound = context.bindServiceAsUser(service, this,
Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
UserHandle.SYSTEM);
if (!mBound) {
Log.w(TAG, "Bind call failed for " + service);
context.unbindService(this);
} else {
if (DBG) Log.d(TAG, "ScoringServiceConnection bound.");
}
}
}
public TrustAgentWrapper(Context context, TrustManagerService trustManagerService,
Intent intent, UserHandle user) {
mContext = context;
mTrustManagerService = trustManagerService;
mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
mUserId = user.getIdentifier();
mName = intent.getComponent();
mAlarmIntent = new Intent(TRUST_EXPIRED_ACTION).putExtra(EXTRA_COMPONENT_NAME, mName);
mAlarmIntent.setData(Uri.parse(mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME)));
mAlarmIntent.setPackage(context.getPackageName());
final IntentFilter alarmFilter = new IntentFilter(TRUST_EXPIRED_ACTION);
alarmFilter.addDataScheme(mAlarmIntent.getScheme());
final String pathUri = mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME);
alarmFilter.addDataPath(pathUri, PatternMatcher.PATTERN_LITERAL);
// Schedules a restart for when connecting times out. If the connection succeeds,
// the restart is canceled in mCallback's onConnected.
scheduleRestart();
mBound = context.bindServiceAsUser(intent, mConnection,
Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, user);
if (mBound) {
mContext.registerReceiver(mBroadcastReceiver, alarmFilter, PERMISSION, null);
} else {
Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString());
}
}