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
Related
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.
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.
I was wondering whether we can use the databases like (contacts.db, mmssms.db) directly instead of content providers ?
I have a reason to ask so. In my recent project, I was supposed develop a contact app. I used content provider for contact management. As I have learnt via content providers, I can query only table at a time via URIs, there is no way (at least, I didn't find) to join two tables and then get a query resolved.
And I had read, that databases are only visible to the applications that originally created them, so do my app would be able to access these databases ?
I am just a hobby developer for my own phone. I have no intention to make an app that directly uses the databases. I can pull the database from device, analyse them via sqlite. It is not that I am up against the content providers or they don't suit my need write now, it is just that it can be done or not ?
Any opinions ?
Content Providers give you a lot more than just database access. Being able to use Loaders to automatically reload your data (and update your UI) when the underlying data changes can vastly simplify your applications.
The goal of Content Providers is more to create a single, controlled layer for accessing your data. There is nothing stopping you from creating a custom URI that joins multiple tables together and returns the resulting joined result.
dear i am confused the use of content provider in android applications.i go through various sides and read it but i am still confused about content provider ..can any one explain it in some simple way ,how to use it?
Content providers store and retrieve data and make it accessible to all applications....whats that means?
can it means any package name or in same pakage name...
if i am making another app of diffrent package name then that c.p. is available also in that package?
OK, first of all, a content provider will be used if and only if you want to use the same data through several applications. You can still use it if you want to implement it in your application.
Now, by definition a ContentProvider "Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security." (According to Android Developer Guides)
So in other words, it's like a new layer of abstraction that you are adding to your data, assigning a new man-in-the-middle that will request everything you ask it to do (read, write, update your database, files, or webservices). And this man-in-the-middle is not only available for you, it's also available for other apps if you set the tag android:exported to true in the <provider> in your manifest file.
A ContentProvider needs you to implement:
A contract: Basically a class with public static variables with your tables names and the fields of info for those tables.
A data model: Normally a database in SQLite but you can implement it also with third party libraries like SugarORM, GreenDao, Retrofit, local files, etc).
Your custom ContentProvider: Extend ContentProvider and inside here set the calls to your data model.
This is a good tutorial on how to make your own ContentProvider:
http://www.grokkingandroid.com/android-tutorial-writing-your-own-content-provider/
There are also a few libraries that will help you create a ContentProvider automatically (I find it amazing because it takes you like 10 min to set up everything, instead of creating your own ContentProvider from scratch which could take you days).
My personal favorite is Schematic:
https://github.com/SimonVT/schematic
I hope it helps. Kind regards!
how to use it?
you actually don't use it directly, but via the content resolver (which will, according to the URI you give it, query the right ContentProvider) :
getContentResolver().query("content://com.myapp.myprovider/data/", ...);
will find your content provider if that one is registered to handle URIs that match "content://com.myapp.myprovider/data/"
if i am making another app of diffrent
package name then that c.p. is
available also in that package?
If you decide to publish the content provider, it is available outside of your application (this is a setting in the manifest).
what is the major benifit of using c.p.?
It is a common design pattern in Android to offer access to data. Major benefit is that you can abstract access to your data and decide whether to open it to other applications or not. For instance, without content providers, you could not access the media stored on the phone or the contacts of the phone.
Since a package is meant as a unique identifier for an application, how can you create an app with the same package name? If this was your question, I think I've answered it. If you don't understand something else - feel free to ask. Good luck!
P.S. Consider accepting some answers, your accept ratio is low.
I am building an application that needs to keep an list of contacts. That list will be built by inserting data by the user directly or by selecting from Android contacts.
But my list of contacts must not be accessible from outside my application (and will be a password protected application).
I guess I can use a SQLite database and encrypt the data. But is it somehow possible to do it on top of the Android contacts provider?
I am targeting 2.2.
Quoting the first sentence of the Content Providers page of the dev guide:
Content providers store and retrieve data and make it accessible to all applications.
The providers are actually built with accessibility in mind, which is exactly the opposite of what you want. Databases, on the other hand, are accessible exclusively by the owner app. You could, in theory, create a content provider that only provides encrypted data, but I can't see the point in doing that. Your data would be less secure and you would not get any additional advantage over a database.