Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet149/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   145   146   147   148   149   150   151   152   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

173
CHAPTER 8: APIs
Let’s try to build a valid example for custom suggestions. We start with a working example 
of a recents suggestion provider as described earlier. It doesn’t matter whether you use the 
dialog or the widget method. Note the differences are the content provider and the search 
configuration.
For a search configuration defined by file /res/xml/searchable.xml, enter the following:

"http://schemas.android.com/apk/res/android"
android:label=
"@string/app_label"
android:hint=
"@string/search_hint"
android:searchSuggestAuthority=
"com.example.CustomProvider"
android:searchSuggestIntentAction=
"android.intent.action.VIEW">

Next we define a new content provider.
class CustomProvider : ContentProvider() {
override fun query(uri: Uri,
projection: Array?,
selection: String?,
selectionArgs: Array?,
sortOrder: String?): Cursor? {
Log.e("LOG", "query(): " + uri +
" - projection=" +
Arrays.toString(projection) +
" - selection=" + selection +
" - selectionArgs=" +
Arrays.toString(selectionArgs) +
" - sortOrder=" + sortOrder)
return null
}
override fun delete(uri: Uri, selection: String?,
selectionArgs: Array?): Int {
throw UnsupportedOperationException(
"Not yet implemented")
}
override fun getType(uri: Uri): String? {
throw UnsupportedOperationException(
"Not yet implemented")
}
override fun insert(uri: Uri, values: ContentValues?):
Uri? {
throw UnsupportedOperationException(
"Not yet implemented")
}


174
CHAPTER 8: APIs
override fun onCreate(): Boolean {
return false
}
override fun update(uri: Uri, values: ContentValues?,
selection: String?,
selectionArgs: Array?): Int {
throw UnsupportedOperationException(
"Not yet implemented")
}
}
Register it in AndroidManifest.xml.
android:name=".CustomProvider"
android:authorities="com.example.CustomProvider"
android:enabled="true"
android:exported="true">
This is what happens so far when the user starts a search:
1. Whenever the user enters or removes a character, the system 
will go to the search configuration it sees by looking at the 
searchSuggestAuthority attribute that it needs so it can address a 
content provider with this authority assigned to it.
2. By looking in AndroidManifest.xml, it sees that this authority is 
connected to the provider class CustomProvider.
3. It invokes a query() on this provider and expects a Cursor to return 
the custom suggestions.
4. If the user taps a suggestion, by virtue of the 
searchSuggestIntentAction attribute set to android.intent.action.
VIEW, the SearchableActivity’s onCreate() will see the incoming 
intent with the VIEW action.
Up to now we let the query() method return null, which is equivalent to no suggestions, 
but we added a logging statement, so we can see what arrives at the query() method. For 
example, when the user enters sp, the arguments so far will read as follows:
query(): content://com.example.CustomProvider/
search_suggest_query/sp?limit=50
projection=null
selection=null
selectionArgs=null
sortOrder=null



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   145   146   147   148   149   150   151   152   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling