Pro Android with Kotlin


CHAPTER 6: Content Providers


Download 5.42 Mb.
Pdf ko'rish
bet60/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   56   57   58   59   60   61   62   63   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

62
CHAPTER 6: Content Providers
If we want to create content-aware apps, both looking at providing and consuming content, 
these are the main questions:

How do apps provide content?

How do apps access content provided by other apps?

How do apps handle content provided by other apps?

How do we secure the provided data?
We will be looking at these topics in the following sections. Figure 
6-1
 shows an outline.
Figure 6-1. Content provider framework


63
CHAPTER 6: Content Providers
Providing Content
Content can be provided by your app as well as by system apps. Think of the pictures taken 
by the camera or contacts in your contacts list. The content provider framework is a little 
easier to grasp if we first look at the content-providing side. In later sections, we will also 
look at the consumers and other topics.
First we need to know where the data lives. However, the content provider framework makes 
no assumptions on where the data actually comes from. It can reside in files, databases, in-
memory storages, or any other place you might think of. This improves the maintenance of 
your app. For example, in an early stage, data may come from files, but later you may switch 
to a database or cloud storage, and the possible consumers don’t have to care about those 
changes because they don’t have to change how they access your content. The content 
provider framework thus provides an abstraction layer for your data.
The single interface you need to implement to provide content is the following abstract class:
android.content.ContentProvider
In the following sections, we will be looking at the implementation of this class from a use 
case perspective.
Initializing the Provider
You have to implement the following method:
ContentProvider.onCreate()
This method gets called by the Android OS when the content provider is being instantiated. 
Here you can initialize the content provider. You should, however, avoid putting time-
consuming initialization processes here since instantiation does not necessarily mean the 
content provider will be actually used.
If you don’t have anything interesting to do here, just implement it as an empty method.
To find out more about the environment your content provider is running in when 
instantiated, you can overwrite its attachInfo() method. There you will be told about the 
context the content provider is running in and also get a ProviderInfo object. Just don’t 
forget to also call super.attachInfo() from inside.
Querying Data
For querying database-like data sets, there is one method you must implement and two 
more you can optionally implement.
abstract fun query( // ----- Variant A -----
uri:Uri,
projection:Array,
selection:String,
selectionArgs:Array,
sortOrder:String) : Cursor



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   56   57   58   59   60   61   62   63   ...   223




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