Pro Android with Kotlin


CHAPTER 6: Content Providers


Download 5.42 Mb.
Pdf ko'rish
bet68/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   64   65   66   67   68   69   70   71   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

71
CHAPTER 6: Content Providers
The path part of the URI specifies the data domain, or table in SQL. The user profile data 
inside the contacts, for example, gets addressed by the following:
content://com.android.contacts/profile
In this example, the path has just one element, but in can be more complex like pathpart1/
pathpart2/pathpart3.
A URI may also have a query part specifying a selection. Looking at the query methods 
from the class android.content.ContentProvider, we already have the ability to specify 
a selection on an API basis, but it is totally acceptable, although not mandatory, to 
also allow query parameters inside the URI. If you need to put several elements into 
the query parameter, you can follow the usual convention to use & as a separator, as in 
name=John&age=37.
The fragment specifies a secondary resource and is not used often for content providers. 
But you can use it, if it helps you.
Since a URI is such a generic construct and guessing correct URIs for accessing content 
provided by some apps is almost impossible, a content provider app usually provides a 
contract class that helps in building correct URIs for the task at hand.
Building a Content Interface Contract
The URIs a client has to use to access data represent the interface to the contents. It is 
therefore a good idea to have a central place where a client can look to find out which 
URIs to use. The Android documentation suggests using a content contract class for that 
purpose. The outline of such a class will look like this:
class MyContentContract {
companion object {
// The authority and the base URI
@JvmField
val AUTHORITY = "com.xyz.whatitis"
@JvmField
val CONTENT_URI = Uri.parse("content://" +
AUTHORITY)
// Selection for ID bases query
@JvmField
val SELECTION_ID_BASED = BaseColumns._ID +
" = ? "
}
// For various tables (or item types) it is
// recommended to use inner classes to specify
// them. This is just an example
class Items {
companion object {
// The name of the item.
@JvmField
val NAME = "item_name"



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   64   65   66   67   68   69   70   71   ...   223




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