Android Content Provider *not* in Android? - 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.

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.

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.

Create a content provider for a different database

Is it possible to create a content provider for a different database like MySQL or SQL. How can I achieve this? Any suggestions?
ContentProvider can be applied to any kind of data even not SQL one. If you know how to get connection to MySQL (not easy task) - you can easily implement your own ContentProvider over MySQL data - just create your own class extending ContentProvider - and you're there.
This is not possible to use android content provider for different database.Please have a look at Does the content provider allow for MySql synchronization?. As Android doesn't currently include support for automated MySql server synchronization.
The Alternate Option
Content provider can be applied to any kind of data even not SQL one. If you know how to get connection to MySQL (not easy task) - you can easily implement your own content provider over MySQL data - just create your own class extending content provider - and you're there.
But As currently Android content provider or resources are not available to connect or create content provider which can be able to server synch.
If you want to expose the data to your another app you can use content provider. Otherwise it is not needed.
Unfortunately, Android doesn't have too much support for MySQL unless you have a web server. As far as SQL Server, there may be some classes that are available for you to use but many of them are targeted at ASP.NET and Microsoft-based platforms. This page practically says it all.

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.

Android Content Provider tied to external storage (text file)

I'm trying to code a system for running user studies. I have a webpage and a webservice on top of a user DB.
My Android app is a client to run the studies on. I'm use the Account Manager to store user credentials authenticated via the webservice. The point is to then implement a sync adapter that will send a text file with study data to my server, via the webservice (using the credentials).
My problem is that I'm having difficulty with the Content Provider, I've searched a lot throught the web but I can't seem to find an example of how to code a Content Provider tied to a file on external storage. All the examples I find use Content Providers tied to database tables. Can someone please point me in the right direction or shed some coding light? (BTW, I have read the Android Developer texts on Content Providers, also, I'm using Android 2.2).
Any help is appreciated.
Cheers,
André Coelho
A content provider can be used to read file data by overriding the openFile method. You could have a top level provider that provides an listing of all the files (via query) then each file could have its own unique URI that could then have openFile called on it to retrieve the file data.

Categories

Resources