I want to use a Contentprovider within a Service. I've written a handler-class to parse JSON and want to insert data in my contentprovider, how is it possible to give a Service a reference for the contentprovider?
The comments are on the right track, but I feel that they need elaboration:
Content providers, even ones that you only use within your own app, are not designed to be referenced directly. Instead, an app should use methods in ContentResolver to access a content provider. ContentResolver features the same set of methods as those that you must implement for ContentProvider (its abstract methods). For example, if you want to insert data into a ContentProvider, you call getContentResolver().insert(). A ContentResolver is a general-purpose object for communicating with content providers, file providers, and the sync adapter framework.
When you define a content provider in your app, you have to define its authority value in the manifest, and you also have to write code that interprets incoming content URIs. The best way to work with content URIs is to start by assigning a separate content URI to each table in your provider, using the form
content://*authority*/*path*
Authority is the authority string you specified in the manifest; the de facto standard is to use your app's package name with ".provider" appended to it. Path is a descriptive term for the table it represents. For example:
content://com.example.android.myapp.provider/sales
to point to the Sales table in your provider. You can get fancier if you want, but most developers will satisfy their needs with this proposal.
To work with content URIs in your content provider, use the android.content.UriMatcher class.
Just call getContentResolver() from your service;
Related
I am about to create two applications which have the same SQLite database. I went through this link. But it does not make sense. I have to update the database from both applications. Please suggest one or more better ways.
You should read about Content Provider
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 SQLiteDatabase.
https://developer.android.com/guide/topics/providers/content-providers
https://developer.android.com/reference/android/content/ContentProvider
Content Provider is a great way to share database with other applications.
When you create a Content Provider, you can define which part of your database can be shared as well.
A Content Provider has following parts-
1. A class that extends ContentProvider class
2. An Authority that distinguishes this ContentProvider from others on the Android O.S.
3. A Contract class that holds Uri to tables and columns that you want to share with other applications
4. A ContentResolver to access data from a ContentProvider
5. Set of user defined permissions to write to and read from a shared ContentProvider. Permissions allow only those apps that you authorize to access your ContentProvider
Here is a simple tutorial about the use of ContentProvider and how to share your database with other applications- https://www.tutorialspoint.com/android/android_content_providers.htm
What is the difference between ContentProviders and ContentResolver? I do not want for the SQLite database. I am developing an application for media.
I found some explanation here. In summary
Content Resolver resolves a URI to a specific Content provider.
Content Provider provides an interface to query content.
The way to query a content provider is contentResolverInstance.query(URI,.....)
ContentProviders are used to abstract the database from other parts and acts as an interface between your database and UI/other classes. You must create your own ContentProvider to share your app data with other apps.
ContentResolver is used to select the right ContentProvider based on the ContentUris. A ContentUri may look like
content://com.android.contacts/contacts/3
content:// is called scheme and indicates that it is a ContentUri.
com.android.contacts is called Content authority and ContentResolver uses it to resolve to a unique provider (in this case, ContactProvider).
contacts is the path that identify some subset of the provider's data (for example, Table name).
3 is the id used to uniquely identify a row within the subset of data.
NOTE : Your own app can also use this route to handle its data.
See Content Providers in Android for more detail
Two layered Abstraction :
ContentResolver --> ContentProvider -->SQLiteDatabase
The main difference is this as mentioned in other answers.
ContentProvider exposes private data of your application to external application
while
ContentResolver provides the right ContentProvider among all the ContentProviders using a URI.
Deeper Understanding (of two-layered abstraction)
Let's take a detour.
We all know that when we create an SQLite database then the database remains private to your application which means, you just can not share your app data with any other external application.
How data is shared then?
ContentProvider and ContentResolver are part of android.content package. These two classes work together to provide robust, secure data sharing model among applications.
ContentProvider exposes data stored in the SQLite database to other application without telling them the underlying implementation of your database.
So it abstracts the SQliteDatabase. But wait there is a catch !!!
The external application can not directly access ContentProvider. For that, you need to first interact with another class called ContentResolver
Think ContentResolver as a ContentProvider finder. There is only one instance of it and all the ContentProviders of your Device are registered with a simple Namespace URI. If you want to reach a particular ContentProvider you just need to know its URI. Pass it to ContentResolver and it will find the Provider using the URI.
Now lets have a look at the most important method getContentResolver().query(URI,String[] proj.....)
What happens when getContentResolver().query(URI,String[] proj.....) gets called
query() method belongs to ContentResolver class however it invokes the abstract query() method of resolved ContentProvider and returns Cursor object.
In this way, the External application gets exposed to the private database via two abstraction layers.
Just to add more points
You cannot create your own ContentResolver class but you can always create your own
ContentProvider class
Hope you have a better understanding
You can also see some sample code here for creating SQLitedatabase, ContentProvider etc, But it's not well documented.
In 2021 :D
Content Resolver : For Data Request
Content Provider : For Data Response
I watched a course about ContentProvider on the Internet demonstrating how to define and use a ContentProvider.
I was confused about using the method named getContentResolver(). What does this method return?
My ContentProvider is not instanced and the code just writes that getContentProvider().query().
I don't understand how ContentProvider works.
It returns Content Resolver.
What is the Content Resolver?
The Content Resolver is the single, global instance in your application that provides access to your (and other applications') content providers. The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers. This design is important, as it allows a simple and secure means of accessing other applications' Content Providers.
The Content Resolver includes the CRUD (create, read, update, delete) methods corresponding to the abstract methods (insert, delete, query, update) in the Content Provider class. The Content Resolver does not know the implementation of the Content Providers it is interacting with (nor does it need to know); each method is passed an URI that specifies the Content Provider to interact with.
What is the Content Provider?
Whereas the Content Resolver provides an abstraction from the application's Content Providers, Content Providers provides an abstraction from the underlying data source (i.e. a SQLite database). They provide mechanisms for defining data security (i.e. by enforcing read/write permissions) and offer a standard interface that connects data in one process with code running in another process.
Content Providers provide an interface for publishing and consuming data, based around a simple URI addressing model using the content:// schema. They enable you to decouple your application layers from the underlying data layers, making your application data-source agnostic by abstracting the underlying data source.
Source - androiddesignpatterns
getContentResolver () return a ContentResolver instance for your application's package.
Pasting it from developer.android.com
Content providers manage access to a structured set of data. 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.
When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.
http://developer.android.com/guide/topics/providers/content-providers.html
I am reading the developer guide about the content providers and as it's mentioned there that I shouldn't create my own provider unless there is a need to.
So my questions are:
Can I use content resolver in my application without creating my own provider?
What is the best approach? should I use always a content resolver (and create my own provider if necessary) to do all the operations with the DB or talking to SQLiteHelper class directly.
Can I use content resolver in my application without creating my own provider?
You can use a ContentResolver with any other existing ContentProvider, such as those supplied by the OS (e.g., ContactsContract).
should I use always a content resolver (and create my own provider if necessary) to do all the operations with the DB or talking to SQLiteHelper class directly
There are differing opinions on that subject. Personally, I am not a big fan of ContentProvider, and so I only use it when I need to, such as for inter-process communication. However, there are those who think that using ContentProvider purely for internal use within an app is wonderful. There is no right or wrong answer.
Q-1 - To create my own Content Provider class, when I should extend ContentProvider class and when I should not extend ContentProvider class ?
Q-2 - If I create Content Provider without CONTENT_URI(like many other built-in content providers in android.provider.*, how will I use managedQuery(...) or query(....).
I have seen response to similar question in this question on content provider but I am not sure they answer it completly.
1) Even with extension you'd be implementing the methods anyway. A ContentProvider allows you to actually use the internal system for Android to access data from different places in your application. Basically, if you are storing data, extend the ContentProvider and use either ContentResolver.query or Activity.managedQuery to access that data.
2) AFAIK (and this could be wrong), you need a CONTENT_URI when you create a ContentProvider. This is how the ContentResolver knows where it should be pulling from and one way to allow your application to even access that data (through the application manifest). So, use a CONTENT_URI. There isn't much of a reason not to IMO.