下面列出了怎么用android.media.tv.TvInputService的API类实例代码及写法,或者点击链接到github查看源代码。
private void updateServiceConnectionLocked(ComponentName component, int userId) {
UserState userState = getOrCreateUserStateLocked(userId);
ServiceState serviceState = userState.serviceStateMap.get(component);
if (serviceState == null) {
return;
}
if (serviceState.reconnecting) {
if (!serviceState.sessionTokens.isEmpty()) {
// wait until all the sessions are removed.
return;
}
serviceState.reconnecting = false;
}
boolean shouldBind;
if (userId == mCurrentUserId) {
shouldBind = !serviceState.sessionTokens.isEmpty() || serviceState.isHardware;
} else {
// For a non-current user,
// if sessionTokens is not empty, it contains recording sessions only
// because other sessions must have been removed while switching user
// and non-recording sessions are not created by createSession().
shouldBind = !serviceState.sessionTokens.isEmpty();
}
if (serviceState.service == null && shouldBind) {
// This means that the service is not yet connected but its state indicates that we
// have pending requests. Then, connect the service.
if (serviceState.bound) {
// We have already bound to the service so we don't try to bind again until after we
// unbind later on.
return;
}
if (DEBUG) {
Slog.d(TAG, "bindServiceAsUser(service=" + component + ", userId=" + userId + ")");
}
Intent i = new Intent(TvInputService.SERVICE_INTERFACE).setComponent(component);
serviceState.bound = mContext.bindServiceAsUser(
i, serviceState.connection,
Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
new UserHandle(userId));
} else if (serviceState.service != null && !shouldBind) {
// This means that the service is already connected but its state indicates that we have
// nothing to do with it. Then, disconnect the service.
if (DEBUG) {
Slog.d(TAG, "unbindService(service=" + component + ")");
}
mContext.unbindService(serviceState.connection);
userState.serviceStateMap.remove(component);
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Nullable
@Override
public TvInputService.RecordingSession onCreateRecordingSession(String inputId) {
return new TestRecordingSession(this, inputId);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Nullable
@Override
public TvInputService.RecordingSession onCreateRecordingSession(String inputId) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Nullable
@Override
public TvInputService.RecordingSession onCreateRecordingSession(String inputId) {
return new RichRecordingSession(this, inputId);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Nullable
@Override
public TvInputService.RecordingSession onCreateRecordingSession(String inputId) {
return new TestRecordingSession(this, inputId);
}