Pro Android with Kotlin


CHAPTER 6: Content Providers


Download 5.42 Mb.
Pdf ko'rish
bet75/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   71   72   73   74   75   76   77   78   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

80
CHAPTER 6: Content Providers
This way you can design your own content access framework. Of course, you should inform 
possible clients of how to use the interface, for example, inside the contract class.
Client Access Consistency by URI Canonicalization
Quite often query results contain IDs or list index numbers or other information that depends 
on some short-term database context. For example, a query may return item IDs like 23, 
67, or 56, and if you need to get the details for an item, you query again using another URI 
containing this ID, for example content://com.xyz/people/23. The problem with such URIs 
is that a client usually wouldn’t save them for later retrievals. The ID might have changed 
meanwhile, and the URI is thus not very reliable.
To overcome this problem, a content provider may implement a URI canonicalization. To do 
so, your content provider class has to implement these two methods:

canonicalize(url:Uri): Uri:
Let this method return a canonicalized URI, for example, by adding some 
domain-specific query parameters as follows:
content://com.xyz/people/23 ->
content://com.xyz/people?
firstName=John&
lastName=Bird&
Birthday=20010534&
SSN=123-99-1624

uncanonicalize(url:Uri): Uri:
This does the exact opposite of canonicalize(). Let it return null if the 
item gets lost and the uncanonicalization cannot be performed.
Consuming Content
To consume content, content provider clients use an android.content.ContentResolver 
object. Any Context object that includes activities, services, and more provides one 
called getContentResolver(), or with Kotlin more concisely addressed by just writing 
contentResolver.
Using the Content Resolver
To access database-like content, you use one of the following ContentProvider methods:

insert(url: Uri, values: ContentValues): Int
This inserts a record.
Caution No security checks apply to calling this method. You have to implement appropriate 
security checks yourself, for example by using checkSelfPermission() on the context.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   71   72   73   74   75   76   77   78   ...   223




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