why don't we directly call methods on a Content Provider object? - android

Android development:
I don't understand why we use Content resolver to access Content provider . Instead why don't create an instance of Content Provider and directly call methods on it ?

Technically you could do this if ContentResolver would be built differently by android.
But the reason to this it's not technical, it's arcitcherual.
content providers are primarily intended to be used by other
applications, which access the provider using a provider client objects
When client application reaches other application data - for example Whatsapp automatically add your contacts via the contact application ContentProvider they don't, and don't need to, care where the ContentResolver gathered the data from (e.g local SQlite or maybe a web server).
If one app would directly reaches other app's data many API's would be "broken" over version updates.
Great guide on how the implementation actually happens in the code here.

Related

What is a ContentProvider and what is it typically used for?

i'm beginner in android development, need help regarding ContentProvider.
public class My Application extends ContentProvider {}
A ContentProvider manages access to a structured set of data. It encapsulates the data and provide mechanisms for defining data security. ContentProvider is the standard interface that connects data in one process with code running in another process.
Kindly refer following links,
https://developer.android.com/guide/topics/providers/content-provider-creating.html
and
https://www.tutorialspoint.com/android/android_content_providers.htm
A content provider component supplies data from one application to others on request. one application cannot directly access (read/write) other application's data. Every application has its own id data directory and own protected memory area.
Content provider is the best way to share data across applications. Content provider is a set of data wrapped up in a custom API to read and write. Applications/Processes have to register themselves as a provider of data.
In simple language you can say content provider is a shared database which expose his properties and on there behalf of them other application can access and store the data as per the implementation privilege
Content providers can help an application manage access to data stored by itself, stored by other apps, and provide a way to share data with other apps. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. Implementing a content provider has many advantages. Most importantly you can configure a content provider to allow other applications to securely access and modify your app data.
It is not that they are used only to share data with other applications. You may still use them because they provide a nice abstraction, but you don’t have to necessarily share data with other apps. This abstraction allows you to make modifications to your application data storage implementation without affecting other existing applications that rely on access to your data
You can get more info from the documentation.
ContentProvider is mainly used for access data from one application to another application.
For example by using ContentProvider we can get phone contacts,call log from phone to our own application in android.we can also access data which are stored in (sqlite)databases.

How do content providers works?

Well, as far as I kwnow a content provider is a database, and is used to pass data through apps.
But this data can be accessed only in the phone that it was saved? Or if I saved some data using the app with phone1, can I retrieve it using the app with phone 2?
If it can't do the second option, what could I use to do it?
Thanks.
Content provider is not a database. You can think this as a layer between your app and underlying data. The data can be in sqlite database, file or something else. Content provider is very useful to access and store your data to database or file. You need this when you use syncadapter or widgets in your app. Now to answer your question, content provider of your app can be accessed by a different app in your same phone provided you use correct permission. As I mentioned it's not a database, so answer of second question is not. I would recommend you to read about content provider on android developer site to get the basic concepts.
A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.

What is the main use of Content Providers in multiple apps

I have been reading content providers for a while and i have seen that of two types, one is in built for eg Contacts(that i understood) and other is making our own content provider by content:// thing. Well most of the examples i have gone through are doing it in one app. I mean what is the point of using content providers then if i store data in one app and retrieving it in the same as the definition says it shares data between apps?
I am looking it as i made two projects and i used content provider in one and store some data in database. Then i make other project and get that stored data through the content :// uri . Is that what is main function of content provider? Is this thing possible? If so how?
I have been asking it clear my basics.
To understand content providers you need to understand the Android Architecture first. All android apps runs in its own VM (Virtual Machine), it means when you run app 'A' and store some files or create database in it, now when you run app 'B' and create database in it.
Those two apps 'A' and 'B' do not know each other or in easy words they do not share data between them. To make data accessible one app has to share its data so other can access it. Thats where ContentProvider comes in.
Through content providers any app can expose its data to other apps which are interested in taking it.
One example is your Contact list. You can access Contact List through content provider although it is not created by you and own by Android, but it intents to share data with you, and you can access it through content providers.
I believe you are looking for information on the Content Resolver. As others pointed out, the Provider is just for sharing your app's data. When you want to access it, even in another app, you use the Content Resolver to send commands to the other App's Content Provider; insert(), update(), delete(), and query().
What you spoke of about content://URI, that's the identifier for the name of the data you are looking for in a Content Provider. The link explains Providers, Resolvers, Contracts, URIs, and how to create them.
http://www.grokkingandroid.com/android-tutorial-writing-your-own-content-provider/
Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content usingg insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database.
I am looking it as i made two projects and i used content provider in
one and store some data in database. Then i make other project and get
that stored data through the content :// uri . Is that what is main
function of content provider?
I think you are looking for this:
http://www.vogella.com/tutorials/AndroidSQLite/article.html

How does ContentProvider work internally

Everyone,I was looking at android source.But I could not find something valuable for me.Actually,I want to know whether ContentProvider is worked like AIDL.You know ,AIDL can achieve the communication between two independence application.
So, I just want to see how does the ContentProvider work internally.
From ContentProvider Source code
Content providers are one of the primary building blocks of Android
applications, providing content to applications. They encapsulate data
and provide it to applications through the single ContentResolver
interface. A content provider is only required if you need to share
data between multiple applications. For example, the contacts data is
used by multiple applications and must be stored in a content
provider. If you don't need to share data amongst multiple
applications you can use a database directly via
android.database.sqlite.SQLiteDatabase.
According to this and to the information in the link I sent you, ContentProvider isn't using AIDL... It is using the ContentResolver interface.
Content Providers use IPC Binders internally. "In fact, Intents and ContentProvider are just a higher-level abstraction of Binder"

Android Content Provider *not* in Android?

So far, all content provider tutorials I found, teach how to implement a content provider locally, on the Android device. The URI for such content providers always starts with content:// as in:
content://com.google.provider.NotePad/notes/23
My question: is it possible to implement a content provider that is not on the Android device?
That is, a content provider that follows all the conventions and convenience of Android content providers but is rather located on a remote website, using MySQL for example, instead of SQLite3?
If so, how do I go about implementing such content provider? Where do I find information that teaches how to do that? Does such content provider's URI start with 'content://'?
I don't think it is possible to create a content provider that is not on the Android device. A Content Provider is part of the Android framework and its resolution (i.e. what does the name maps to) is part of the framework as well. Realize that a URI that Android uses for the name of a service is not the same as a URL over the public internet.
If you want to connect to some server side data over the public internet, it is best to wrap it in some web service that will return JSON or XML to avoid database connection issues.
This is indeed possible and I don't think you'll find a better example than RESTProvider .
This exposes a contentprovider interface to REST API services and can be used with any remote service that returns XML or JSON.

Categories

Resources