How do content providers works? - android

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.

Related

Android SQLite data displaying

I'm kinda new in android development and I am little confused about content providers. Content providers are used to share your data with other application legally, but is it correct to use it for retrieving your data from SQLite databaseand display it in, for instance listView? What is better solution for displaying data from db?
Thanks
Yes it is legit using the ContentProvider to retrieve data from a SQLite database while its perfectly fine creating an Handler to handle the SQLiteOpenHelper and just using it.
Its all depends on your app's needs,
Me individually for my own app if no need for sharing data wouldn't use a content provider.
if you don't need to share the data base from your app it's not necessary to use a Content Provider

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.

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

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.

Do I really need Content Provider in my application

I am creating app that should have offline mode, so previously downloaded data should stored somewhere, the most common way is to store data in SQLite database.
Mostly SQLite database is used with Content Provider in android. I have clear understanding what is the purpose of content provider (to share data between different apps), but in my case application will never need to share the data with other apps in the system.
Content provider has the similar interface as HTTP request (GET,POST,PUT,DELETE).
My idea is to create facade class which can be used like this getAllLatestNews(); firstly it will try to get latest data from the internet, if it fails - data from database will be used and if request is successful it also will save retrieved data to the database. This class will be facade for separating different layers of application (not to make requests from activities directly).
But now I am a little bit puzzled deciding whenever I need Content Provider or not. I can use SQLiteOpenHelper classes to retrieve and save data to the database or even use ORM library to do this.
At first I wanted to implement REST API Pattern B by Virgil Dobjanschi. But now I am not sure about this, maybe it would be better to create facade for Robospice(in my case, network request in the service) requests and do persistence there ?
Please share you thoughts about this topic, I would be grateful for any help.
EDIT
I asked this question because I feel that it is not good practice to make requests directly from activities even if they are made in service under the hood, I want to separate different layers of my application in order to make it more flexible and maintainable.
As you don't intend to share your data, i would say that implementing a ContentProvider is overkill.
Personally im a huge fan of ORM libraries (Currently i use SugarOrm in several projects), so i would go down that road.
Then at app startup, you check whether or not you have an active internet connection, and based on that you either get the latest information online, or retrieve older information from the database.
To seperate the logic a bit, i would most likely implement the getting of online information in a service, which would then store it in the database and broadcast to the activity that the information is now available, and then the activity could retrieve the information from the newly updated database.
Content Providers are absolutely for sharing data between applications and are of no use without this purpose.
If you want to use those data only in your app privately, you could use SQLite databases. Also there other objects available:
Shared Preferences
Files
Content provider has the similar interface as HTTP request (GET,POST,PUT,DELETE)
I don't think so. It's more like to SQL language.

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

Categories

Resources