Я пытаюсь реализовать простую службу, которая дает пользователю возможность заполнять данные из базы данных, созданной моим приложением. . Для начала я пытаюсь отобразить одно значение в списке автозаполнения, жестко закодированном в коде — просто чтобы узнать, прежде чем приступить к разработке реального приложения.
Давайте посмотрим на блок кода. Я использую прямо из документации:
Код: Выделить всё
override fun onFillRequest(
request: FillRequest,
cancellationSignal: CancellationSignal,
callback: FillCallback
) {
// Get the structure from the request
val context: List = request.fillContexts
val structure: AssistStructure = context[context.size - 1].structure
// Traverse the structure looking for nodes to fill out
val parsedStructure: ParsedStructure = parseStructure(structure)
// Fetch user data that matches the fields
val (username: String, password: String) = fetchUserData(parsedStructure)
// Build the presentation of the datasets
val usernamePresentation = RemoteViews(packageName, android.R.layout.simple_list_item_1)
usernamePresentation.setTextViewText(android.R.id.text1, "my_username")
val passwordPresentation = RemoteViews(packageName, android.R.layout.simple_list_item_1)
passwordPresentation.setTextViewText(android.R.id.text1, "Password for my_username")
// Add a dataset to the response
val fillResponse: FillResponse = FillResponse.Builder()
.addDataset(Dataset.Builder()
.setValue(
parsedStructure.usernameId,
AutofillValue.forText(username),
usernamePresentation
)
.setValue(
parsedStructure.passwordId,
AutofillValue.forText(password),
passwordPresentation
)
.build())
.build()
// If there are no errors, call onSuccess() and pass the response
callback.onSuccess(fillResponse)
}
data class ParsedStructure(var usernameId: AutofillId, var passwordId: AutofillId)
data class UserData(var username: String, var password: String)
Проблема 2: мне немного неясно, где находится AutofillId для метода setValue. Единственное, что я могу получить поля AutofillId и AutofillValue, — это сделать это:
Код: Выделить всё
val value = structure.getWindowNodeAt(0).rootViewNode.autofillValue
val id = structure.getWindowNodeAt(0).rootViewNode.autofillId
Код: Выделить всё
class FrequentPhrasesService : AutofillService() {
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onFillRequest(
request: FillRequest,
cancellationSignal: CancellationSignal,
callback: FillCallback
) {
val context: List = request.fillContexts
val structure: AssistStructure = context[context.size - 1].structure
val value = structure.getWindowNodeAt(0).rootViewNode.autofillValue
val id = structure.getWindowNodeAt(0).rootViewNode.autofillId
val usernamePresentation = RemoteViews(packageName, R.layout.simple_list_item_1)
usernamePresentation.setTextViewText(R.id.text1, "hello world")
val dataset = Dataset.Builder()
val fieldBuilder = Field.Builder()
if (value != null) {
fieldBuilder.setValue(value)
}
val presentationsBuilder = Presentations.Builder()
presentationsBuilder.setMenuPresentation(usernamePresentation)
fieldBuilder.setPresentations(presentationsBuilder.build())
if (id != null) {
dataset.setField(id, fieldBuilder.build())
}
callback.onSuccess(FillResponse.Builder().addDataset(dataset.build()).build())
}
override fun onSaveRequest(request: SaveRequest, callback: SaveCallback) {
TODO("Not yet implemented")
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... -by-google
Мобильная версия