I have seen custom content providers for sqLite in apps, but thats about it. When should a Custom Content provider be built?
EboMike in this question says:
Other apps will be able to access your data.
You can wrap and abstract a lot of the query logic in your content provider, and limit access.
You will be able to lean on the system to allow for things like managed queries.
Remember that you can control user interacts with your data,for example you can prevent user from modifying data or you can force system to open data with explicit App and so on.
Related
I have two apps and I want one app to change preferences in another app.
Is it possible? If so, then how?
It means that you need some passage between two applications.
If A application provides content provider, B application can access that content provider and change values in that provider.
Have a look this article http://developer.android.com/guide/topics/providers/content-providers.html
You could use Content Providers. Put your settings in a database so that the other application can read.
I've read Android dev guide and notice that we can implement different classes for the content provider. So,
There are many content providers or just one content provider in one Android app?
How to properly implement different content provider classes like that?
Here is what I read from the dev guide:
You implement a provider as one or more classes in an Android
application
http://developer.android.com/guide/topics/providers/content-provider-creating.html
You can implement as many as you want, as you can see from the documentation here. To register a content provider, you need to add its corresponding <provider> tag in the Android Manifest.
In most cases, however, you won't need multiple content providers. One is usually enough, as it can handle multiple tables. You should only really need more than one if you want your app to provide public access to 2+ separate data entities.
You can use (provide as well as use) as many content providers per app as you need. They need different content URIs, of course.
In addition to the uses outlined in the document (your link) you can use content providers for other purposes as accessing data storage. The content URI can have parameters, so you can use a content provider similarly to a web service.
You can create as many content providers as you want. But do you need them al?
What content provider classes do you want to implement? If you read the page very good you should have seen that it contains links to two pages:
http://developer.android.com/guide/topics/providers/content-provider-basics.html - Content Provider Basics
http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentProvider - Implementing the ContentProvider Class
I suggest you first read those pages. Google is giving some more information about Content Providers, tutorials and examples:
http://android10.org/index.php/articlesdatastorage/252-content-providers-using-and-creating-them
http://thinkandroid.wordpress.com/2010/01/13/writing-your-own-contentprovider/
http://www.vogella.com/articles/AndroidSQLite/article.html
http://about-android.blogspot.com/2010/04/content-provider-example-1.html
There is no rule as such that you have to implement only one content provider per application. If your project demands, then you can do so.
If you want to implement multiple content providers in your application package, then make sure that authorities part of each content provider is unique, to route the incoming data requests to each content providers properly.
But having too many content providers can really confuse you and not required.
The only scenario that I see to have multiple content providers is, if you are having multiple databases in your application and you want to share all those databases with outside applications. Where you can use separate content provider for each database to share it with outside world.
Hope it helps.
I'm an Android newbie, learning the SDK by creating some basic apps. I'm currently working on an app that will display content from a news aggregator with news items and comments on each item. Each news item has an 'id', and many comments associated with it.
Each results page will have a 'before' and 'after' reference id to the news item at the beginning of the page and the end, respectively. So each 'query' will be something like '/?before=$ID' or '/?after=$ID'.
It seems like ContentProviders are the preferred way to store data on android. However, the content of the site changes so much that I question whether or not using a ContentProvider would be wise.
I appreciate any insight.
Well content providers are used by the e-mail app to hold e-mail, gmail to store and sync its mail messages, gtalk and contacts to keep track of the people you know and their chat status, etc.
I am not sure just how dynamic you are thinking, but those are all pretty dynamic.
One way to look at it -- a content provider is just an API to access structured data across processes. If that API serves your purpose, well that's a good sign. The other aspect to them is that usually they are implemented on top of a SQLite database, and that is the easiest way to implement them because there is a lot of framework support for that. So you will most likely be using a SQLite database for your content provider impl. Is a database dynamic enough for you?
I’m a beginner to Android development. Something always confuses me. Let say if I want to create a simple app that shows all presidents of United States with some brief info, how do I store this information? Should I store these information in content provider while activity oncreate? Doesn’t it append the data everytime when user start this activity?
Should I store these information in content provider while activity oncreate?
You never store data in a content provider... a content provider is used in order to (guess what?), provide content. In most cases you won't need to create your own content provider; unless you want to share that data with other apps. If the information you are going to handle is going to be used by your app only, don't create content providers... just put your data in a database and access it from there.
When querying a ContentProvider on Android, one specifies the ContentProvider of interest by providing the "content URI" for that ContentProvider. What happens when multiple ContentProvider's serve that same URI, either intentionally? or maliciously?
When trying to open a pic on my phone, I've seen it prompt with apps that can "handle" the image. Will the same kind of thing happen here?
Multiple ContentProviders can't do this. The first application that registers a content provider using the element in its manifest has control over the URI pattern. I'm pretty sure that you'll get an installation error if you try to add another provider that uses the same URI pattern. Android keeps track of providers and URIs.
When you see a prompt with multiple apps for handling a file or situation, that's because the apps have specified an with a child that includes
android.intent.category.CATEGORY_ALTERNATIVE or android.intent-category.CATEGORY_SELECTED_ALTERNATVE. In essence, the app or apps are declaring themselves to be alternatives to the action specified in the child. This allows the user to have multiple choices for handling a type of data.
It makes sense to provide alternatives: a user might want to edit a picture, share it via Twitter, or e-mail it.
Note that two content providers can do the same thing, but they can't use the same URI. An app has to make a conscious choice of which one to use, or provide some mechanism of choosing between the two.